Skip to content

Commit df1e2ed

Browse files
committed
Map every non-ascii-alphanumeric character to an underscore
1 parent 9ed2f9b commit df1e2ed

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

diesel_cli/src/infer_schema_internals/inference.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ static RESERVED_NAMES: &[&str] = &[
1414
"type", "typeof", "unsafe", "unsized", "use", "virtual", "where", "while", "yield",
1515
];
1616

17-
static UNMAPPABLE_CHARS: &[char] = &['-', ' '];
18-
1917
fn is_reserved_name(name: &str) -> bool {
2018
RESERVED_NAMES.contains(&name)
2119
|| (
@@ -26,20 +24,21 @@ fn is_reserved_name(name: &str) -> bool {
2624
}
2725

2826
fn contains_unmappable_chars(name: &str) -> bool {
29-
UNMAPPABLE_CHARS.iter().any(|c| name.contains(*c))
27+
// Rust identifier names are restricted to [a-zA-Z0-9_].
28+
!name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
3029
}
3130

3231
pub fn rust_name_for_sql_name(sql_name: &str) -> Option<String> {
3332
if is_reserved_name(sql_name) {
3433
Some(format!("{}_", sql_name))
3534
} else if contains_unmappable_chars(sql_name) {
36-
Some(
37-
UNMAPPABLE_CHARS
38-
.iter()
39-
.fold(sql_name.to_string(), |rust_name, c| {
40-
rust_name.replace(*c, "_")
41-
}),
42-
)
35+
// Map each non-alphanumeric character ([^a-zA-Z0-9]) to an underscore.
36+
let rust_name = sql_name
37+
.chars()
38+
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
39+
.collect();
40+
41+
Some(rust_name)
4342
} else {
4443
None
4544
}

0 commit comments

Comments
 (0)