Skip to content

Commit

Permalink
change a bit structure
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Oct 25, 2024
1 parent 56e26ce commit 5458e6b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/filter/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ impl From<DirectiveFilter> for Filter {
}
}

impl From<LevelFilter> for DirectiveFilter {
fn from(filter: LevelFilter) -> Self {
DirectiveFilter::new(DirectiveFilterBuilder::new().filter_level(filter))
}
}

impl<'a> From<&'a str> for DirectiveFilter {
fn from(filter: &'a str) -> Self {
DirectiveFilter::new(DirectiveFilterBuilder::new().parse(filter))
}
}

impl FromStr for DirectiveFilter {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
DirectiveFilterBuilder::new()
.try_parse(s)
.map(DirectiveFilter::new)
}
}

/// A builder for the directive log filter.
///
/// It can be used to parse a set of directives from a string before building a [DirectiveFilter]
Expand Down Expand Up @@ -203,31 +225,3 @@ impl DirectiveFilterBuilder {
self
}
}

impl From<LevelFilter> for DirectiveFilter {
fn from(filter: LevelFilter) -> Self {
DirectiveFilter::new(DirectiveFilterBuilder::new().filter_level(filter))
}
}

impl From<LevelFilter> for Filter {
fn from(filter: LevelFilter) -> Self {
Filter::Directive(filter.into())
}
}

impl<'a> From<&'a str> for Filter {
fn from(filter: &'a str) -> Self {
DirectiveFilter::new(DirectiveFilterBuilder::new().parse(filter)).into()
}
}

impl FromStr for DirectiveFilter {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
DirectiveFilterBuilder::new()
.try_parse(s)
.map(DirectiveFilter::new)
}
}
24 changes: 24 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

//! Determinate whether a log record should be processed.
use std::str::FromStr;

use log::LevelFilter;

pub use self::custom::CustomFilter;
pub use self::directive::DirectiveFilter;

Expand Down Expand Up @@ -52,3 +56,23 @@ impl Filter {
}
}
}

impl From<LevelFilter> for Filter {
fn from(filter: LevelFilter) -> Self {
Filter::Directive(filter.into())
}
}

impl<'a> From<&'a str> for Filter {
fn from(filter: &'a str) -> Self {
DirectiveFilter::from(filter).into()
}
}

impl FromStr for Filter {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
DirectiveFilter::from_str(s).map(Into::into)
}
}

0 comments on commit 5458e6b

Please sign in to comment.