xur-std 0.1.7

The standard library of the Xur language
Documentation
//! Which cargo feature of xur-std provides each module, for generated docs.

/// The cargo feature that provides the module at `path` (`std::fs` →
/// `fs`), or `None` for modules that are always there.
pub fn module_feature(path: &str) -> Option<&'static str> {
    MODULE_FEATURES
        .iter()
        .filter(|(prefix, _)| path == *prefix || path.starts_with(&format!("{prefix}::")))
        .max_by_key(|(prefix, _)| prefix.len())
        .map(|&(_, feature)| feature)
}

/// Module path prefixes and their features; the longest match wins.
const MODULE_FEATURES: &[(&str, &str)] = &[
    ("std::cli", "cli"),
    ("std::datetime", "datetime"),
    ("std::decimal", "rust_decimal"),
    ("std::env", "env"),
    ("std::fs", "fs"),
    ("std::gui", "gui"),
    ("std::hash", "hash"),
    ("std::http", "http"),
    ("std::json", "json"),
    ("std::log", "log"),
    ("std::net", "net"),
    ("std::process", "process"),
    ("std::ptr", "ptr"),
    ("std::random", "random"),
    ("std::regex", "regex"),
    ("std::thread", "thread"),
    ("std::time", "time"),
    ("std::re::asm", "re-asm"),
    ("std::re::dotnet", "re-dotnet"),
    ("std::re::dotnet::il2cpp", "re-dotnet-il2cpp"),
    ("std::re::dotnet::runtime", "re-dotnet-runtime"),
    ("std::re::elf", "re-image"),
    ("std::re::pe", "re-image"),
    ("std::re::proc", "re-proc"),
];

#[cfg(test)]
mod tests {
    use super::module_feature;

    #[test]
    fn the_longest_prefix_wins() {
        assert_eq!(module_feature("std::fs"), Some("fs"));
        assert_eq!(module_feature("std::re::dotnet::sig"), Some("re-dotnet"));
        assert_eq!(
            module_feature("std::re::dotnet::il2cpp::binary"),
            Some("re-dotnet-il2cpp")
        );
        assert_eq!(module_feature("std::fmt"), None);
        assert_eq!(module_feature("std::fsx"), None);
    }
}