pub mod emoji;
pub mod file_type;
pub mod profiles;
use std::path::Path;
pub use emoji::EmojiMapper;
pub use file_type::FileType;
#[allow(dead_code)]
pub trait Profile {
fn file_type(&self) -> FileType;
fn emoji(&self) -> &str;
fn name(&self) -> &str;
fn extensions(&self) -> &[&str];
fn matches(&self, path: &Path) -> bool {
if let Some(ext) = path.extension() {
if let Some(ext_str) = ext.to_str() {
return self.extensions().contains(&ext_str);
}
}
false
}
fn should_count_lines(&self) -> bool {
true
}
}
#[allow(dead_code)]
pub struct ProfileRegistry {
profiles: Vec<Box<dyn Profile>>,
}
impl ProfileRegistry {
#[allow(dead_code)]
pub fn new() -> Self {
Self {
profiles: Vec::new(),
}
}
#[allow(dead_code)]
pub fn register(&mut self, profile: Box<dyn Profile>) {
self.profiles.push(profile);
}
#[allow(dead_code)]
pub fn find_profile(&self, path: &Path) -> Option<&dyn Profile> {
for profile in &self.profiles {
if profile.matches(path) {
return Some(profile.as_ref());
}
}
None
}
#[allow(dead_code)]
pub fn classify_file(&self, path: &Path) -> FileType {
if let Some(profile) = self.find_profile(path) {
profile.file_type()
} else {
FileType::classify_path(path)
}
}
}
impl Default for ProfileRegistry {
fn default() -> Self {
let mut registry = Self::new();
profiles::register_builtin_profiles(&mut registry);
registry
}
}