From 3aaae291a4bf6efb003833eeebbc8ee2c0a6474e Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Sun, 7 Jan 2024 20:53:20 +0100 Subject: [PATCH] feat: add identifying repository files --- src/core/mod.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 26 ++++++++++++++++++++------ 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/core/mod.rs b/src/core/mod.rs index c233f47..0b23564 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + /// A collection of programming language definitions. /// /// `LanguageDefinitions` is used to manage a list of programming language definitions, which consist @@ -25,6 +27,54 @@ pub struct LanguageDefinitions { } impl LanguageDefinitions { + /// Identifies the programming language of a file based on its extension. + /// + /// The `identify_file` method takes a `PathBuf` representing the file and searches + /// the collection of language definitions for a matching file extension. If a match + /// is found, it returns the corresponding `LanguageDefinition`. If no match is found, + /// it returns `None`. + /// + /// # Parameters + /// + /// - `file`: The path to the file whose language is to be identified. + /// + /// # Returns + /// + /// An `Option` representing the identified language if found, + /// or `None` if no matching language is found. + /// + /// # Examples + /// + /// ``` + /// use std::path::PathBuf; + /// use smolguess::core::{LanguageDefinition, LanguageDefinitions}; + /// + /// let languages = LanguageDefinitions::default() + /// .insert(("Rust", "rs")) + /// .insert(("Markdown", "md")); + /// + /// let file_path = PathBuf::from("example.rs"); + /// let identified_language = languages.identify_file(file_path); + /// + /// match identified_language { + /// Some(language) => assert_eq!(language.name, "Rust"), + /// None => panic!("Language not identified."), + /// } + /// ``` + pub fn identify_file(&self, file: PathBuf) -> Option { + self.items + .iter() + .find(|lang| { + lang.extension.eq(&file + .extension() + .unwrap_or_else(|| file.file_name().expect("Unable to get file name")) + .to_string_lossy() + .to_string() + .to_lowercase()) + }) + .cloned() + } + /// Insert a language definition into the collection. /// /// This method takes a generic parameter `T` that can be converted into a `LanguageDefinition`. @@ -174,7 +224,7 @@ impl LanguageDefinitions { /// /// assert_eq!((definition.name.as_str(), definition.extension.as_str()), ("Rust", "rs")); /// ``` -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct LanguageDefinition { pub name: String, pub extension: String, diff --git a/src/main.rs b/src/main.rs index b4a601c..ad51318 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ use std::{env, path::PathBuf}; -use smolguess::{core::LanguageDefinitions, repository}; +use smolguess::{ + core::{LanguageDefinition, LanguageDefinitions}, + repository, +}; fn main() { let args: Vec = env::args().collect(); @@ -12,10 +15,21 @@ fn main() { let files = repository::get_bare_repository_files(PathBuf::from(&args[1])); println!("{:?}", files); + let definitions = LanguageDefinitions::default().load_builtins(); - LanguageDefinitions::default() - .load_builtins() - .items - .iter() - .for_each(|def| println!("{} has the extension {}", def.name, def.extension)); + for file in files { + println!( + "{:?}", + definitions.identify_file(file.clone()).unwrap_or_else(|| { + let extension = file + .extension() + .unwrap_or(file.file_name().unwrap_or_default()) + .to_string_lossy() + .to_string() + .to_lowercase(); + + LanguageDefinition::new("Unknown", &extension) + }) + ); + } } -- libgit2 1.7.2