Skip to content

Commit

Permalink
Implement Display for Case and Sort enums
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Jun 6, 2024
1 parent d2846e9 commit bf8816c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use clap::ValueEnum;
use core::cmp::Reverse;
use core::fmt;
use core::hash::{Hash, Hasher};
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read};
Expand Down Expand Up @@ -72,6 +73,18 @@ pub enum Case {
Lower,
}

impl fmt::Display for Case {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let case = match self {
Self::Lower => "lower",
Self::Upper => "upper",
Self::Original => "original",
};

f.write_str(case)
}
}

/// Sort order by count.
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
pub enum Sort {
Expand All @@ -81,6 +94,18 @@ pub enum Sort {
Unsorted,
}

impl fmt::Display for Sort {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let order = match self {
Self::Desc => "desc",
Self::Asc => "asc",
Self::Unsorted => "unsorted",
};

f.write_str(order)
}
}

/// Minimum requirements for a word to be included in the tally.
#[derive(Clone, Copy, Debug, Default)]
pub struct Minimums {
Expand Down
18 changes: 3 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap_stdin::Source;
use std::fs::File;
use std::io::{self, ErrorKind::BrokenPipe, LineWriter, StderrLock, Write};
use unescaper::unescape;
use word_tally::{Case, Minimums, Sort, WordTally};
use word_tally::{Minimums, WordTally};

/// `Writer` is a boxed type for dynamic dispatch of the `Write` trait.
type Writer = Box<dyn Write>;
Expand Down Expand Up @@ -97,22 +97,10 @@ fn log_verbose(

/// Log debug details to stderr.
fn log_debug(stderr: &mut StderrLock<'_>, args: &Args, delimiter: &str) -> Result<()> {
let case_name = match args.case {
Case::Lower => "lower",
Case::Upper => "upper",
Case::Original => "original",
};

let sort_name = match args.sort {
Sort::Asc => "asc",
Sort::Desc => "desc",
Sort::Unsorted => "unsorted",
};

let details = [
format!("delimiter{delimiter}{delimiter:#?}\n"),
format!("case{delimiter}{case_name}\n"),
format!("order{delimiter}{sort_name}\n"),
format!("case{delimiter}{}\n", args.case),
format!("order{delimiter}{}\n", args.sort),
format!("min-chars{delimiter}{}\n", args.min_chars),
format!("min-count{delimiter}{}\n", args.min_count),
format!("verbose{delimiter}{}\n", args.verbose),
Expand Down

0 comments on commit bf8816c

Please sign in to comment.