Skip to main content

fluidattacks_blends_domain/
language.rs

1//! Counterpart of the `Language` enum in `blends/models/__init__.py`.
2
3use crate::syntax::dispatchers::{self, Dispatcher};
4
5/// A source language this crate has a syntax-graph dispatcher for.
6///
7/// New grammars are added over time, so callers must not match this
8/// exhaustively.
9#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
10#[non_exhaustive]
11pub enum Language {
12    /// C#.
13    CSharp,
14    /// Elixir.
15    Elixir,
16    /// Go.
17    Go,
18    /// `HashiCorp` Configuration Language (Terraform).
19    Hcl,
20    /// Java.
21    Java,
22    /// JavaScript.
23    JavaScript,
24    /// JSON.
25    Json,
26    /// Kotlin.
27    Kotlin,
28    /// PHP.
29    Php,
30    /// Python.
31    Python,
32    /// Ruby.
33    Ruby,
34    /// Rust.
35    Rust,
36    /// Scala.
37    Scala,
38    /// Swift.
39    Swift,
40    /// TypeScript.
41    TypeScript,
42    /// YAML.
43    Yaml,
44}
45
46impl Language {
47    /// The language a file extension (without its leading dot) implies, if any.
48    #[must_use]
49    pub fn from_extension(extension: &str) -> Option<Self> {
50        match extension {
51            "cs" => Some(Self::CSharp),
52            "ex" | "exs" => Some(Self::Elixir),
53            "go" => Some(Self::Go),
54            "hcl" | "tf" => Some(Self::Hcl),
55            "java" => Some(Self::Java),
56            "js" | "jsx" => Some(Self::JavaScript),
57            "json" => Some(Self::Json),
58            "kt" | "ktm" | "kts" => Some(Self::Kotlin),
59            "swift" => Some(Self::Swift),
60            "ts" | "tsx" => Some(Self::TypeScript),
61            "php" => Some(Self::Php),
62            "py" => Some(Self::Python),
63            "rb" => Some(Self::Ruby),
64            "rs" => Some(Self::Rust),
65            "scala" => Some(Self::Scala),
66            "yaml" | "yml" => Some(Self::Yaml),
67            _ => None,
68        }
69    }
70
71    pub(crate) fn syntax_dispatcher(self) -> Option<Dispatcher> {
72        match self {
73            Self::CSharp => Some(dispatchers::c_sharp::dispatcher),
74            Self::Go => Some(dispatchers::go::dispatcher),
75            Self::Java => Some(dispatchers::java::dispatcher),
76            Self::JavaScript => Some(dispatchers::javascript::dispatcher),
77            Self::Json => Some(dispatchers::json::dispatcher),
78            Self::Kotlin => Some(dispatchers::kotlin::dispatcher),
79            Self::Php => Some(dispatchers::php::dispatcher),
80            Self::Python => Some(dispatchers::python::dispatcher),
81            Self::Ruby => Some(dispatchers::ruby::dispatcher),
82            Self::Rust => Some(dispatchers::rust::dispatcher),
83            Self::Swift => Some(dispatchers::swift::dispatcher),
84            Self::TypeScript => Some(dispatchers::typescript::dispatcher),
85            Self::Yaml => Some(dispatchers::yaml::dispatcher),
86            _ => None,
87        }
88    }
89}