refactor(cli): change log feature and impl default for VerbosityLevel

This commit is contained in:
Christoph J. Scherr 2024-07-09 17:41:55 +02:00
parent 9ea146aabf
commit 4f15f4b639
2 changed files with 13 additions and 6 deletions

View File

@ -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"] }

View File

@ -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
}
}