Skip to content

Commit

Permalink
Escape the full list of Rust keywords when converting identifiers (#278)
Browse files Browse the repository at this point in the history
A couple of keywords were already escaped, but not all of them; this changes it to escape all keywords.
  • Loading branch information
Liamolucko committed Jul 11, 2022
1 parent 7b7625a commit 2679e06
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions crates/gen-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,12 +1009,58 @@ pub trait RustFunctionGenerator {

pub fn to_rust_ident(name: &str) -> String {
match name {
// Escape Rust keywords.
// Source: https://doc.rust-lang.org/reference/keywords.html
"as" => "as_".into(),
"break" => "break_".into(),
"const" => "const_".into(),
"continue" => "continue_".into(),
"crate" => "crate_".into(),
"else" => "else_".into(),
"enum" => "enum_".into(),
"extern" => "extern_".into(),
"false" => "false_".into(),
"fn" => "fn_".into(),
"for" => "for_".into(),
"if" => "if_".into(),
"impl" => "impl_".into(),
"in" => "in_".into(),
"let" => "let_".into(),
"loop" => "loop_".into(),
"match" => "match_".into(),
"mod" => "mod_".into(),
"move" => "move_".into(),
"mut" => "mut_".into(),
"pub" => "pub_".into(),
"ref" => "ref_".into(),
"return" => "return_".into(),
"self" => "self_".into(),
"static" => "static_".into(),
"struct" => "struct_".into(),
"super" => "super_".into(),
"trait" => "trait_".into(),
"true" => "true_".into(),
"type" => "type_".into(),
"unsafe" => "unsafe_".into(),
"use" => "use_".into(),
"where" => "where_".into(),
"yield" => "yield_".into(),
"while" => "while_".into(),
"async" => "async_".into(),
"self" => "self_".into(),
"await" => "await_".into(),
"dyn" => "dyn_".into(),
"abstract" => "abstract_".into(),
"become" => "become_".into(),
"box" => "box_".into(),
"do" => "do_".into(),
"final" => "final_".into(),
"macro" => "macro_".into(),
"override" => "override_".into(),
"priv" => "priv_".into(),
"typeof" => "typeof_".into(),
"unsized" => "unsized_".into(),
"virtual" => "virtual_".into(),
"yield" => "yield_".into(),
"try" => "try_".into(),
s => s.to_snake_case(),
}
}
Expand Down

0 comments on commit 2679e06

Please sign in to comment.