Skip to content

Commit

Permalink
TEMP Build analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserblack authored Jul 26, 2023
1 parent 95461b7 commit 15b2074
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gengo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ repository = "https://github.com/spenserblack/gengo"
readme = "../README.md"

[dependencies]
regex = "1"

[build-dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand Down
85 changes: 79 additions & 6 deletions gengo/src/languages/analyzer.rs
Original file line number Diff line number Diff line change
@@ -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<Analyzer>);
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<OsString>,
filenames: HashSet<OsString>,
patterns: Vec<Regex>,
heuristics: Vec<Regex>,
priority: f32,
}

impl Analyzer {
/// Create a new language analyzer.
Expand All @@ -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"));
}
}

0 comments on commit 15b2074

Please sign in to comment.