1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
5#[serde(rename_all = "lowercase")]
6pub enum Language {
7 Rust,
8 Python,
9 JavaScript,
10 TypeScript,
11 Go,
12 Unknown,
13}
14
15impl Language {
16 pub fn from_extension(ext: &str) -> Self {
17 match ext {
18 "rs" => Self::Rust,
19 "py" => Self::Python,
20 "js" | "mjs" | "cjs" => Self::JavaScript,
21 "ts" | "tsx" => Self::TypeScript,
22 "go" => Self::Go,
23 _ => Self::Unknown,
24 }
25 }
26
27 pub fn from_path(path: &std::path::Path) -> Self {
28 path.extension()
29 .and_then(|e| e.to_str())
30 .map(Self::from_extension)
31 .unwrap_or(Self::Unknown)
32 }
33}
34
35impl fmt::Display for Language {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 let s = match self {
38 Self::Rust => "rust",
39 Self::Python => "python",
40 Self::JavaScript => "javascript",
41 Self::TypeScript => "typescript",
42 Self::Go => "go",
43 Self::Unknown => "unknown",
44 };
45 write!(f, "{s}")
46 }
47}