Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Nov 20, 2024
1 parent 0a77622 commit fed7d4a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
17 changes: 7 additions & 10 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,38 @@ use indexmap::IndexMap;
use std::collections::HashSet;
use unicode_segmentation::UnicodeSegmentation;

/// Filters for words to be included in the tally.
/// Filters for which words should be tallied.
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Filters {
/// Word chars filters for tallying.
/// Minimum characters required for a word.
pub min_chars: Option<MinChars>,

/// Word count filters for tallying.
/// Minimum count for number of times a word must appear.
pub min_count: Option<MinCount>,

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

impl Filters {
/// Removes words from the `tally_map` based on any word `Filters`.
pub fn apply(&self, tally_map: &mut IndexMap<Box<str>, usize>, case: Case) {
// Remove any words that lack the minimum count.
if let Some(MinCount(min_count)) = self.min_count {
tally_map.retain(|_, &mut count| count >= min_count);
}

// Remove any words that lack the minimum numbner of characters.
if let Some(MinChars(min_chars)) = self.min_chars {
tally_map.retain(|word, _| word.graphemes(true).count() >= min_chars);
}

// Remove any words on the `discard` list.
if let Some(ExcludeWords(words)) = &self.exclude {
let discard: HashSet<_> = words.iter().map(|word| case.apply_and_box(word)).collect();
tally_map.retain(|word, _| !discard.contains(word));
}
}
}

/// Min number of chars a word needs to be tallied.
/// Minimum number of characters a word needs to have to be tallied.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct MinChars(pub usize);

Expand All @@ -54,7 +51,7 @@ impl From<usize> for MinChars {
}
}

/// Min count a word needs to be tallied.
/// Minimum number of times a word needs to appear to be tallied.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct MinCount(pub usize);

Expand All @@ -70,7 +67,7 @@ impl From<usize> for MinCount {
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;

/// `Input` from a file or stdin input source.
/// `Input` to read from a file or stdin source.
pub enum Input {
File(PathBuf),
Stdin,
Expand All @@ -19,7 +19,7 @@ impl Input {
}
}

/// Gets the reader for the input source.
/// Gets the reader from the input source.
pub fn get_reader(&self) -> Result<Box<dyn Read>> {
match self {
Self::File(path) => {
Expand All @@ -31,7 +31,7 @@ impl Input {
}
}

/// Returns the file name of the input or `"-"` for STDIN.
/// Returns the file name of the input or `"-"` for stdin.
pub fn source(&self) -> String {
match self {
Self::File(path) => path
Expand Down
4 changes: 2 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use clap::ValueEnum;
use core::cmp::Reverse;
use core::fmt::{self, Display, Formatter};

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

/// Word case normalization.
/// Word case normalization options.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, ValueEnum)]
pub enum Case {
Original,
Expand Down
4 changes: 2 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::fs::File;
use std::io::{self, ErrorKind::BrokenPipe, LineWriter, Write};
use std::path::{Path, PathBuf};

/// `Writer` is a boxed type for dynamic dispatch of the `Write` trait.
/// `Writer` dynamic dispatches the `Write` trait.
pub type Writer = Box<dyn Write>;

/// `Output` manages writing to either a file, stdout, or stderr.
/// `Output` writes to either a file or stream like stdout or stderr.
pub struct Output {
writer: Writer,
}
Expand Down
2 changes: 1 addition & 1 deletion src/verbose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Verbose {
Ok(())
}

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

0 comments on commit fed7d4a

Please sign in to comment.