Skip to content

Commit

Permalink
Add excluded words to verbose output
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Nov 6, 2024
1 parent 81d2044 commit 2856e82
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl From<usize> for MinCount {
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct ExcludeWords(pub Vec<String>);

impl Display for ExcludeWords {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.join(","))
}
}

impl From<Vec<String>> for ExcludeWords {
fn from(raw: Vec<String>) -> Self {
Self(raw)
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ pub struct WordTally {
/// Ordered pairs of words and the count of times they appear.
tally: Box<[(Box<str>, usize)]>,

/// Word tallying options like case normalization and sort order.
options: Options,

/// Filters that limit words from being tallied.
filters: Filters,

/// The sum of all words tallied.
count: usize,

Expand All @@ -74,6 +80,8 @@ impl WordTally {
let uniq_count = tally.len();
let mut word_tally = Self {
tally,
options,
filters,
count,
uniq_count,
};
Expand All @@ -97,6 +105,16 @@ impl WordTally {
self.tally
}

/// Gets the `options` field.
pub const fn options(&self) -> Options {
self.options
}

/// Gets the `filters` field.
pub fn filters(&self) -> Filters {
self.filters.clone()
}

/// Gets the `uniq_count` field.
pub const fn uniq_count(&self) -> usize {
self.uniq_count
Expand Down
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ fn main() -> Result<()> {
let word_tally = WordTally::new(reader, options, filters);

if args.verbose {
let verbose = Verbose {
case: args.case,
sort: args.sort,
min_chars: args.min_chars,
min_count: args.min_count,
};
let verbose = Verbose {};

let mut stderr_output = Output::stderr();
verbose.log(
Expand Down
6 changes: 3 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use core::cmp::Reverse;
use core::fmt::{self, Display, Formatter};

// Tallying options.
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
pub struct Options {
pub case: Case,
pub sort: Sort,
}

/// Word case normalization.
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, ValueEnum)]
pub enum Case {
Original,
Upper,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Display for Case {
}

/// Sort order by count.
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, ValueEnum)]
pub enum Sort {
#[default]
Desc,
Expand Down
38 changes: 25 additions & 13 deletions src/verbose.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use crate::output::Output;
use anyhow::Result;
use word_tally::{Case, Sort, WordTally};
use word_tally::WordTally;

/// `Verbose` contains some config details to be logged.
pub struct Verbose {
pub case: Case,
pub sort: Sort,
pub min_chars: Option<usize>,
pub min_count: Option<usize>,
}
pub struct Verbose;

impl Verbose {
/// Log word tally details to stderr.
Expand All @@ -23,10 +17,28 @@ impl Verbose {
self.write_entry(stderr, "total-words", delimiter, word_tally.count())?;
self.write_entry(stderr, "unique-words", delimiter, word_tally.uniq_count())?;
self.write_entry(stderr, "delimiter", delimiter, format!("{:?}", delimiter))?;
self.write_entry(stderr, "case", delimiter, self.case)?;
self.write_entry(stderr, "order", delimiter, self.sort)?;
self.write_entry(stderr, "min-chars", delimiter, self.format(self.min_chars))?;
self.write_entry(stderr, "min-count", delimiter, self.format(self.min_count))?;
self.write_entry(stderr, "case", delimiter, word_tally.options().case)?;
self.write_entry(stderr, "order", delimiter, word_tally.options().sort)?;

let filters = word_tally.filters();
self.write_entry(
stderr,
"min-chars",
delimiter,
self.format(filters.min_chars),
)?;
self.write_entry(
stderr,
"min-count",
delimiter,
self.format(filters.min_count),
)?;
self.write_entry(
stderr,
"exclude-words",
delimiter,
self.format(filters.exclude),
)?;

if word_tally.count() > 0 {
stderr.write_line("\n")?;
Expand All @@ -36,7 +48,7 @@ impl Verbose {
}

/// Format `"none"` or a `usize` as a `String`.
fn format(&self, value: Option<usize>) -> String {
fn format<T: ToString>(&self, value: Option<T>) -> String {
value.map_or_else(|| "none".to_string(), |v| v.to_string())
}

Expand Down
20 changes: 16 additions & 4 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn verbose_without_input() {
let assert = word_tally().arg("-v").assert();
assert
.success()
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count none\n")
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count none\nexclude-words none\n")
.stdout("");
}

Expand All @@ -33,7 +33,7 @@ fn verbose_with_min_chars() {
let assert = word_tally().arg("-v").arg("--min-chars=42").assert();
assert
.success()
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars 42\nmin-count none\n")
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars 42\nmin-count none\nexclude-words none\n")
.stdout("");
}

Expand All @@ -42,7 +42,19 @@ fn verbose_with_min_count() {
let assert = word_tally().arg("-v").arg("--min-count=42").assert();
assert
.success()
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count 42\n")
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count 42\nexclude-words none\n")
.stdout("");
}

#[test]
fn verbose_with_exclude() {
let assert = word_tally()
.arg("-v")
.arg("--exclude=wombat,trees")
.assert();
assert
.success()
.stderr("source -\ntotal-words 0\nunique-words 0\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count none\nexclude-words wombat,trees\n")
.stdout("");
}

Expand All @@ -51,7 +63,7 @@ fn verbose_with_input() {
let assert = word_tally().write_stdin("wombat").arg("-v").assert();
assert
.success()
.stderr("source -\ntotal-words 1\nunique-words 1\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count none\n\n")
.stderr("source -\ntotal-words 1\nunique-words 1\ndelimiter \" \"\ncase lower\norder desc\nmin-chars none\nmin-count none\nexclude-words none\n\n")
.stdout("wombat 1\n");
}

Expand Down

0 comments on commit 2856e82

Please sign in to comment.