-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add log feature flag (enabled by default) - Move log specific code to `log` module - Move top level re-exports of log types to `log` module BREAKING CHANGE: The log crate is now an optional dependency, enabled by default. The `log::Level` and `log::LevelFilter` types are now re-exported from the `log` module rather than the crate root. If you were using these types directly, you will need to update your imports. ```diff -use clap_verbosity_flag::{Level, LevelFilter}; +use clap_verbosity_flag::log::{Level, LevelFilter}; ```
- Loading branch information
Showing
3 changed files
with
126 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// These re-exports of the log crate make it easy to use this crate without having to depend on the | ||
// log crate directly. See <https://github.com/clap-rs/clap-verbosity-flag/issues/54> for more | ||
// information. | ||
pub use log::{Level, LevelFilter}; | ||
|
||
use crate::VerbosityFilter; | ||
|
||
impl From<VerbosityFilter> for LevelFilter { | ||
fn from(filter: VerbosityFilter) -> Self { | ||
match filter { | ||
VerbosityFilter::Off => LevelFilter::Off, | ||
VerbosityFilter::Error => LevelFilter::Error, | ||
VerbosityFilter::Warn => LevelFilter::Warn, | ||
VerbosityFilter::Info => LevelFilter::Info, | ||
VerbosityFilter::Debug => LevelFilter::Debug, | ||
VerbosityFilter::Trace => LevelFilter::Trace, | ||
} | ||
} | ||
} | ||
|
||
impl From<LevelFilter> for VerbosityFilter { | ||
fn from(level: LevelFilter) -> Self { | ||
match level { | ||
LevelFilter::Off => Self::Off, | ||
LevelFilter::Error => Self::Error, | ||
LevelFilter::Warn => Self::Warn, | ||
LevelFilter::Info => Self::Info, | ||
LevelFilter::Debug => Self::Debug, | ||
LevelFilter::Trace => Self::Trace, | ||
} | ||
} | ||
} | ||
|
||
impl From<VerbosityFilter> for Option<Level> { | ||
fn from(filter: VerbosityFilter) -> Self { | ||
match filter { | ||
VerbosityFilter::Off => None, | ||
VerbosityFilter::Error => Some(Level::Error), | ||
VerbosityFilter::Warn => Some(Level::Warn), | ||
VerbosityFilter::Info => Some(Level::Info), | ||
VerbosityFilter::Debug => Some(Level::Debug), | ||
VerbosityFilter::Trace => Some(Level::Trace), | ||
} | ||
} | ||
} | ||
|
||
impl From<Option<Level>> for VerbosityFilter { | ||
fn from(level: Option<Level>) -> Self { | ||
match level { | ||
None => Self::Off, | ||
Some(Level::Error) => Self::Error, | ||
Some(Level::Warn) => Self::Warn, | ||
Some(Level::Info) => Self::Info, | ||
Some(Level::Debug) => Self::Debug, | ||
Some(Level::Trace) => Self::Trace, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{DebugLevel, ErrorLevel, InfoLevel, OffLevel, TraceLevel, Verbosity, WarnLevel}; | ||
|
||
#[test] | ||
fn log_level() { | ||
let v = Verbosity::<OffLevel>::default(); | ||
assert_eq!(v.log_level(), None); | ||
assert_eq!(v.log_level_filter(), LevelFilter::Off); | ||
|
||
let v = Verbosity::<ErrorLevel>::default(); | ||
assert_eq!(v.log_level(), Some(Level::Error)); | ||
assert_eq!(v.log_level_filter(), LevelFilter::Error); | ||
|
||
let v = Verbosity::<WarnLevel>::default(); | ||
assert_eq!(v.log_level(), Some(Level::Warn)); | ||
assert_eq!(v.log_level_filter(), LevelFilter::Warn); | ||
|
||
let v = Verbosity::<InfoLevel>::default(); | ||
assert_eq!(v.log_level(), Some(Level::Info)); | ||
assert_eq!(v.log_level_filter(), LevelFilter::Info); | ||
|
||
let v = Verbosity::<DebugLevel>::default(); | ||
assert_eq!(v.log_level(), Some(Level::Debug)); | ||
assert_eq!(v.log_level_filter(), LevelFilter::Debug); | ||
|
||
let v = Verbosity::<TraceLevel>::default(); | ||
assert_eq!(v.log_level(), Some(Level::Trace)); | ||
assert_eq!(v.log_level_filter(), LevelFilter::Trace); | ||
} | ||
} |