Skip to main content

weavatrix_parse/syntax/
detection.rs

1use super::Language;
2
3impl Language {
4    /// The language a file extension selects, if this crate handles it.
5    #[must_use]
6    pub fn from_extension(extension: &str) -> Option<Self> {
7        Some(match extension.to_ascii_lowercase().as_str() {
8            "js" | "jsx" | "mjs" | "cjs" => Self::JavaScript,
9            "ts" | "tsx" | "mts" | "cts" => Self::TypeScript,
10            "graphql" | "gql" => Self::Graphql,
11            "proto" => Self::Protobuf,
12            "rs" => Self::Rust,
13            "py" | "pyi" => Self::Python,
14            "go" => Self::Go,
15            "java" => Self::Java,
16            "cs" => Self::CSharp,
17            "c" | "h" => Self::C,
18            "cc" | "cpp" | "cxx" | "hh" | "hpp" | "hxx" => Self::Cpp,
19            "sql" | "psql" => Self::Sql,
20            "sol" => Self::Solidity,
21            "swift" => Self::Swift,
22            "tf" | "tfvars" | "hcl" => Self::Terraform,
23            "html" | "htm" | "xhtml" | "vue" | "svelte" => Self::Html,
24            "xml" | "xsd" | "xsl" | "xslt" | "pom" | "csproj" | "vbproj" | "fsproj" | "props"
25            | "targets" | "plist" | "storyboard" | "xib" | "resx" | "nuspec" => Self::Xml,
26            "md" | "markdown" | "mdown" | "mkd" | "mkdn" => Self::Markdown,
27            "mdx" => Self::Mdx,
28            "rst" => Self::ReStructuredText,
29            "adoc" | "asciidoc" | "asc" => Self::AsciiDoc,
30            "css" => Self::Css,
31            "scss" | "sass" | "less" => Self::Scss,
32            "sh" | "bash" | "zsh" => Self::Bash,
33            "yaml" | "yml" => Self::Yaml,
34            _ => return None,
35        })
36    }
37
38    #[must_use]
39    pub const fn as_str(self) -> &'static str {
40        match self {
41            Self::JavaScript => "javascript",
42            Self::TypeScript => "typescript",
43            Self::Graphql => "graphql",
44            Self::Protobuf => "protobuf",
45            Self::Rust => "rust",
46            Self::Python => "python",
47            Self::Go => "go",
48            Self::Java => "java",
49            Self::CSharp => "csharp",
50            Self::C => "c",
51            Self::Cpp => "cpp",
52            Self::Sql => "sql",
53            Self::Solidity => "solidity",
54            Self::Swift => "swift",
55            Self::Terraform => "terraform",
56            Self::Xml => "xml",
57            Self::Markdown => "markdown",
58            Self::Mdx => "mdx",
59            Self::ReStructuredText => "rst",
60            Self::AsciiDoc => "asciidoc",
61            Self::Html => "html",
62            Self::Css => "css",
63            Self::Scss => "scss",
64            Self::Bash => "bash",
65            Self::Yaml => "yaml",
66        }
67    }
68}