pub const UNREPRESENTABLE_KEYWORDS: &[&str] = &["self", "Self", "super", "crate"];
const RAW_ESCAPABLE_KEYWORDS: &[&str] = &[
"as", "break", "const", "continue", "else", "enum", "extern", "false", "fn", "for", "if",
"impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return", "static",
"struct", "trait", "true", "type", "unsafe", "use", "where", "while",
"async", "await", "dyn", "abstract", "become", "box", "do", "final", "macro", "override", "priv", "typeof", "unsized",
"virtual", "yield", "try", "gen",
];
pub fn is_rust_keyword(name: &str) -> bool {
is_raw_escapable_keyword(name) || is_unrepresentable_keyword(name)
}
pub fn is_raw_escapable_keyword(name: &str) -> bool {
RAW_ESCAPABLE_KEYWORDS.contains(&name)
}
pub fn is_unrepresentable_keyword(name: &str) -> bool {
UNREPRESENTABLE_KEYWORDS.contains(&name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escapable_keywords_are_not_also_unrepresentable() {
for keyword in RAW_ESCAPABLE_KEYWORDS {
assert!(
!is_unrepresentable_keyword(keyword),
"`{keyword}` listed as both raw-escapable and unrepresentable"
);
}
}
#[test]
fn ordinary_identifiers_are_not_keywords() {
for name in ["user", "email", "created_at", "matches", "typeName"] {
assert!(!is_rust_keyword(name), "`{name}` should not be a keyword");
}
}
#[test]
fn ticket_398_keyword_table_is_covered() {
for keyword in [
"match", "type", "ref", "move", "impl", "fn", "let", "loop", "box",
] {
assert!(is_raw_escapable_keyword(keyword));
}
for keyword in ["self", "crate"] {
assert!(is_unrepresentable_keyword(keyword));
}
}
}