-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a
Logger
from main for handling details
- Loading branch information
Showing
3 changed files
with
85 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::output::Output; | ||
use anyhow::Result; | ||
use word_tally::{Case, Sort, WordTally}; | ||
|
||
/// `Logger` contains configuration and methods for logging verbose and debug information. | ||
pub struct Logger { | ||
pub verbose: bool, | ||
pub debug: bool, | ||
pub case: Case, | ||
pub sort: Sort, | ||
pub min_chars: Option<usize>, | ||
pub min_count: Option<usize>, | ||
} | ||
|
||
impl Logger { | ||
/// Log verbose and debug details to stderr. | ||
pub fn log_details( | ||
&self, | ||
stderr: &mut Output, | ||
word_tally: &WordTally, | ||
delimiter: &str, | ||
source: &str, | ||
) -> Result<()> { | ||
if self.verbose { | ||
self.log(stderr, "source", delimiter, source)?; | ||
self.log(stderr, "total-words", delimiter, word_tally.count())?; | ||
self.log(stderr, "unique-words", delimiter, word_tally.uniq_count())?; | ||
} | ||
|
||
if self.debug { | ||
self.log(stderr, "delimiter", delimiter, format!("{:?}", delimiter))?; | ||
self.log(stderr, "case", delimiter, self.case)?; | ||
self.log(stderr, "order", delimiter, self.sort)?; | ||
self.log( | ||
stderr, | ||
"min-chars", | ||
delimiter, | ||
self.min_chars | ||
.map_or("none".to_string(), |count| count.to_string()), | ||
)?; | ||
self.log( | ||
stderr, | ||
"min-count", | ||
delimiter, | ||
self.min_count | ||
.map_or("none".to_string(), |count| count.to_string()), | ||
)?; | ||
self.log(stderr, "verbose", delimiter, self.verbose)?; | ||
self.log(stderr, "debug", delimiter, self.debug)?; | ||
} | ||
|
||
if word_tally.count() > 0 { | ||
stderr.write_line("\n")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Log a formatted details line. | ||
fn log<T: std::fmt::Display>( | ||
&self, | ||
w: &mut Output, | ||
label: &str, | ||
delimiter: &str, | ||
value: T, | ||
) -> Result<()> { | ||
w.write_line(&format!("{label}{delimiter}{value}\n")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters