#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CodeLanguage {
Rust,
TypeScript,
JavaScript,
Python,
Shell,
Json,
Toml,
Markdown,
Yaml,
Other(String),
Unknown,
}
impl CodeLanguage {
pub fn from_token(token: &str) -> Self {
match token.to_ascii_lowercase().as_str() {
"rs" | "rust" => Self::Rust,
"ts" | "tsx" | "typescript" => Self::TypeScript,
"js" | "jsx" | "javascript" | "node" => Self::JavaScript,
"py" | "python" | "python3" => Self::Python,
"sh" | "bash" | "zsh" | "shell" => Self::Shell,
"json" => Self::Json,
"toml" => Self::Toml,
"md" | "markdown" => Self::Markdown,
"yaml" | "yml" => Self::Yaml,
"" => Self::Unknown,
other => Self::Other(other.to_string()),
}
}
pub fn syntect_token(&self) -> Option<&str> {
match self {
Self::Rust => Some("rust"),
Self::TypeScript => Some("ts"),
Self::JavaScript => Some("js"),
Self::Python => Some("python"),
Self::Shell => Some("bash"),
Self::Json => Some("json"),
Self::Toml => Some("toml"),
Self::Markdown => Some("markdown"),
Self::Yaml => Some("yaml"),
Self::Other(token) => Some(token.as_str()),
Self::Unknown => None,
}
}
pub fn label(&self) -> &str {
self.syntect_token().unwrap_or("plain text")
}
}