pbjson_build/escape.rs
1//! Contains code to escape strings to avoid collisions with reserved Rust keywords
2
3pub fn escape_ident(mut ident: String) -> String {
4 // Copied from prost-build::ident
5 //
6 // Use a raw identifier if the identifier matches a Rust keyword:
7 // https://doc.rust-lang.org/reference/keywords.html.
8 match ident.as_str() {
9 // 2015 strict keywords.
10 | "as" | "break" | "const" | "continue" | "else" | "enum" | "false"
11 | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "match" | "mod" | "move" | "mut"
12 | "pub" | "ref" | "return" | "static" | "struct" | "trait" | "true"
13 | "type" | "unsafe" | "use" | "where" | "while"
14 // 2018 strict keywords.
15 | "dyn"
16 // 2015 reserved keywords.
17 | "abstract" | "become" | "box" | "do" | "final" | "macro" | "override" | "priv" | "typeof"
18 | "unsized" | "virtual" | "yield"
19 // 2018 reserved keywords.
20 | "async" | "await" | "try" => ident.insert_str(0, "r#"),
21 // the following keywords are not supported as raw identifiers and are therefore suffixed with an underscore.
22 "self" | "super" | "extern" | "crate" => ident += "_",
23 _ => (),
24 };
25 ident
26}
27
28pub fn escape_type(mut ident: String) -> String {
29 // this keyword is not supported as a raw identifier and is therefore suffixed with an underscore.
30 if ident == "Self" {
31 ident += "_";
32 }
33 ident
34}