Skip to content

Commit 1ffa6b7

Browse files
committed
Swap out Option<String> for Cow<str> when checking for unmappable chars
1 parent 2cf1117 commit 1ffa6b7

5 files changed

Lines changed: 24 additions & 33 deletions

File tree

diesel_cli/src/infer_schema_internals/data_structures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ impl fmt::Display for ColumnType {
5353
#[derive(Debug)]
5454
pub struct ColumnDefinition {
5555
pub sql_name: String,
56+
pub rust_name: String,
5657
pub ty: ColumnType,
5758
pub docs: String,
58-
pub rust_name: Option<String>,
5959
}
6060

6161
impl ColumnInformation {

diesel_cli/src/infer_schema_internals/inference.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use diesel::result::Error::NotFound;
55
use super::data_structures::*;
66
use super::table_data::*;
77
use crate::database::InferConnection;
8+
use std::borrow::Cow;
89

910
static RESERVED_NAMES: &[&str] = &[
1011
"abstract", "alignof", "as", "become", "box", "break", "const", "continue", "crate", "do",
@@ -28,9 +29,9 @@ fn contains_unmappable_chars(name: &str) -> bool {
2829
!name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
2930
}
3031

31-
pub fn rust_name_for_sql_name(sql_name: &str) -> Option<String> {
32+
pub fn rust_name_for_sql_name(sql_name: &str) -> Cow<str> {
3233
if is_reserved_name(sql_name) {
33-
Some(format!("{}_", sql_name))
34+
Cow::Owned(format!("{}_", sql_name))
3435
} else if contains_unmappable_chars(sql_name) {
3536
// Map each non-alphanumeric character ([^a-zA-Z0-9]) to an underscore.
3637
let mut rust_name: String = sql_name
@@ -49,9 +50,9 @@ pub fn rust_name_for_sql_name(sql_name: &str) -> Option<String> {
4950
last_len = rust_name.len();
5051
}
5152

52-
Some(rust_name)
53+
Cow::Owned(rust_name)
5354
} else {
54-
None
55+
Cow::Borrowed(sql_name)
5556
}
5657
}
5758

@@ -181,14 +182,14 @@ pub fn load_table_data(database_url: &str, name: TableName) -> Result<TableData,
181182
let primary_key = get_primary_keys(&connection, &name)?;
182183
let primary_key = primary_key
183184
.iter()
184-
.map(|k| rust_name_for_sql_name(&k).unwrap_or_else(|| k.clone()))
185+
.map(|k| rust_name_for_sql_name(&k).into())
185186
.collect();
186187

187188
let column_data = get_column_information(&connection, &name)?
188189
.into_iter()
189190
.map(|c| {
190191
let ty = determine_column_type(&c, &connection)?;
191-
let rust_name = rust_name_for_sql_name(&c.column_name);
192+
let rust_name = rust_name_for_sql_name(&c.column_name).to_string();
192193

193194
Ok(ColumnDefinition {
194195
docs: doc_comment!(

diesel_cli/src/infer_schema_internals/information_schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ where
192192
Ok(table_names
193193
.into_iter()
194194
.map(|name| TableName {
195-
rust_name: inference::rust_name_for_sql_name(&name),
195+
rust_name: inference::rust_name_for_sql_name(&name).to_string(),
196196
sql_name: name,
197197
schema: schema_name
198198
.filter(|&schema| schema != default_schema)

diesel_cli/src/infer_schema_internals/table_data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use super::inference;
99
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1010
pub struct TableName {
1111
pub sql_name: String,
12+
pub rust_name: String,
1213
pub schema: Option<String>,
13-
pub rust_name: Option<String>,
1414
}
1515

1616
impl TableName {
1717
pub fn from_name<T: Into<String>>(name: T) -> Self {
1818
let name = name.into();
1919

2020
TableName {
21-
rust_name: inference::rust_name_for_sql_name(&name),
21+
rust_name: inference::rust_name_for_sql_name(&name).to_string(),
2222
sql_name: name,
2323
schema: None,
2424
}
@@ -32,7 +32,7 @@ impl TableName {
3232
let name = name.into();
3333

3434
TableName {
35-
rust_name: inference::rust_name_for_sql_name(&name),
35+
rust_name: inference::rust_name_for_sql_name(&name).to_string(),
3636
sql_name: name,
3737
schema: Some(schema.into()),
3838
}
@@ -67,7 +67,7 @@ where
6767

6868
impl fmt::Display for TableName {
6969
fn fmt(&self, out: &mut fmt::Formatter) -> Result<(), fmt::Error> {
70-
let name = self.rust_name.as_ref().unwrap_or(&self.sql_name);
70+
let name = &self.rust_name;
7171

7272
match self.schema {
7373
Some(ref schema_name) => write!(out, "{}.{}", schema_name, name),

diesel_cli/src/print_schema.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ impl<'a> Display for TableDefinitions<'a> {
163163
let mut out = PadAdapter::new(f);
164164
writeln!(out)?;
165165
for table in &self.tables {
166-
if let Some(ref rust_name) = &table.name.rust_name {
167-
writeln!(out, "{},", rust_name)?;
168-
} else {
166+
if table.name.rust_name == table.name.sql_name {
169167
writeln!(out, "{},", table.name.sql_name)?;
168+
} else {
169+
writeln!(out, "{},", table.name.rust_name)?;
170170
}
171171
}
172172
}
@@ -203,7 +203,7 @@ impl<'a> Display for TableDefinition<'a> {
203203
}
204204
}
205205

206-
if self.table.name.rust_name.is_some() {
206+
if self.table.name.rust_name != self.table.name.sql_name {
207207
writeln!(
208208
out,
209209
r#"#[sql_name = "{}"]"#,
@@ -250,11 +250,11 @@ impl<'a> Display for ColumnDefinitions<'a> {
250250
writeln!(out, "///{}{}", if d.is_empty() { "" } else { " " }, d)?;
251251
}
252252
}
253-
if let Some(ref rust_name) = column.rust_name {
254-
writeln!(out, r#"#[sql_name = "{}"]"#, column.sql_name)?;
255-
writeln!(out, "{} -> {},", rust_name, column.ty)?;
256-
} else {
253+
if column.rust_name == column.sql_name {
257254
writeln!(out, "{} -> {},", column.sql_name, column.ty)?;
255+
} else {
256+
writeln!(out, r#"#[sql_name = "{}"]"#, column.sql_name)?;
257+
writeln!(out, "{} -> {},", column.rust_name, column.ty)?;
258258
}
259259
}
260260
}
@@ -267,19 +267,9 @@ struct Joinable<'a>(&'a ForeignKeyConstraint);
267267

268268
impl<'a> Display for Joinable<'a> {
269269
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
270-
let child_table_name = self
271-
.0
272-
.child_table
273-
.rust_name
274-
.as_ref()
275-
.unwrap_or(&self.0.child_table.sql_name);
276-
277-
let parent_table_name = self
278-
.0
279-
.parent_table
280-
.rust_name
281-
.as_ref()
282-
.unwrap_or(&self.0.parent_table.sql_name);
270+
let child_table_name = &self.0.child_table.rust_name;
271+
272+
let parent_table_name = &self.0.parent_table.rust_name;
283273

284274
write!(
285275
f,

0 commit comments

Comments
 (0)