#[must_use]
pub fn extension_to_language(ext: &str) -> Option<&'static str> {
match ext.to_lowercase().as_str() {
"rs" => Some("Rust"),
"py" | "pyw" | "pyi" => Some("Python"),
"js" | "mjs" | "cjs" | "jsx" => Some("JavaScript"),
"ts" | "mts" | "cts" | "tsx" => Some("TypeScript"),
"go" => Some("Go"),
"java" => Some("Java"),
"kt" | "kts" => Some("Kotlin"),
"c" | "h" => Some("C"),
"cpp" | "cc" | "cxx" | "hpp" | "hxx" | "hh" => Some("C++"),
"cs" => Some("C#"),
"rb" | "erb" => Some("Ruby"),
"php" => Some("PHP"),
"swift" => Some("Swift"),
"scala" | "sc" => Some("Scala"),
"sh" | "bash" | "zsh" => Some("Shell"),
"sql" => Some("SQL"),
"yml" | "yaml" => Some("YAML"),
"json" => Some("JSON"),
"html" | "htm" => Some("HTML"),
"css" => Some("CSS"),
"scss" => Some("SCSS"),
"sass" => Some("Sass"),
"less" => Some("Less"),
"lua" => Some("Lua"),
"pl" | "pm" => Some("Perl"),
"r" => Some("R"),
"dart" => Some("Dart"),
"ex" | "exs" => Some("Elixir"),
"hs" | "lhs" => Some("Haskell"),
"ml" | "mli" => Some("OCaml"),
"fs" | "fsi" | "fsx" => Some("F#"),
"clj" | "cljs" | "cljc" | "edn" => Some("Clojure"),
"zig" => Some("Zig"),
"nim" => Some("Nim"),
"v" => Some("V"),
_ => None,
}
}
pub(super) fn is_non_primary_language(lang: &str) -> bool {
matches!(
lang,
"YAML" | "JSON" | "HTML" | "CSS" | "SCSS" | "Sass" | "Less"
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extension_to_language() {
assert_eq!(extension_to_language("rs"), Some("Rust"));
assert_eq!(extension_to_language("py"), Some("Python"));
assert_eq!(extension_to_language("js"), Some("JavaScript"));
assert_eq!(extension_to_language("ts"), Some("TypeScript"));
assert_eq!(extension_to_language("go"), Some("Go"));
assert_eq!(extension_to_language("java"), Some("Java"));
assert_eq!(extension_to_language("rb"), Some("Ruby"));
assert_eq!(extension_to_language("yml"), Some("YAML"));
assert_eq!(extension_to_language("json"), Some("JSON"));
assert_eq!(extension_to_language("html"), Some("HTML"));
assert_eq!(extension_to_language("css"), Some("CSS"));
assert_eq!(extension_to_language("unknown"), None);
}
#[test]
fn test_is_non_primary_language() {
assert!(is_non_primary_language("YAML"));
assert!(is_non_primary_language("JSON"));
assert!(is_non_primary_language("HTML"));
assert!(is_non_primary_language("CSS"));
assert!(!is_non_primary_language("Rust"));
assert!(!is_non_primary_language("Python"));
}
}