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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
@@ -1,4 +1,3 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any of these changes should be in this PR. Whether it should be ignored or not is for a different discussion.

# Created by https://www.gitignore.io/api/rust,macos,visualstudiocode

### macOS ###
Expand Down Expand Up @@ -43,10 +42,9 @@ Cargo.lock

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json


# End of https://www.gitignore.io/api/rust,macos,visualstudiocode
4 changes: 2 additions & 2 deletions tracing-subscriber/Cargo.toml
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"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, you're missing a space after the comma after version.

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
@@ -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 = regex_automata::dfa::dense::BuildError;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have the type imported so there is no need to fully name it.

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
@@ -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
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