From 4f5b20f8156704947e0cb3946080554a2a229633 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 13 Nov 2024 16:54:37 -0800 Subject: [PATCH] Implement conversions between Option and LevelFilter 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 or Into> 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 https://github.com/clap-rs/clap-verbosity-flag/issues/121 --- src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 6b43a9ae1..5f36e9606 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -635,6 +635,21 @@ impl FromStr for LevelFilter { } } +impl From for Option { + fn from(level: LevelFilter) -> Option { + level.to_level() + } +} + +impl From> for LevelFilter { + fn from(level: Option) -> 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())