Skip to content

Commit

Permalink
Merge pull request #115 from joshka/jm/verbosity-u8
Browse files Browse the repository at this point in the history
Use u8 for internal verbosity level calculation
  • Loading branch information
epage authored Sep 26, 2024
2 parents c621a6a + 9936958 commit 41425bb
Showing 1 changed file with 114 additions and 16 deletions.
130 changes: 114 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,32 @@ impl<L: LogLevel> Verbosity<L> {
self.log_level().is_none()
}

fn verbosity(&self) -> i8 {
level_value(L::default()) - (self.quiet as i8) + (self.verbose as i8)
fn verbosity(&self) -> u8 {
let default_verbosity = level_value(L::default());
let verbosity = default_verbosity as i16 - self.quiet as i16 + self.verbose as i16;
verbosity.clamp(0, u8::MAX as i16) as u8
}
}

fn level_value(level: Option<Level>) -> i8 {
fn level_value(level: Option<Level>) -> u8 {
match level {
None => -1,
Some(Level::Error) => 0,
Some(Level::Warn) => 1,
Some(Level::Info) => 2,
Some(Level::Debug) => 3,
Some(Level::Trace) => 4,
None => 0,
Some(Level::Error) => 1,
Some(Level::Warn) => 2,
Some(Level::Info) => 3,
Some(Level::Debug) => 4,
Some(Level::Trace) => 5,
}
}

fn level_enum(verbosity: i8) -> Option<Level> {
fn level_enum(verbosity: u8) -> Option<Level> {
match verbosity {
i8::MIN..=-1 => None,
0 => Some(Level::Error),
1 => Some(Level::Warn),
2 => Some(Level::Info),
3 => Some(Level::Debug),
4..=i8::MAX => Some(Level::Trace),
0 => None,
1 => Some(Level::Error),
2 => Some(Level::Warn),
3 => Some(Level::Info),
4 => Some(Level::Debug),
5..=u8::MAX => Some(Level::Trace),
}
}

Expand Down Expand Up @@ -235,4 +237,100 @@ mod test {
use clap::CommandFactory;
Cli::command().debug_assert();
}

#[test]
fn verbosity_error_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Error), LevelFilter::Error),
(1, 0, Some(Level::Warn), LevelFilter::Warn),
(2, 0, Some(Level::Info), LevelFilter::Info),
(3, 0, Some(Level::Debug), LevelFilter::Debug),
(4, 0, Some(Level::Trace), LevelFilter::Trace),
(5, 0, Some(Level::Trace), LevelFilter::Trace),
(255, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, None, LevelFilter::Off),
(0, 2, None, LevelFilter::Off),
(0, 255, None, LevelFilter::Off),
(255, 255, Some(Level::Error), LevelFilter::Error),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<ErrorLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

#[test]
fn verbosity_warn_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Warn), LevelFilter::Warn),
(1, 0, Some(Level::Info), LevelFilter::Info),
(2, 0, Some(Level::Debug), LevelFilter::Debug),
(3, 0, Some(Level::Trace), LevelFilter::Trace),
(4, 0, Some(Level::Trace), LevelFilter::Trace),
(255, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, Some(Level::Error), LevelFilter::Error),
(0, 2, None, LevelFilter::Off),
(0, 3, None, LevelFilter::Off),
(0, 255, None, LevelFilter::Off),
(255, 255, Some(Level::Warn), LevelFilter::Warn),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<WarnLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

#[test]
fn verbosity_info_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Info), LevelFilter::Info),
(1, 0, Some(Level::Debug), LevelFilter::Debug),
(2, 0, Some(Level::Trace), LevelFilter::Trace),
(3, 0, Some(Level::Trace), LevelFilter::Trace),
(255, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, Some(Level::Warn), LevelFilter::Warn),
(0, 2, Some(Level::Error), LevelFilter::Error),
(0, 3, None, LevelFilter::Off),
(0, 4, None, LevelFilter::Off),
(0, 255, None, LevelFilter::Off),
(255, 255, Some(Level::Info), LevelFilter::Info),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<InfoLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}
}

0 comments on commit 41425bb

Please sign in to comment.