Skip to content

Commit

Permalink
Switch excludes to be Option like exclusives
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Sep 18, 2024
1 parent 5988e62 commit 85e2845
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
10 changes: 2 additions & 8 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ pub struct Args {
pub min_count: u64,

/// Exclude any words from a comma-delimited list.
#[arg(
short,
long,
default_value = "",
use_value_delimiter = true,
value_name = "WORDS"
)]
pub exclude: Vec<String>,
#[arg(short, long, use_value_delimiter = true, value_name = "WORDS")]
pub exclude: Option<Vec<String>>,

/// Only include words from a comma-delimited list.
#[arg(short = 'O', long, use_value_delimiter = true, value_name = "WORDS")]
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ 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 Vec<String>);
pub struct WordsExclude(pub Option<Vec<String>>);

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

Expand Down Expand Up @@ -279,13 +279,13 @@ impl WordTally {
}

// Remove any words on the `exclude` word list.
let normalized_excludes: Vec<_> = filters
.words_exclude
.0
.iter()
.map(|exclude| Self::normalize_case(exclude, case))
.collect();
tally_map.retain(|word, _| !normalized_excludes.contains(word));
if let Some(excludes) = filters.words_exclude.0 {
let normalized_excludes: Vec<_> = excludes
.iter()
.map(|exclude| Self::normalize_case(exclude, case))
.collect();
tally_map.retain(|word, _| !normalized_excludes.contains(word));
}

// Remove any words absent from the `only` word list.
if let Some(exclusives) = filters.words_only.0 {
Expand Down
7 changes: 5 additions & 2 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(excluded_words),
words_exclude: WordsExclude(Some(excluded_words)),
..Filters::default()
};
let tally = WordTally::new(input, Case::Lower, Sort::Unsorted, filters);
Expand Down Expand Up @@ -346,7 +346,10 @@ 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(excluded));
assert_eq!(
WordsExclude::from(excluded.clone()),
WordsExclude(Some(excluded))
);
}

#[test]
Expand Down

0 comments on commit 85e2845

Please sign in to comment.