1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
use std::ffi::OsStr; use std::path::Path; pub enum Indent { AsIs, Fixed(&'static str), } #[derive(Debug, Clone, Copy, PartialEq)] pub enum Language { Plain, C, Rust, JavaScript, Go, Cpp, } impl Language { pub fn name(self) -> &'static str { use Language::*; match self { Plain => "plain", C => "c", Rust => "rust", JavaScript => "javascript", Go => "go", Cpp => "c++", } } fn file_exts(self) -> &'static [&'static str] { use Language::*; match self { Plain => &[], C => &["c", "h"], Rust => &["rs"], JavaScript => &["js"], Go => &["go"], Cpp => &["cpp", "hpp", "cxx", "hxx", "cc", "hh"], } } pub fn indent(self) -> Indent { use Language::*; match self { Plain | Go => Indent::AsIs, C | Rust | Cpp => Indent::Fixed(" "), JavaScript => Indent::Fixed(" "), } } pub fn detect<P: AsRef<Path>>(path: P) -> Language { use Language::*; if let Some(ext) = path.as_ref().extension().and_then(OsStr::to_str) { for lang in &[C, Rust, JavaScript, Go, Cpp] { if lang.file_exts().contains(&ext) { return *lang; } } } Plain } }