From 74aaeb0ec2ccb703dc49ee5a042c9d8c618b5219 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 29 Jun 2024 17:05:37 +0200 Subject: [PATCH] refactor(cli): verbosity level no option anymore, prevent verbosity overflows --- members/libpt-cli/src/args.rs | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/members/libpt-cli/src/args.rs b/members/libpt-cli/src/args.rs index 7ebdacd..d947a3e 100644 --- a/members/libpt-cli/src/args.rs +++ b/members/libpt-cli/src/args.rs @@ -94,9 +94,9 @@ Author: {author-with-newline} /// /// // Level might be None if the user wants no output at all. /// // for the 'tracing' level: -/// let level: Option = opts.verbose.level(); +/// let level: Level = opts.verbose.level(); /// // for the 'log' level: -/// let llevel: Option = opts.verbose.level_for_log_crate(); +/// let llevel: log::Level = opts.verbose.level_for_log_crate(); /// } /// ``` #[derive(Parser, Clone, PartialEq, Eq, Hash)] @@ -134,27 +134,20 @@ impl VerbosityLevel { } #[inline] fn value(&self) -> i8 { - let v = Self::level_value(Level::INFO) - (self.quiet as i8) + (self.verbose as i8); - if v > Self::level_value(Level::TRACE) { - Self::level_value(Level::TRACE) - } else { - v - } + Self::level_value(Level::INFO) - (self.quiet as i8).min(10) + (self.verbose as i8).min(10) } /// get the [Level] for that VerbosityLevel - /// - /// [None] means that absolutely no output is wanted (completely quiet) #[inline] - pub fn level(&self) -> Option { - Some(match self.value() { + pub fn level(&self) -> Level { + match self.value() { 0 => Level::ERROR, 1 => Level::WARN, 2 => Level::INFO, 3 => Level::DEBUG, 4 => Level::TRACE, - _ => return None, - }) + _ => Level::ERROR, + } } /// get the [log::Level] for that VerbosityLevel @@ -163,14 +156,14 @@ impl VerbosityLevel { /// /// [None] means that absolutely no output is wanted (completely quiet) #[inline] - pub fn level_for_log_crate(&self) -> Option { - self.level().map(|ll| match ll { + pub fn level_for_log_crate(&self) -> log::Level { + match self.level() { Level::TRACE => log::Level::Trace, Level::DEBUG => log::Level::Debug, Level::INFO => log::Level::Info, Level::WARN => log::Level::Warn, Level::ERROR => log::Level::Error, - }) + } } #[inline]