Skip to content

Commit

Permalink
refactor(filter): Flatten the mod
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 19, 2024
1 parent 3b45c6f commit c769e03
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 10 additions & 6 deletions crates/env_filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@
//! }
//! ```

use log::{Level, LevelFilter, Metadata, Record};
mod op;
mod parser;

use std::env;
use std::fmt;
use std::mem;

mod op;
mod parser;
use log::{Level, LevelFilter, Metadata, Record};

use op::FilterOp;
use parser::parse_spec;

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

Expand Down Expand Up @@ -146,7 +150,7 @@ impl Builder {
///
/// [Enabling Logging]: ../index.html#enabling-logging
pub fn parse(&mut self, filters: &str) -> &mut Self {
let (directives, filter) = parser::parse_spec(filters);
let (directives, filter) = parse_spec(filters);

self.filter = filter;

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

impl Filter {
Expand Down
6 changes: 3 additions & 3 deletions crates/env_filter/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use log::LevelFilter;

use crate::op;
use crate::Directive;
use crate::FilterOp;

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

let mut parts = spec.split('/');
Expand Down Expand Up @@ -63,7 +63,7 @@ pub(crate) fn parse_spec(spec: &str) -> (Vec<Directive>, Option<op::FilterOp>) {
}
}

let filter = filter.and_then(|filter| match op::FilterOp::new(filter) {
let filter = filter.and_then(|filter| match FilterOp::new(filter) {
Ok(re) => Some(re),
Err(e) => {
eprintln!("warning: invalid regex filter - {}", e);
Expand Down

0 comments on commit c769e03

Please sign in to comment.