From 15b2074418dd20f2af5bf756d61aea74aa9e7c06 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Wed, 26 Jul 2023 23:51:44 +0000 Subject: [PATCH] TEMP Build analyzer --- Cargo.lock | 1 + gengo/Cargo.toml | 1 + gengo/src/languages/analyzer.rs | 85 ++++++++++++++++++++++++++++++--- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9cac838b..0b911538 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -245,6 +245,7 @@ dependencies = [ name = "gengo" version = "0.1.0" dependencies = [ + "regex", "serde", "serde_yaml", "tera", diff --git a/gengo/Cargo.toml b/gengo/Cargo.toml index 95e55d62..8a338e8e 100644 --- a/gengo/Cargo.toml +++ b/gengo/Cargo.toml @@ -6,6 +6,7 @@ repository = "https://github.com/spenserblack/gengo" readme = "../README.md" [dependencies] +regex = "1" [build-dependencies] serde = { version = "1.0", features = ["derive"] } diff --git a/gengo/src/languages/analyzer.rs b/gengo/src/languages/analyzer.rs index 810f6f1e..cdc1f184 100644 --- a/gengo/src/languages/analyzer.rs +++ b/gengo/src/languages/analyzer.rs @@ -1,8 +1,28 @@ /// Analyzes a language. use super::{Category, Language}; +use std::collections::HashSet; +use regex::Regex; +use std::path::Path; +use std::ffi::{OsStr, OsString}; pub struct Analyzers(Vec); -struct Analyzer {} + +impl Default for Analyzers { + /// Create a new language analyzer with default values. + fn default() -> Self { + Self(include!(concat!(env!("OUT_DIR"), "/analyzer.rs"))) + } +} +pub struct Analyzer { + language: Language, + category: Category, + color: String, + extensions: HashSet, + filenames: HashSet, + patterns: Vec, + heuristics: Vec, + priority: f32, +} impl Analyzer { /// Create a new language analyzer. @@ -16,13 +36,66 @@ impl Analyzer { heuristics: &[&str], priority: f32, ) -> Self { - todo!() + let extensions = extensions.iter().map(|s| s.into()).collect(); + let filenames = filenames.iter().map(|s| s.into()).collect(); + // TODO Handle regex compile failures. + let patterns = patterns.iter().map(|s| Regex::new(s).unwrap()).collect(); + let heuristics = heuristics.iter().map(|s| Regex::new(s).unwrap()).collect(); + Self { + language, + category, + color: color.to_string(), + extensions, + filenames, + patterns, + heuristics, + priority, + } + } + + pub fn matches_extension(&self, filename: &str) -> bool { + let extension = Path::new(filename).extension().unwrap_or_default(); + self.extensions.contains(extension) + } + + pub fn matches_filename(&self, filename: &str) -> bool { + self.filenames.contains(Path::new(filename).file_name().unwrap_or_default()) } } -impl Default for Analyzers { - /// Create a new language analyzer with default values. - fn default() -> Self { - Self(include!(concat!(env!("OUT_DIR"), "/analyzer.rs"))) +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_matches_extension() { + let analyzer = Analyzer::new( + Language::PlainText, + Category::Prose, + "#000000", + &["txt"], + &[], + &[], + &[], + 0.5, + ); + assert!(analyzer.matches_extension("foo.txt")); + assert!(!analyzer.matches_extension("foo.rs")); + } + + #[test] + fn test_matches_filename() { + let analyzer = Analyzer::new( + Language::PlainText, + Category::Prose, + "#000000", + &[], + &["LICENSE"], + &[], + &[], + 0.5, + ); + assert!(analyzer.matches_filename("LICENSE")); + assert!(!analyzer.matches_filename("Dockerfile")); } }