use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum Language {
Python = 0,
Rust = 1,
TypeScript = 2,
JavaScript = 3,
Go = 4,
Cpp = 5,
Java = 6,
CSharp = 7,
Unknown = 255,
}
impl Language {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Python),
1 => Some(Self::Rust),
2 => Some(Self::TypeScript),
3 => Some(Self::JavaScript),
4 => Some(Self::Go),
5 => Some(Self::Cpp),
6 => Some(Self::Java),
7 => Some(Self::CSharp),
255 => Some(Self::Unknown),
_ => None,
}
}
pub fn from_extension(ext: &str) -> Self {
match ext.to_lowercase().as_str() {
"py" | "pyi" => Self::Python,
"rs" => Self::Rust,
"ts" | "tsx" => Self::TypeScript,
"js" | "jsx" | "mjs" | "cjs" => Self::JavaScript,
"go" => Self::Go,
"cpp" | "cc" | "cxx" | "c++" | "h" | "hpp" | "hxx" | "hh" => Self::Cpp,
"java" => Self::Java,
"cs" => Self::CSharp,
_ => Self::Unknown,
}
}
pub fn from_path(path: &Path) -> Self {
path.extension()
.and_then(|ext| ext.to_str())
.map(Self::from_extension)
.unwrap_or(Self::Unknown)
}
pub fn tree_sitter_language(&self) -> Option<tree_sitter::Language> {
match self {
Self::Python => Some(tree_sitter_python::language()),
Self::Rust => Some(tree_sitter_rust::language()),
Self::TypeScript => Some(tree_sitter_typescript::language_typescript()),
Self::JavaScript => Some(tree_sitter_javascript::language()),
Self::Go => Some(tree_sitter_go::language()),
Self::Cpp => Some(tree_sitter_cpp::language()),
Self::Java => Some(tree_sitter_java::language()),
Self::CSharp => Some(tree_sitter_c_sharp::language()),
Self::Unknown => None,
}
}
pub fn name(&self) -> &'static str {
match self {
Self::Python => "Python",
Self::Rust => "Rust",
Self::TypeScript => "TypeScript",
Self::JavaScript => "JavaScript",
Self::Go => "Go",
Self::Cpp => "C++",
Self::Java => "Java",
Self::CSharp => "C#",
Self::Unknown => "Unknown",
}
}
}
impl std::fmt::Display for Language {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}