Skip to content

Commit

Permalink
Move the Option outside the word filters
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Sep 30, 2024
1 parent e4cce11 commit d32ba2f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ pub struct Filters {
pub min_count: Option<MinCount>,

/// List of specific words to exclude for tallying.
pub words_exclude: WordsExclude,
pub words_exclude: Option<WordsExclude>,

/// List of specific words to only include for tallying.
pub words_only: WordsOnly,
pub words_only: Option<WordsOnly>,
}

/// Min number of chars a word needs to be tallied.
Expand Down Expand Up @@ -164,21 +164,21 @@ impl From<u64> for MinCount {

/// A list of words that should not be tallied.
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct WordsExclude(pub Option<Vec<String>>);
pub struct WordsExclude(pub Vec<String>);

impl From<Vec<String>> for WordsExclude {
fn from(raw: Vec<String>) -> Self {
Self(Some(raw))
Self(raw)
}
}

/// A list of words that should only be tallied.
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct WordsOnly(pub Option<Vec<String>>);
pub struct WordsOnly(pub Vec<String>);

impl From<Vec<String>> for WordsOnly {
fn from(raw: Vec<String>) -> Self {
Self(Some(raw))
Self(raw)
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ impl WordTally {
}

// Remove any words on the `exclude` word list.
if let WordsExclude(Some(excludes)) = filters.words_exclude {
if let Some(WordsExclude(excludes)) = filters.words_exclude {
let normalized_excludes: Vec<_> = excludes
.iter()
.map(|exclude| Self::normalize_case(exclude, case))
Expand All @@ -289,7 +289,7 @@ impl WordTally {
}

// Remove any words absent from the `only` word list.
if let WordsOnly(Some(exclusives)) = filters.words_only {
if let Some(WordsOnly(exclusives)) = filters.words_only {
let normalized_exclusives: Vec<_> = exclusives
.iter()
.map(|exclusive| Self::normalize_case(exclusive, case))
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ fn main() -> Result<()> {
let filters = Filters {
min_chars: min_chars.map(MinChars),
min_count: min_count.map(MinCount),
words_exclude: WordsExclude(exclude),
words_only: WordsOnly(only),
words_exclude: exclude.map(WordsExclude),
words_only: only.map(WordsOnly),
};

let word_tally = WordTally::new(reader, case, sort, filters);
Expand Down
11 changes: 4 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn test_excluding_words() {
let input = "The tree that would grow to heaven must send its roots to hell.".as_bytes();
let excluded_words = vec!["Heaven".to_string(), "Hell".to_string()];
let filters = Filters {
words_exclude: WordsExclude(Some(excluded_words)),
words_exclude: Some(WordsExclude(excluded_words)),
..Filters::default()
};
let tally = WordTally::new(input, Case::Lower, Sort::Unsorted, filters);
Expand All @@ -309,7 +309,7 @@ fn test_only_words() {
let input = "One must still have chaos in oneself to be able to give birth to a dancing star. I tell you: you have chaos in yourselves.".as_bytes();
let only = vec!["chaos".to_string(), "star".to_string()];
let filters = Filters {
words_only: WordsOnly(Some(only)),
words_only: Some(WordsOnly(only)),
..Filters::default()
};
let tally = WordTally::new(input, Case::Lower, Sort::Desc, filters);
Expand Down Expand Up @@ -346,16 +346,13 @@ fn test_min_count_from() {
#[test]
fn test_words_exclude_from() {
let excluded = vec!["beep".to_string(), "boop".to_string()];
assert_eq!(
WordsExclude::from(excluded.clone()),
WordsExclude(Some(excluded))
);
assert_eq!(WordsExclude::from(excluded.clone()), WordsExclude(excluded));
}

#[test]
fn test_words_only_from() {
let only = vec!["bep".to_string(), "bop".to_string()];
assert_eq!(WordsOnly::from(only.clone()), WordsOnly(Some(only)));
assert_eq!(WordsOnly::from(only.clone()), WordsOnly(only));
}

#[test]
Expand Down

0 comments on commit d32ba2f

Please sign in to comment.