#
cargo/util/
restricted_names.rs

    1//! Helpers for validating and checking names like package and crate names.
    2
    3use std::path::Path;
    4
    5/// Returns `true` if the name contains non-ASCII characters.
    6pub fn is_non_ascii_name(name: &str) -> bool {
    7    name.chars().any(|ch| ch > '\x7f')
    8}
    9
    10/// A Rust keyword.
    11pub fn is_keyword(name: &str) -> bool {
    12    // See https://doc.rust-lang.org/reference/keywords.html
    13    [
    14        "Self", "abstract", "as", "async", "await", "become", "box", "break", "const", "continue",
    15        "crate", "do", "dyn", "else", "enum", "extern", "false", "final", "fn", "for", "if",
    16        "impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "override", "priv",
    17        "pub", "ref", "return", "self", "static", "struct", "super", "trait", "true", "try",
    18        "type", "typeof", "unsafe", "unsized", "use", "virtual", "where", "while", "yield",
    19    ]
    20    .contains(&name)
    21}
    22
    23/// These names cannot be used on Windows, even with an extension.
    24pub fn is_windows_reserved(name: &str) -> bool {
    25    [
    26        "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8",
    27        "com9", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9",
    28    ]
    29    .contains(&name.to_ascii_lowercase().as_str())
    30}
    31
    32/// An artifact with this name will conflict with one of Cargo's build directories.
    33pub fn is_conflicting_artifact_name(name: &str) -> bool {
    34    ["deps", "examples", "build", "incremental"].contains(&name)
    35}
    36
    37/// Check the entire path for names reserved in Windows.
    38pub fn is_windows_reserved_path(path: &Path) -> bool {
    39    path.iter()
    40        .filter_map(|component| component.to_str())
    41        .any(|component| {
    42            let stem = component.split('.').next().unwrap();
    43            is_windows_reserved(stem)
    44        })
    45}
    46
    47/// Returns `true` if the name contains any glob pattern wildcards.
    48pub fn is_glob_pattern<T: AsRef<str>>(name: T) -> bool {
    49    name.as_ref().contains(&['*', '?', '[', ']'][..])
    50}