Skip to content

Commit

Permalink
Add constructor functions and pass by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Nov 21, 2024
1 parent 07d5cc0 commit f18f3ff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ pub struct Filters {
}

impl Filters {
/// Constructs `Filters`.
pub fn new(
min_chars: &Option<usize>,
min_count: &Option<usize>,
exclude: Option<Vec<String>>,
) -> Self {
Self {
min_chars: min_chars.map(MinChars),
min_count: min_count.map(MinCount),
exclude: exclude.map(ExcludeWords),
}
}

/// Removes words from the `tally_map` based on any word `Filters`.
pub fn apply(&self, tally_map: &mut IndexMap<Box<str>, usize>, case: Case) {
if let Some(MinCount(min_count)) = self.min_count {
Expand Down
2 changes: 1 addition & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Input {

impl Input {
/// Construct an `Input` from a file path or stdin.
pub fn from_args(path: String) -> Result<Self> {
pub fn from_args(path: &str) -> Result<Self> {
if path == "-" {
Ok(Self::Stdin)
} else {
Expand Down
25 changes: 7 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,29 @@ use input::Input;
use output::Output;
use unescaper::unescape;
use verbose::Verbose;
use word_tally::{ExcludeWords, Filters, MinChars, MinCount, Options, WordTally};
use word_tally::{Filters, Options, WordTally};

fn main() -> Result<()> {
let args = Args::parse();
let delimiter = unescape(&args.delimiter)?;

let input = Input::from_args(args.input)?;
let input = Input::from_args(&args.input)?;
let source = input.source();
let reader = input.get_reader(&source)?;
let mut output = Output::from_args(&args.output)?;

let options = Options {
case: args.case,
sort: args.sort,
};

let filters = Filters {
min_chars: args.min_chars.map(MinChars),
min_count: args.min_count.map(MinCount),
exclude: args.exclude.map(ExcludeWords),
};
let reader = input.get_reader(&source)?;
let options = Options::new(args.case, args.sort);
let filters = Filters::new(&args.min_chars, &args.min_count, args.exclude);

let word_tally = WordTally::new(reader, options, filters);

if args.verbose {
let mut verbose = Verbose::new(Output::stderr(), &word_tally, &delimiter, &source);
verbose.log()?;
};

let mut output = Output::from_args(args.output)?;
}

for (word, count) in word_tally.tally() {
output.write_line(&format!("{word}{delimiter}{count}\n"))?;
}

output.flush()?;

Ok(())
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ pub struct Options {
pub sort: Sort,
}

/// Construct `Options`.
impl Options {
pub const fn new(case: Case, sort: Sort) -> Self {
Self { case, sort }
}
}

/// Word case normalization options.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, ValueEnum)]
pub enum Case {
Expand Down
2 changes: 1 addition & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Output {
}

/// Creates an `Output` from optional arguments, choosing between file or stdout.
pub fn from_args(output: Option<PathBuf>) -> Result<Self> {
pub fn from_args(output: &Option<PathBuf>) -> Result<Self> {
match output.as_deref() {
Some(path) if path == Path::new("-") => Ok(Self::stdout()),
Some(path) => Self::file(path.to_path_buf()),
Expand Down

0 comments on commit f18f3ff

Please sign in to comment.