index : smol-guess.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2024-01-07 19:53:20.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2024-01-07 19:53:20.0 +00:00:00
commit
3aaae291a4bf6efb003833eeebbc8ee2c0a6474e [patch]
tree
ae4456e4b7f42f633b5e820cd7857e6d79c0fbf5
parent
ac9edd48cff10b84c3e45ecc6bfc069921e24b95
download
3aaae291a4bf6efb003833eeebbc8ee2c0a6474e.tar.gz

feat: add identifying repository files



Diff

 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<LanguageDefinition>` 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<LanguageDefinition> {
        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<String> = 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)
            })
        );
    }
}