Skip to main content

codefold_core/
language.rs

1use std::path::Path;
2
3use crate::Error;
4
5/// A supported source language.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Language {
8    Python,
9    TypeScript,
10    Rust,
11    Go,
12}
13
14impl Language {
15    pub fn name(self) -> &'static str {
16        match self {
17            Language::Python => "python",
18            Language::TypeScript => "typescript",
19            Language::Rust => "rust",
20            Language::Go => "go",
21        }
22    }
23
24    pub fn detect(path: &Path) -> Result<Self, Error> {
25        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
26        match ext {
27            "py" | "pyi" => Ok(Language::Python),
28            "ts" => Ok(Language::TypeScript),
29            "rs" => Ok(Language::Rust),
30            "go" => Ok(Language::Go),
31            other => Err(Error::UnsupportedLanguage(other.to_string())),
32        }
33    }
34}