use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ImportLanguage {
Rust,
Python,
Java,
JavaScript,
TypeScript,
Tsx,
}
impl ImportLanguage {
#[must_use]
pub fn from_path(path: &Path) -> Option<Self> {
let ext = path.extension()?.to_str()?;
match ext {
"rs" => Some(Self::Rust),
"py" | "pyi" => Some(Self::Python),
"java" => Some(Self::Java),
"js" | "jsx" | "mjs" | "cjs" => Some(Self::JavaScript),
"ts" => Some(Self::TypeScript),
"tsx" => Some(Self::Tsx),
_ => None,
}
}
#[must_use]
pub fn language(self) -> tree_sitter::Language {
match self {
Self::Rust => tree_sitter_rust::LANGUAGE.into(),
Self::Python => tree_sitter_python::LANGUAGE.into(),
Self::Java => tree_sitter_java::LANGUAGE.into(),
Self::JavaScript => tree_sitter_javascript::LANGUAGE.into(),
Self::TypeScript => tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
Self::Tsx => tree_sitter_typescript::LANGUAGE_TSX.into(),
}
}
#[must_use]
pub fn import_node_kinds(self) -> &'static [&'static str] {
match self {
Self::Rust => &["use_declaration"],
Self::Python => &["import_statement", "import_from_statement"],
Self::Java => &["import_declaration"],
Self::JavaScript | Self::TypeScript | Self::Tsx => {
&["import_statement", "export_statement", "call_expression"]
}
}
}
}