From 4f15f4b63949ad7b0612620f3c2e6b58c740cbe2 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 9 Jul 2024 17:41:55 +0200 Subject: [PATCH] refactor(cli): change log feature and impl default for VerbosityLevel --- members/libpt-cli/Cargo.toml | 6 +++--- members/libpt-cli/src/args.rs | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/members/libpt-cli/Cargo.toml b/members/libpt-cli/Cargo.toml index 9ec6de5..12176b3 100644 --- a/members/libpt-cli/Cargo.toml +++ b/members/libpt-cli/Cargo.toml @@ -16,8 +16,8 @@ categories.workspace = true cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"] [features] -default = ["log"] -log = ["dep:libpt-log", "dep:log"] +default = [] +log = ["dep:log"] [dependencies] anyhow.workspace = true @@ -29,7 +29,7 @@ embed-doc-image = "0.1.4" exitcode = "1.1.2" human-panic = "2.0.0" indicatif = "0.17.8" -libpt-log = { workspace = true, optional = true } +libpt-log = { workspace = true, optional = false } log = { version = "0.4.21", optional = true } shlex = "1.3.0" strum = { version = "0.26.3", features = ["derive"] } diff --git a/members/libpt-cli/src/args.rs b/members/libpt-cli/src/args.rs index 31ae1b7..ba8a57d 100644 --- a/members/libpt-cli/src/args.rs +++ b/members/libpt-cli/src/args.rs @@ -1,7 +1,6 @@ //! Utilities for parsing options and arguments on the start of a CLI application use clap::Parser; -#[cfg(feature = "log")] use libpt_log::Level; #[cfg(feature = "log")] use log; @@ -100,7 +99,6 @@ Author: {author-with-newline} /// } /// ``` #[derive(Parser, Clone, PartialEq, Eq, Hash)] -#[cfg(feature = "log")] pub struct VerbosityLevel { /// make the output more verbose #[arg( @@ -135,6 +133,7 @@ impl VerbosityLevel { self.verbose != 0 || self.quiet != 0 } #[inline] + #[must_use] fn value(&self) -> i8 { Self::level_value(Level::INFO) .saturating_sub((self.quiet).min(10)) @@ -180,6 +179,7 @@ impl VerbosityLevel { /// [None] means that absolutely no output is wanted (completely quiet) #[inline] #[must_use] + #[cfg(feature = "log")] pub fn level_for_log_crate(&self) -> log::Level { match self.level() { Level::TRACE => log::Level::Trace, @@ -191,7 +191,8 @@ impl VerbosityLevel { } #[inline] - fn level_value(level: Level) -> i8 { + #[must_use] + const fn level_value(level: Level) -> i8 { match level { Level::TRACE => 4, Level::DEBUG => 3, @@ -274,3 +275,9 @@ impl std::fmt::Debug for VerbosityLevel { write!(f, "{:?}", self.level()) } } + +impl Default for VerbosityLevel { + fn default() -> Self { + Self::INFO + } +}