Handle crate, Self and self rust keywords

This commit is contained in:
Billy Chan 2021-10-06 22:24:52 +08:00
parent a970e43f50
commit d6b53abd95
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
6 changed files with 48 additions and 30 deletions

View File

@ -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![],

View File

@ -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<T>(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"];

View File

@ -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)]

View File

@ -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(),
}
}
}

View File

@ -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<T>(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"];

View File

@ -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");