Skip to content

Commit

Permalink
refactor(filter): Clean up op mod
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 19, 2024
1 parent f187782 commit 57d938c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 62 deletions.
16 changes: 5 additions & 11 deletions crates/env_filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ use std::env;
use std::fmt;
use std::mem;

#[cfg(feature = "regex")]
#[path = "regex.rs"]
mod inner;

#[cfg(not(feature = "regex"))]
#[path = "string.rs"]
mod inner;
mod op;

/// A builder for a log filter.
///
Expand All @@ -85,7 +79,7 @@ mod inner;
/// ```
pub struct Builder {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
filter: Option<op::FilterOp>,
built: bool,
}

Expand Down Expand Up @@ -226,7 +220,7 @@ struct Directive {
/// [`Builder`]: struct.Builder.html
pub struct Filter {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
filter: Option<op::FilterOp>,
}

impl Filter {
Expand Down Expand Up @@ -289,7 +283,7 @@ impl fmt::Debug for Filter {

/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo")
/// and return a vector with log directives.
fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
fn parse_spec(spec: &str) -> (Vec<Directive>, Option<op::FilterOp>) {
let mut dirs = Vec::new();

let mut parts = spec.split('/');
Expand Down Expand Up @@ -347,7 +341,7 @@ fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
}
}

let filter = filter.and_then(|filter| match inner::Filter::new(filter) {
let filter = filter.and_then(|filter| match op::FilterOp::new(filter) {
Ok(re) => Some(re),
Err(e) => {
eprintln!("warning: invalid regex filter - {}", e);
Expand Down
42 changes: 42 additions & 0 deletions crates/env_filter/src/op.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fmt;

#[derive(Debug)]
pub struct FilterOp {
#[cfg(feature = "regex")]
inner: regex::Regex,
#[cfg(not(feature = "regex"))]
inner: String,
}

#[cfg(feature = "regex")]
impl FilterOp {
pub fn new(spec: &str) -> Result<Self, String> {
match regex::Regex::new(spec) {
Ok(r) => Ok(Self { inner: r }),
Err(e) => Err(e.to_string()),
}
}

pub fn is_match(&self, s: &str) -> bool {
self.inner.is_match(s)
}
}

#[cfg(not(feature = "regex"))]
impl FilterOp {
pub fn new(spec: &str) -> Result<Self, String> {
Ok(Self {
inner: spec.to_string(),
})
}

pub fn is_match(&self, s: &str) -> bool {
s.contains(&self.inner)
}
}

impl fmt::Display for FilterOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
27 changes: 0 additions & 27 deletions crates/env_filter/src/regex.rs

This file was deleted.

24 changes: 0 additions & 24 deletions crates/env_filter/src/string.rs

This file was deleted.

0 comments on commit 57d938c

Please sign in to comment.