Skip to content

Commit a799550

Browse files
committed
sanitize all rust keywords
This list of keywords was pulled from the Rust grammar [1]. This also changes the sanitize method to take ownership of its argument. [1]: https://doc.rust-lang.org/grammar.html#keywords
1 parent d7b0f03 commit a799550

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,23 @@ use syn::*;
261261
/// Trait that sanitizes name avoiding rust keywords and the like.
262262
trait SanitizeName {
263263
/// Sanitize a name; avoiding Rust keywords and the like.
264-
fn sanitize(&self) -> String;
264+
fn sanitize(self) -> String;
265265
}
266266

267267
impl SanitizeName for String {
268-
fn sanitize(&self) -> String {
269-
match self.as_str() {
270-
"fn" => "fn_".to_owned(),
271-
"in" => "in_".to_owned(),
272-
"match" => "match_".to_owned(),
273-
"mod" => "mod_".to_owned(),
274-
_ => self.to_owned(),
268+
fn sanitize(self) -> String {
269+
const KEYWORDS: [&'static str; 52] =
270+
["abstract", "alignof", "as", "become", "box", "break", "const", "continue", "crate",
271+
"do", "else", "enum", "extern", "false", "final", "fn", "for", "if", "impl", "in",
272+
"let", "loop", "macro", "match", "mod", "move", "mut", "offsetof", "override",
273+
"priv", "proc", "pub", "pure", "ref", "return", "Self", "self", "sizeof", "static",
274+
"struct", "super", "trait", "true", "type", "typeof", "unsafe", "unsized", "use",
275+
"virtual", "where", "while", "yield"];
276+
277+
if KEYWORDS.contains(&self.as_str()) {
278+
self + "_"
279+
} else {
280+
self
275281
}
276282
}
277283
}

0 commit comments

Comments
 (0)