fix: Check if name in keyword (#1020)

This commit is contained in:
Aadhi 2021-11-16 06:24:51 +05:30 committed by GitHub
parent 7b86aed638
commit ab3e1294d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 0 deletions

View File

@ -392,6 +392,27 @@ fn init(cfg_override: &ConfigOverride, name: String, javascript: bool) -> Result
return Err(anyhow!("Workspace already initialized"));
}
// The list is taken from https://doc.rust-lang.org/reference/keywords.html.
let key_words = [
"as", "break", "const", "continue", "crate", "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", "unsafe",
"use", "where", "while", "async", "await", "dyn", "abstract", "become", "box", "do",
"final", "macro", "override", "priv", "typeof", "unsized", "virtual", "yield", "try",
"unique",
];
if key_words.contains(&name[..].into()) {
return Err(anyhow!(
"{} is a reserved word in rust, name your project something else!",
name
));
} else if name.chars().next().unwrap().is_numeric() {
return Err(anyhow!(
"Cannot start project name with numbers, name your project something else!"
));
}
fs::create_dir(name.clone())?;
std::env::set_current_dir(&name)?;
fs::create_dir("app")?;