From 5bf954118ea6e22cf8a97f66a05933b6705fe668 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Sun, 21 Apr 2024 07:41:17 -0400 Subject: [PATCH 1/3] Remove the matchers crate --- .gitignore | 4 +- tracing-subscriber/Cargo.toml | 4 +- tracing-subscriber/src/filter/env/field.rs | 10 +- tracing-subscriber/src/filter/env/matchers.rs | 93 +++++++++++++++++++ tracing-subscriber/src/filter/env/mod.rs | 1 + 5 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 tracing-subscriber/src/filter/env/matchers.rs diff --git a/.gitignore b/.gitignore index 724f2bfa82..992f222552 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ - # Created by https://www.gitignore.io/api/rust,macos,visualstudiocode ### macOS ### @@ -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 diff --git a/tracing-subscriber/Cargo.toml b/tracing-subscriber/Cargo.toml index 524bf4b482..1ae285e9b5 100644 --- a/tracing-subscriber/Cargo.toml +++ b/tracing-subscriber/Cargo.toml @@ -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"] @@ -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" } diff --git a/tracing-subscriber/src/filter/env/field.rs b/tracing-subscriber/src/filter/env/field.rs index 211d2bded9..023340f7a2 100644 --- a/tracing-subscriber/src/filter/env/field.rs +++ b/tracing-subscriber/src/filter/env/field.rs @@ -1,4 +1,3 @@ -use matchers::Pattern; use std::{ cmp::Ordering, error::Error, @@ -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)] @@ -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 { + fn parse_regex(s: &str) -> Result { s.parse::() .map(ValueMatch::Bool) .or_else(|_| s.parse::().map(ValueMatch::U64)) @@ -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; fn from_str(s: &str) -> Result { - let matcher = Pattern::new_anchored(s)?; + let matcher = Pattern::new(s)?; Ok(Self { matcher, pattern: s.to_owned().into(), diff --git a/tracing-subscriber/src/filter/env/matchers.rs b/tracing-subscriber/src/filter/env/matchers.rs new file mode 100644 index 0000000000..1b69dceaa9 --- /dev/null +++ b/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>> { + automaton: A, +} + +#[derive(Debug, Clone)] +pub(crate) struct Matcher>> { + automaton: A, + state: StateID, +} + +impl Pattern { + pub(crate) fn new(pattern: &str) -> Result { + let automaton = DFA::new(pattern)?; + Ok(Pattern { automaton }) + } +} + +impl Pattern { + 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) -> bool { + self.matcher().matches(s) + } + + pub(crate) fn debug_matches(&self, d: &impl fmt::Debug) -> bool { + self.matcher().debug_matches(d) + } +} + +// === impl Matcher === + +impl Matcher +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) -> 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 fmt::Write for Matcher { + 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(()) + } +} diff --git a/tracing-subscriber/src/filter/env/mod.rs b/tracing-subscriber/src/filter/env/mod.rs index 4819e7936b..6309cbf4b5 100644 --- a/tracing-subscriber/src/filter/env/mod.rs +++ b/tracing-subscriber/src/filter/env/mod.rs @@ -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, From fddc503a9eed4e9c1b77851d2ce8cf68c1ce4831 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Thu, 25 Apr 2024 07:16:06 -0400 Subject: [PATCH 2/3] Review Changes --- .gitignore | 2 +- tracing-subscriber/Cargo.toml | 2 +- tracing-subscriber/src/filter/env/field.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 992f222552..ac6a513529 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,7 @@ Cargo.lock ### VisualStudioCode ### .vscode/* -.vscode/settings.json +!.vscode/settings.json !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json diff --git a/tracing-subscriber/Cargo.toml b/tracing-subscriber/Cargo.toml index 1ae285e9b5..cd5b968c6b 100644 --- a/tracing-subscriber/Cargo.toml +++ b/tracing-subscriber/Cargo.toml @@ -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 } -regex-automata = { optional = true, version = "0.4.6",default-features = false, features = ["syntax", "dfa-build", "dfa-search"] } +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" } diff --git a/tracing-subscriber/src/filter/env/field.rs b/tracing-subscriber/src/filter/env/field.rs index 023340f7a2..a575f1929c 100644 --- a/tracing-subscriber/src/filter/env/field.rs +++ b/tracing-subscriber/src/filter/env/field.rs @@ -279,7 +279,7 @@ impl fmt::Display for ValueMatch { // === impl MatchPattern === impl FromStr for MatchPattern { - type Err = regex_automata::dfa::dense::BuildError; + type Err = BuildError; fn from_str(s: &str) -> Result { let matcher = Pattern::new(s)?; Ok(Self { From 6b6ff8877401e9952fbd020a76ca60676a30894c Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Thu, 25 Apr 2024 07:17:42 -0400 Subject: [PATCH 3/3] Revert .gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ac6a513529..90763be8e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ + # Created by https://www.gitignore.io/api/rust,macos,visualstudiocode ### macOS ### @@ -47,4 +48,5 @@ Cargo.lock !.vscode/launch.json !.vscode/extensions.json -# End of https://www.gitignore.io/api/rust,macos,visualstudiocode + +# End of https://www.gitignore.io/api/rust,macos,visualstudiocode \ No newline at end of file