Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the matchers crate #2945

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ Cargo.lock
!.vscode/extensions.json


# End of https://www.gitignore.io/api/rust,macos,visualstudiocode
# End of https://www.gitignore.io/api/rust,macos,visualstudiocode
4 changes: 2 additions & 2 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rust-version = "1.63.0"
default = ["smallvec", "fmt", "ansi", "tracing-log", "std"]
alloc = ["tracing-core/alloc"]
std = ["alloc", "tracing-core/std"]
env-filter = ["matchers", "regex", "once_cell", "tracing", "std", "thread_local"]
env-filter = ["regex-automata", "regex", "once_cell", "tracing", "std", "thread_local"]
fmt = ["registry", "std"]
ansi = ["fmt", "nu-ansi-term"]
registry = ["sharded-slab", "thread_local", "std"]
Expand All @@ -42,7 +42,7 @@ tracing-core = { path = "../tracing-core", version = "0.2", default-features = f

# only required by the `env-filter` feature
tracing = { optional = true, path = "../tracing", version = "0.2", default-features = false }
matchers = { optional = true, version = "0.1.0" }
regex-automata = { optional = true, version = "0.4.6", default-features = false, features = ["syntax", "dfa-build", "dfa-search"] }
regex = { optional = true, version = "1.6.0", default-features = false, features = ["std", "unicode-case", "unicode-perl"] }
smallvec = { optional = true, version = "1.9.0" }
once_cell = { optional = true, version = "1.13.0" }
Expand Down
10 changes: 5 additions & 5 deletions tracing-subscriber/src/filter/env/field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use matchers::Pattern;
use std::{
cmp::Ordering,
error::Error,
Expand All @@ -10,7 +9,8 @@ use std::{
},
};

use super::{FieldMap, LevelFilter};
use super::{matchers::Pattern, FieldMap, LevelFilter};
use regex_automata::dfa::dense::BuildError;
use tracing_core::field::{Field, Visit};

#[derive(Debug, Eq, PartialEq, Clone)]
Expand Down Expand Up @@ -234,7 +234,7 @@ impl ValueMatch {
/// This returns an error if the string didn't contain a valid `bool`,
/// `u64`, `i64`, or `f64` literal, and couldn't be parsed as a regular
/// expression.
fn parse_regex(s: &str) -> Result<Self, matchers::Error> {
fn parse_regex(s: &str) -> Result<Self, BuildError> {
s.parse::<bool>()
.map(ValueMatch::Bool)
.or_else(|_| s.parse::<u64>().map(ValueMatch::U64))
Expand Down Expand Up @@ -279,9 +279,9 @@ impl fmt::Display for ValueMatch {
// === impl MatchPattern ===

impl FromStr for MatchPattern {
type Err = matchers::Error;
type Err = BuildError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let matcher = Pattern::new_anchored(s)?;
let matcher = Pattern::new(s)?;
Ok(Self {
matcher,
pattern: s.to_owned().into(),
Expand Down
93 changes: 93 additions & 0 deletions tracing-subscriber/src/filter/env/matchers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/// Regex Matchers on characters and byte streams. This code is inspired by the [matchers](https://github.com/hawkw/matchers) crate
/// The code was stripped down as everything was not needed and uses an updated version of regex-automata.
use regex_automata::dfa::dense::{BuildError, DFA};
use regex_automata::dfa::Automaton;
use regex_automata::util::primitives::StateID;
use regex_automata::util::start::Config;
use regex_automata::Anchored;
use std::{fmt, fmt::Write};

#[derive(Debug, Clone)]
pub(crate) struct Pattern<A = DFA<Vec<u32>>> {
automaton: A,
}

#[derive(Debug, Clone)]
pub(crate) struct Matcher<A = DFA<Vec<u32>>> {
automaton: A,
state: StateID,
}

impl Pattern {
pub(crate) fn new(pattern: &str) -> Result<Self, BuildError> {
let automaton = DFA::new(pattern)?;
Ok(Pattern { automaton })
}
}

impl<A: Automaton> Pattern<A> {
pub(crate) fn matcher(&self) -> Matcher<&'_ A> {
Matcher {
automaton: &self.automaton,
state: self
.automaton
.start_state(&Config::new().anchored(Anchored::Yes))
.unwrap(),
}
}

pub(crate) fn matches(&self, s: &impl AsRef<str>) -> bool {
self.matcher().matches(s)
}

pub(crate) fn debug_matches(&self, d: &impl fmt::Debug) -> bool {
self.matcher().debug_matches(d)
}
}

// === impl Matcher ===

impl<A> Matcher<A>
where
A: Automaton,
{
#[inline]
fn advance(&mut self, input: u8) {
self.state = unsafe { self.automaton.next_state_unchecked(self.state, input) };
}

#[inline]
pub(crate) fn is_matched(&self) -> bool {
let eoi_state = self.automaton.next_eoi_state(self.state);
self.automaton.is_match_state(eoi_state)
}

/// Returns `true` if this pattern matches the formatted output of the given
/// type implementing `fmt::Debug`.
pub(crate) fn matches(mut self, s: &impl AsRef<str>) -> bool {
for &byte in s.as_ref().as_bytes() {
self.advance(byte);
if self.automaton.is_dead_state(self.state) {
return false;
}
}
self.is_matched()
}

pub(crate) fn debug_matches(mut self, d: &impl fmt::Debug) -> bool {
write!(&mut self, "{:?}", d).expect("matcher write impl should not fail");
self.is_matched()
}
}

impl<A: Automaton> fmt::Write for Matcher<A> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for &byte in s.as_bytes() {
self.advance(byte);
if self.automaton.is_dead_state(self.state) {
break;
}
}
Ok(())
}
}
1 change: 1 addition & 0 deletions tracing-subscriber/src/filter/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use self::{builder::Builder, directive::Directive, field::BadName as BadFiel
mod builder;
mod directive;
mod field;
mod matchers;

use crate::{
filter::LevelFilter,
Expand Down