Skip to content

Commit 57d938c

Browse files
committed
refactor(filter): Clean up op mod
1 parent f187782 commit 57d938c

File tree

4 files changed

+47
-62
lines changed

4 files changed

+47
-62
lines changed

crates/env_filter/src/lib.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ use std::env;
5555
use std::fmt;
5656
use std::mem;
5757

58-
#[cfg(feature = "regex")]
59-
#[path = "regex.rs"]
60-
mod inner;
61-
62-
#[cfg(not(feature = "regex"))]
63-
#[path = "string.rs"]
64-
mod inner;
58+
mod op;
6559

6660
/// A builder for a log filter.
6761
///
@@ -85,7 +79,7 @@ mod inner;
8579
/// ```
8680
pub struct Builder {
8781
directives: Vec<Directive>,
88-
filter: Option<inner::Filter>,
82+
filter: Option<op::FilterOp>,
8983
built: bool,
9084
}
9185

@@ -226,7 +220,7 @@ struct Directive {
226220
/// [`Builder`]: struct.Builder.html
227221
pub struct Filter {
228222
directives: Vec<Directive>,
229-
filter: Option<inner::Filter>,
223+
filter: Option<op::FilterOp>,
230224
}
231225

232226
impl Filter {
@@ -289,7 +283,7 @@ impl fmt::Debug for Filter {
289283

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

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

350-
let filter = filter.and_then(|filter| match inner::Filter::new(filter) {
344+
let filter = filter.and_then(|filter| match op::FilterOp::new(filter) {
351345
Ok(re) => Some(re),
352346
Err(e) => {
353347
eprintln!("warning: invalid regex filter - {}", e);

crates/env_filter/src/op.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::fmt;
2+
3+
#[derive(Debug)]
4+
pub struct FilterOp {
5+
#[cfg(feature = "regex")]
6+
inner: regex::Regex,
7+
#[cfg(not(feature = "regex"))]
8+
inner: String,
9+
}
10+
11+
#[cfg(feature = "regex")]
12+
impl FilterOp {
13+
pub fn new(spec: &str) -> Result<Self, String> {
14+
match regex::Regex::new(spec) {
15+
Ok(r) => Ok(Self { inner: r }),
16+
Err(e) => Err(e.to_string()),
17+
}
18+
}
19+
20+
pub fn is_match(&self, s: &str) -> bool {
21+
self.inner.is_match(s)
22+
}
23+
}
24+
25+
#[cfg(not(feature = "regex"))]
26+
impl FilterOp {
27+
pub fn new(spec: &str) -> Result<Self, String> {
28+
Ok(Self {
29+
inner: spec.to_string(),
30+
})
31+
}
32+
33+
pub fn is_match(&self, s: &str) -> bool {
34+
s.contains(&self.inner)
35+
}
36+
}
37+
38+
impl fmt::Display for FilterOp {
39+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40+
self.inner.fmt(f)
41+
}
42+
}

crates/env_filter/src/regex.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

crates/env_filter/src/string.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)