From 9ea146aabfc6425da5dea0a105c4032e06ceb398 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 9 Jul 2024 17:36:45 +0200 Subject: [PATCH] refactor(cli): apply some clippy lints and add constants for VerbosityLevel --- members/libpt-cli/src/args.rs | 94 ++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 6 deletions(-) diff --git a/members/libpt-cli/src/args.rs b/members/libpt-cli/src/args.rs index e4bc491..31ae1b7 100644 --- a/members/libpt-cli/src/args.rs +++ b/members/libpt-cli/src/args.rs @@ -46,14 +46,14 @@ use log; /// Author: Christoph J. Scherr /// /// ``` -pub const HELP_TEMPLATE: &str = r#"{about-section} +pub const HELP_TEMPLATE: &str = r"{about-section} {usage-heading} {usage} {all-args}{tab} {name}: {version} Author: {author-with-newline} -"#; +"; /// Transform -v and -q flags to some kind of loglevel /// @@ -111,7 +111,7 @@ pub struct VerbosityLevel { // help = L::verbose_help(), // long_help = L::verbose_long_help(), )] - verbose: u8, + verbose: i8, /// make the output less verbose /// @@ -123,22 +123,37 @@ pub struct VerbosityLevel { global = true, conflicts_with = "verbose", )] - quiet: u8, + quiet: i8, } impl VerbosityLevel { /// true only if no verbose and no quiet was set (user is using defaults) #[inline] + #[must_use] + #[allow(clippy::missing_const_for_fn)] // the values of self can change pub fn changed(&self) -> bool { self.verbose != 0 || self.quiet != 0 } #[inline] fn value(&self) -> i8 { - Self::level_value(Level::INFO) - (self.quiet as i8).min(10) + (self.verbose as i8).min(10) + Self::level_value(Level::INFO) + .saturating_sub((self.quiet).min(10)) + .saturating_add((self.verbose).min(10)) } - /// get the [Level] for that VerbosityLevel + /// get the [Level] for that [`VerbosityLevel`] + /// + /// # Examples + /// + /// ``` + /// use libpt_log::Level; // reexport: tracing + /// use libpt_cli::args::VerbosityLevel; + /// + /// let verbosity_level = VerbosityLevel::INFO; + /// assert_eq!(verbosity_level.level(), Level::INFO); + /// ``` #[inline] + #[must_use] pub fn level(&self) -> Level { let v = self.value(); match v { @@ -164,6 +179,7 @@ impl VerbosityLevel { /// /// [None] means that absolutely no output is wanted (completely quiet) #[inline] + #[must_use] pub fn level_for_log_crate(&self) -> log::Level { match self.level() { Level::TRACE => log::Level::Trace, @@ -184,6 +200,72 @@ impl VerbosityLevel { Level::ERROR => 0, } } + + /// # Examples + /// + /// ``` + /// use libpt_log::Level; // reexport: tracing + /// use libpt_cli::args::VerbosityLevel; + /// + /// let verbosity_level = VerbosityLevel::TRACE; + /// assert_eq!(verbosity_level.level(), Level::TRACE); + /// ``` + pub const TRACE: Self = Self { + verbose: 2, + quiet: 0, + }; + /// # Examples + /// + /// ``` + /// use libpt_log::Level; // reexport: tracing + /// use libpt_cli::args::VerbosityLevel; + /// + /// let verbosity_level = VerbosityLevel::DEBUG; + /// assert_eq!(verbosity_level.level(), Level::DEBUG); + /// ``` + pub const DEBUG: Self = Self { + verbose: 1, + quiet: 0, + }; + /// # Examples + /// + /// ``` + /// use libpt_log::Level; // reexport: tracing + /// use libpt_cli::args::VerbosityLevel; + /// + /// let verbosity_level = VerbosityLevel::INFO; + /// assert_eq!(verbosity_level.level(), Level::INFO); + /// ``` + pub const INFO: Self = Self { + verbose: 0, + quiet: 0, + }; + /// # Examples + /// + /// ``` + /// use libpt_log::Level; // reexport: tracing + /// use libpt_cli::args::VerbosityLevel; + /// + /// let verbosity_level = VerbosityLevel::WARN; + /// assert_eq!(verbosity_level.level(), Level::WARN); + /// ``` + pub const WARN: Self = Self { + verbose: 0, + quiet: 1, + }; + /// # Examples + /// + /// ``` + /// use libpt_log::Level; // reexport: tracing + /// use libpt_cli::args::VerbosityLevel; + /// + /// let verbosity_level = VerbosityLevel::ERROR; + /// assert_eq!(verbosity_level.level(), Level::ERROR); + /// ``` + pub const ERROR: Self = Self { + verbose: 0, + quiet: 2, + }; } impl std::fmt::Debug for VerbosityLevel {