Skip to content

Commit

Permalink
Refactored readability solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Jul 22, 2024
1 parent ec56dba commit a96af81
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/other/readability.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

fn coleman_liau_index(average_letters: f64, average_sentences: f64) -> usize {
let index = (0.0588 * average_letters) - (0.296 * average_sentences) - 15.8;
index.round() as usize
Expand All @@ -9,6 +7,7 @@ fn is_sentence_end(c: char) -> bool {
c == '!' || c == '.' || c == '?'
}

#[derive(Debug)]
pub struct ColemanLiauIndex(usize);

impl From<&str> for ColemanLiauIndex {
Expand Down Expand Up @@ -37,19 +36,17 @@ impl From<&str> for ColemanLiauIndex {
}
}

impl Deref for ColemanLiauIndex {
type Target = usize;

fn deref(&self) -> &Self::Target {
&self.0
impl PartialEq<usize> for ColemanLiauIndex {
fn eq(&self, other: &usize) -> bool {
self.0 == *other
}
}

#[cfg(test)]
mod tests {
use test_case::test_case;

use crate::other::readability::ColemanLiauIndex;
use super::*;

#[test_case(
"Congratulations! Today is your day. You're off to Great Places! You're off and away!",
Expand All @@ -64,7 +61,7 @@ mod tests {
11
)]
fn should_return_correct_grade(s: &str, grade: usize) {
let result = ColemanLiauIndex::from(s);
assert_eq!(*result, grade)
let index = ColemanLiauIndex::from(s);
assert_eq!(index, grade)
}
}

0 comments on commit a96af81

Please sign in to comment.