Skip to content

Commit

Permalink
Merge pull request #96 from rust-lang-nursery/optional
Browse files Browse the repository at this point in the history
Make regex an optional dependency
  • Loading branch information
sfackler authored Sep 6, 2016
2 parents d040d32 + 9bba3d5 commit 4677eee
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ script:
- cargo test --verbose
- ([ $TRAVIS_RUST_VERSION != nightly ] || cargo test --verbose --no-default-features)
- cargo test --verbose --manifest-path env/Cargo.toml
- cargo test --verbose --manifest-path env/Cargo.toml --no-default-features
- cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml
- cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml --release
- ([ $TRAVIS_RUST_VERSION != nightly ] || cargo doc --no-deps --features nightly)
Expand Down
10 changes: 5 additions & 5 deletions env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ An logging implementation for `log` which is configured via an environment
variable.
"""

[dependencies.log]
version = "0.3"
path = ".."

[dependencies]
regex = "0.1"
log = { version = "0.3", path = ".." }
regex = { version = "0.1", optional = true }

[[test]]
name = "regexp_filter"
harness = false

[features]
default = ["regex"]
18 changes: 12 additions & 6 deletions env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,27 @@
html_root_url = "http://doc.rust-lang.org/env_logger/")]
#![cfg_attr(test, deny(warnings))]

extern crate regex;
extern crate log;

use regex::Regex;
use std::env;
use std::io::prelude::*;
use std::io;
use std::mem;

use log::{Log, LogLevel, LogLevelFilter, LogRecord, SetLoggerError, LogMetadata};

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

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

/// The logger.
pub struct Logger {
directives: Vec<LogDirective>,
filter: Option<Regex>,
filter: Option<filter::Filter>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
}

Expand Down Expand Up @@ -183,7 +189,7 @@ pub struct Logger {
/// ```
pub struct LogBuilder {
directives: Vec<LogDirective>,
filter: Option<Regex>,
filter: Option<filter::Filter>,
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
}

Expand Down Expand Up @@ -352,7 +358,7 @@ pub fn init() -> Result<(), SetLoggerError> {

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

let mut parts = spec.split('/');
Expand Down Expand Up @@ -399,7 +405,7 @@ fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<Regex>) {
}});

let filter = filter.map_or(None, |filter| {
match Regex::new(filter) {
match filter::Filter::new(filter) {
Ok(re) => Some(re),
Err(e) => {
println!("warning: invalid regex filter - {}", e);
Expand Down
28 changes: 28 additions & 0 deletions env/src/regex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern crate regex;

use std::fmt;

use self::regex::Regex;

pub struct Filter {
inner: Regex,
}

impl Filter {
pub fn new(spec: &str) -> Result<Filter, String> {
match Regex::new(spec){
Ok(r) => Ok(Filter { inner: r }),
Err(e) => Err(e.to_string()),
}
}

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

impl fmt::Display for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
21 changes: 21 additions & 0 deletions env/src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::fmt;

pub struct Filter {
inner: String,
}

impl Filter {
pub fn new(spec: &str) -> Result<Filter, String> {
Ok(Filter { inner: spec.to_string() })
}

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

impl fmt::Display for Filter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}

0 comments on commit 4677eee

Please sign in to comment.