-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add length algorithm; allow for scoring algorithm selection
What? ===== This introduces a simple scoring algorithm to score only by non-whitespice line length.
- Loading branch information
1 parent
1a31bd0
commit c6f6f5a
Showing
4 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)); | ||
} | ||
} |