Skip to content

Commit

Permalink
Implement conversions between Option<Level> and LevelFilter
Browse files Browse the repository at this point in the history
This is nice to have when working with the `log` crate, as it allows
for more ergonomic conversions between the two types. Specifically, This
allows type bounds that require Into<LevelFilter> or Into<Option<Level>>
to be used. This in turn makes it possible to write more generic code
that can work with tracing and log crates interchangeably.

Specifically, this supports some ideas in
clap-rs/clap-verbosity-flag#121
  • Loading branch information
joshka committed Nov 14, 2024
1 parent 70fead8 commit 4f5b20f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,21 @@ impl FromStr for LevelFilter {
}
}

impl From<LevelFilter> for Option<Level> {
fn from(level: LevelFilter) -> Option<Level> {
level.to_level()
}
}

impl From<Option<Level>> for LevelFilter {
fn from(level: Option<Level>) -> LevelFilter {
match level {
Some(level) => level.to_level_filter(),
None => LevelFilter::Off,
}
}
}

impl fmt::Display for LevelFilter {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.pad(self.as_str())
Expand Down

0 comments on commit 4f5b20f

Please sign in to comment.