Skip to content

Commit

Permalink
Add length algorithm; allow for scoring algorithm selection
Browse files Browse the repository at this point in the history
What?
=====

This introduces a simple scoring algorithm to score only by
non-whitespice line length.
  • Loading branch information
joshuaclayton committed Dec 19, 2020
1 parent 1a31bd0 commit c6f6f5a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ fn calculate_complexity(flags: flags::Flags) {
builder.filter_entry(move |e| files_filter.matches(e.path()));

let results = Arc::new(Mutex::new(vec![]));
let scorer = flags.scorer;

builder.build_parallel().run(|| {
Box::new(|result| {
let mut scorer: Box<dyn scoring::ScoreVisitor> = Box::new(scoring::Standard::default());
let mut scorer = build_scorer(&scorer);
if let Some(parsed_file) = parse_dir_entry(&mut scorer, result) {
let mut results = results.lock().unwrap();
results.push(parsed_file);
Expand All @@ -60,6 +61,13 @@ fn calculate_complexity(flags: flags::Flags) {
}
}

fn build_scorer(algorithm: &flags::ScoringAlgorithm) -> Box<dyn scoring::ScoreVisitor> {
match algorithm {
flags::ScoringAlgorithm::Standard => Box::new(scoring::Standard::default()),
flags::ScoringAlgorithm::Length => Box::new(scoring::Length::default()),
}
}

fn render_standard(results: &[ParsedFile]) {
for parsed_file in results {
println!(
Expand Down
22 changes: 22 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ impl FromStr for Format {
}
}

#[derive(Debug)]
pub enum ScoringAlgorithm {
Standard,
Length,
}

impl FromStr for ScoringAlgorithm {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"standard" => Ok(ScoringAlgorithm::Standard),
"length" => Ok(ScoringAlgorithm::Length),
v => Err(format!("Unknown scoring algorithm: {}", v)),
}
}
}

#[derive(Debug, StructOpt)]
#[structopt(
name = "complexity",
Expand All @@ -51,4 +69,8 @@ pub struct Flags {
/// Format output
#[structopt(long, possible_values = &["standard", "csv", "json"], default_value = "standard", case_insensitive = true)]
pub format: Format,

/// Scoring algorithm
#[structopt(short = "s", long, possible_values = &["standard", "length"], default_value = "standard", case_insensitive = true)]
pub scorer: ScoringAlgorithm,
}
2 changes: 2 additions & 0 deletions src/scoring.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod length;
mod standard;

pub use length::Length;
pub use standard::Standard;

pub trait ScoreVisitor {
Expand Down
47 changes: 47 additions & 0 deletions src/scoring/length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::scoring::ScoreVisitor;

pub struct Length {
line_length: usize,
}

impl Default for Length {
fn default() -> Self {
Self { line_length: 0 }
}
}

impl ScoreVisitor for Length {
fn visit_line_length(&mut self, length: usize) {
self.line_length = length;
}

fn visit_first_line(&mut self, _: usize) {}

fn visit_indent(&mut self, _: usize) {}

fn visit_same(&mut self, _: usize) {}

fn visit_dedent(&mut self, _: usize) {}

fn score(&self) -> f32 {
self.line_length as f32
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::scoring::{score, ScoreVisitor};
use approx::*;

#[test]
fn length_only_uses_file_length() {
let mut scorer: Box<dyn ScoreVisitor> = Box::new(Length::default());

assert!(abs_diff_eq!(
score(&mut scorer, &vec![0, 2, 4, 6, 8, 10, 8, 6, 4, 2, 0]),
11.0,
epsilon = 0.0001
));
}
}

0 comments on commit c6f6f5a

Please sign in to comment.