diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 5d064f88..17e74130 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -642,6 +642,20 @@ mod tests { not_null: true, unique: false, }, + Column { + name: "crate".to_owned(), + col_type: ColumnType::Integer(Some(11)), + auto_increment: false, + not_null: true, + unique: false, + }, + Column { + name: "self".to_owned(), + col_type: ColumnType::Integer(Some(11)), + auto_increment: false, + not_null: true, + unique: false, + }, ], relations: vec![], conjunct_relations: vec![], diff --git a/sea-orm-codegen/src/util.rs b/sea-orm-codegen/src/util.rs index e752215b..34c46c54 100644 --- a/sea-orm-codegen/src/util.rs +++ b/sea-orm-codegen/src/util.rs @@ -3,25 +3,21 @@ where T: ToString, { let string = string.to_string(); - if is_rust_keyword(&string) { + if RUST_KEYWORDS.iter().any(|s| s.eq(&string)) { format!("r#{}", string) + } else if RUST_SPECIAL_KEYWORDS.iter().any(|s| s.eq(&string)) { + format!("{}_", string) } else { string } } -pub(crate) fn is_rust_keyword(string: T) -> bool -where - T: ToString, -{ - let string = string.to_string(); - RUST_KEYWORDS.iter().any(|s| s.eq(&string)) -} - -pub(crate) const RUST_KEYWORDS: [&str; 52] = [ - "as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", - "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", - "ref", "return", "Self", "self", "static", "struct", "super", "trait", "true", "type", "union", - "unsafe", "use", "where", "while", "abstract", "become", "box", "do", "final", "macro", - "override", "priv", "try", "typeof", "unsized", "virtual", "yield", +pub(crate) const RUST_KEYWORDS: [&str; 49] = [ + "as", "async", "await", "break", "const", "continue", "dyn", "else", "enum", "extern", "false", + "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", + "return", "static", "struct", "super", "trait", "true", "type", "union", "unsafe", "use", + "where", "while", "abstract", "become", "box", "do", "final", "macro", "override", "priv", + "try", "typeof", "unsized", "virtual", "yield", ]; + +pub(crate) const RUST_SPECIAL_KEYWORDS: [&str; 3] = ["crate", "Self", "self"]; diff --git a/sea-orm-codegen/tests/compact/rust_keyword.rs b/sea-orm-codegen/tests/compact/rust_keyword.rs index 9e51bafd..229eae22 100644 --- a/sea-orm-codegen/tests/compact/rust_keyword.rs +++ b/sea-orm-codegen/tests/compact/rust_keyword.rs @@ -12,6 +12,8 @@ pub struct Model { pub keywords: i32, pub r#type: i32, pub r#typeof: i32, + pub crate_: i32, + pub self_: i32, } #[derive(Copy, Clone, Debug, EnumIter)] diff --git a/sea-orm-codegen/tests/expanded/rust_keyword.rs b/sea-orm-codegen/tests/expanded/rust_keyword.rs index 5c24d71c..1ab8a627 100644 --- a/sea-orm-codegen/tests/expanded/rust_keyword.rs +++ b/sea-orm-codegen/tests/expanded/rust_keyword.rs @@ -19,6 +19,8 @@ pub struct Model { pub keywords: i32, pub r#type: i32, pub r#typeof: i32, + pub crate_: i32, + pub self_: i32, } #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] @@ -29,6 +31,8 @@ pub enum Column { Keywords, Type, Typeof, + Crate, + Self_, } #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] @@ -58,6 +62,8 @@ impl ColumnTrait for Column { Self::Keywords => ColumnType::Integer.def(), Self::Type => ColumnType::Integer.def(), Self::Typeof => ColumnType::Integer.def(), + Self::Crate => ColumnType::Integer.def(), + Self::Self_ => ColumnType::Integer.def(), } } } diff --git a/sea-orm-macros/src/util.rs b/sea-orm-macros/src/util.rs index 8929e9e8..379b486c 100644 --- a/sea-orm-macros/src/util.rs +++ b/sea-orm-macros/src/util.rs @@ -40,27 +40,23 @@ where T: ToString, { let string = string.to_string(); - if is_rust_keyword(&string) { + if RUST_KEYWORDS.iter().any(|s| s.eq(&string)) { format!("r#{}", string) + } else if RUST_SPECIAL_KEYWORDS.iter().any(|s| s.eq(&string)) { + format!("{}_", string) } else { string } } -pub(crate) fn is_rust_keyword(string: T) -> bool -where - T: ToString, -{ - let string = string.to_string(); - RUST_KEYWORDS.iter().any(|s| s.eq(&string)) -} - pub(crate) const RAW_IDENTIFIER: &str = "r#"; -pub(crate) const RUST_KEYWORDS: [&str; 52] = [ - "as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", - "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", - "ref", "return", "Self", "self", "static", "struct", "super", "trait", "true", "type", "union", - "unsafe", "use", "where", "while", "abstract", "become", "box", "do", "final", "macro", - "override", "priv", "try", "typeof", "unsized", "virtual", "yield", +pub(crate) const RUST_KEYWORDS: [&str; 49] = [ + "as", "async", "await", "break", "const", "continue", "dyn", "else", "enum", "extern", "false", + "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", + "return", "static", "struct", "super", "trait", "true", "type", "union", "unsafe", "use", + "where", "while", "abstract", "become", "box", "do", "final", "macro", "override", "priv", + "try", "typeof", "unsized", "virtual", "yield", ]; + +pub(crate) const RUST_SPECIAL_KEYWORDS: [&str; 3] = ["crate", "Self", "self"]; diff --git a/src/tests_cfg/rust_keyword.rs b/src/tests_cfg/rust_keyword.rs index 30052db6..c8662347 100644 --- a/src/tests_cfg/rust_keyword.rs +++ b/src/tests_cfg/rust_keyword.rs @@ -16,6 +16,7 @@ pub struct Model { pub r#break: i32, pub r#const: i32, pub r#continue: i32, + pub crate_: i32, pub r#dyn: i32, pub r#else: i32, pub r#enum: i32, @@ -35,6 +36,7 @@ pub struct Model { pub r#pub: i32, pub r#ref: i32, pub r#return: i32, + pub self_: i32, pub r#static: i32, pub r#struct: i32, pub r#trait: i32, @@ -92,6 +94,7 @@ mod tests { assert_eq!(Column::Const.to_string().as_str(), "const"); assert_eq!(Column::Continue.to_string().as_str(), "continue"); assert_eq!(Column::Dyn.to_string().as_str(), "dyn"); + assert_eq!(Column::Crate.to_string().as_str(), "crate"); assert_eq!(Column::Else.to_string().as_str(), "else"); assert_eq!(Column::Enum.to_string().as_str(), "enum"); assert_eq!(Column::Extern.to_string().as_str(), "extern"); @@ -110,6 +113,7 @@ mod tests { assert_eq!(Column::Pub.to_string().as_str(), "pub"); assert_eq!(Column::Ref.to_string().as_str(), "ref"); assert_eq!(Column::Return.to_string().as_str(), "return"); + assert_eq!(Column::Self_.to_string().as_str(), "self"); assert_eq!(Column::Static.to_string().as_str(), "static"); assert_eq!(Column::Struct.to_string().as_str(), "struct"); assert_eq!(Column::Trait.to_string().as_str(), "trait");