Skip to content

Commit b1d37b9

Browse files
committed
Make regex an optional dependency
1 parent dca23d6 commit b1d37b9

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ path = "src/lib.rs"
1717

1818
[dependencies]
1919
slog = "2"
20-
regex = "0.1"
20+
regex = { version = "0.1", optional = true }
2121
slog-term = "2"
2222
slog-stdlog = "2"
2323
slog-scope = "3"
@@ -28,3 +28,5 @@ log = "0.3.6"
2828
name = "regexp_filter"
2929
harness = false
3030

31+
[features]
32+
default = ["regex"]

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
html_root_url = "http://doc.rust-lang.org/env_logger/")]
7272
#![cfg_attr(test, deny(warnings))]
7373

74-
extern crate regex;
7574
#[macro_use]
7675
extern crate slog;
7776
extern crate slog_term;
@@ -80,11 +79,18 @@ extern crate slog_scope;
8079
extern crate slog_async;
8180
extern crate log;
8281

83-
use regex::Regex;
8482
use std::{env, result, sync};
8583
use std::cell::RefCell;
8684
use slog::*;
8785

86+
#[cfg(feature = "regex")]
87+
#[path = "regex.rs"]
88+
mod filter;
89+
90+
#[cfg(not(feature = "regex"))]
91+
#[path = "string.rs"]
92+
mod filter;
93+
8894
thread_local! {
8995
static TL_BUF: RefCell<String> = RefCell::new(String::new())
9096
}
@@ -93,7 +99,7 @@ thread_local! {
9399
pub struct EnvLogger<T : Drain> {
94100
drain : T,
95101
directives: Vec<LogDirective>,
96-
filter: Option<Regex>,
102+
filter: Option<filter::Filter>,
97103
}
98104

99105
/// LogBuilder acts as builder for initializing the EnvLogger.
@@ -102,7 +108,7 @@ pub struct EnvLogger<T : Drain> {
102108
pub struct LogBuilder<T : Drain> {
103109
drain : T,
104110
directives: Vec<LogDirective>,
105-
filter: Option<Regex>,
111+
filter: Option<filter::Filter>,
106112
}
107113

108114
impl<T : Drain> LogBuilder<T> {
@@ -271,7 +277,7 @@ pub fn init() -> std::result::Result<slog_scope::GlobalLoggerGuard, log::SetLogg
271277

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

277283
let mut parts = spec.split('/');
@@ -318,7 +324,7 @@ fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<Regex>) {
318324
}});
319325

320326
let filter = filter.map_or(None, |filter| {
321-
match Regex::new(filter) {
327+
match filter::Filter::new(filter) {
322328
Ok(re) => Some(re),
323329
Err(e) => {
324330
println!("warning: invalid regex filter - {}", e);

src/regex.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate regex;
2+
3+
use std::fmt;
4+
5+
use self::regex::Regex;
6+
7+
pub struct Filter {
8+
inner: Regex,
9+
}
10+
11+
impl Filter {
12+
pub fn new(spec: &str) -> Result<Filter, String> {
13+
match Regex::new(spec){
14+
Ok(r) => Ok(Filter { inner: r }),
15+
Err(e) => Err(e.to_string()),
16+
}
17+
}
18+
19+
pub fn is_match(&self, s: &str) -> bool {
20+
self.inner.is_match(s)
21+
}
22+
}
23+
24+
impl fmt::Display for Filter {
25+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26+
self.inner.fmt(f)
27+
}
28+
}

src/string.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::fmt;
2+
3+
pub struct Filter {
4+
inner: String,
5+
}
6+
7+
impl Filter {
8+
pub fn new(spec: &str) -> Result<Filter, String> {
9+
Ok(Filter { inner: spec.to_string() })
10+
}
11+
12+
pub fn is_match(&self, s: &str) -> bool {
13+
s.contains(&self.inner)
14+
}
15+
}
16+
17+
impl fmt::Display for Filter {
18+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19+
self.inner.fmt(f)
20+
}
21+
}

0 commit comments

Comments
 (0)