Skip to content

Commit 98c450f

Browse files
committed
refactor(filter): Pull out directive mod
1 parent c769e03 commit 98c450f

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

crates/env_filter/src/directive.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use log::Level;
2+
use log::LevelFilter;
3+
4+
#[derive(Debug)]
5+
pub(crate) struct Directive {
6+
pub(crate) name: Option<String>,
7+
pub(crate) level: LevelFilter,
8+
}
9+
10+
// Check whether a level and target are enabled by the set of directives.
11+
pub(crate) fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
12+
// Search for the longest match, the vector is assumed to be pre-sorted.
13+
for directive in directives.iter().rev() {
14+
match directive.name {
15+
Some(ref name) if !target.starts_with(&**name) => {}
16+
Some(..) | None => return level <= directive.level,
17+
}
18+
}
19+
false
20+
}

crates/env_filter/src/lib.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@
5050
//! }
5151
//! ```
5252
53+
mod directive;
5354
mod op;
5455
mod parser;
5556

5657
use std::env;
5758
use std::fmt;
5859
use std::mem;
5960

60-
use log::{Level, LevelFilter, Metadata, Record};
61+
use log::{LevelFilter, Metadata, Record};
6162

63+
use directive::enabled;
64+
use directive::Directive;
6265
use op::FilterOp;
6366
use parser::parse_spec;
6467

@@ -210,12 +213,6 @@ impl fmt::Debug for Builder {
210213
}
211214
}
212215

213-
#[derive(Debug)]
214-
struct Directive {
215-
name: Option<String>,
216-
level: LevelFilter,
217-
}
218-
219216
/// A log filter.
220217
///
221218
/// This struct can be used to determine whether or not a log record
@@ -286,18 +283,6 @@ impl fmt::Debug for Filter {
286283
}
287284
}
288285

289-
// Check whether a level and target are enabled by the set of directives.
290-
fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
291-
// Search for the longest match, the vector is assumed to be pre-sorted.
292-
for directive in directives.iter().rev() {
293-
match directive.name {
294-
Some(ref name) if !target.starts_with(&**name) => {}
295-
Some(..) | None => return level <= directive.level,
296-
}
297-
}
298-
false
299-
}
300-
301286
#[cfg(test)]
302287
mod tests {
303288
use log::{Level, LevelFilter};

0 commit comments

Comments
 (0)