Skip to main content

embed_src/
lang.rs

1/// Maps a file extension to the corresponding code fence language identifier.
2pub fn ext_to_lang(ext: &str) -> &'static str {
3    match ext {
4        "rs" => "rust",
5        "ts" => "typescript",
6        "js" => "javascript",
7        "py" => "python",
8        "sh" | "bash" => "bash",
9        "yml" | "yaml" => "yaml",
10        "json" => "json",
11        "go" => "go",
12        "java" => "java",
13        "c" => "c",
14        "cpp" | "cc" | "cxx" => "cpp",
15        "h" | "hpp" => "c",
16        "rb" => "ruby",
17        "sql" => "sql",
18        "html" => "html",
19        "css" => "css",
20        "md" => "markdown",
21        "toml" => "toml",
22        "xml" => "xml",
23        "txt" => "text",
24        _ => "",
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn known_extensions() {
34        assert_eq!(ext_to_lang("rs"), "rust");
35        assert_eq!(ext_to_lang("py"), "python");
36        assert_eq!(ext_to_lang("yml"), "yaml");
37        assert_eq!(ext_to_lang("yaml"), "yaml");
38        assert_eq!(ext_to_lang("cpp"), "cpp");
39    }
40
41    #[test]
42    fn unknown_extension() {
43        assert_eq!(ext_to_lang("zzz"), "");
44        assert_eq!(ext_to_lang(""), "");
45    }
46}