From 608512e519bdd2c08f01e9ab88cfbdfe196f9ee0 Mon Sep 17 00:00:00 2001 From: Lucas Pickering Date: Wed, 31 Jul 2024 13:12:16 -0400 Subject: [PATCH 1/2] feat: Add `regex` feature flag This hides the regex functions (`match` and `search`) behind a feature flag `regex`, which is enabled by default. This allows consumers to completely eliminate regex as a dependency if they choose. Also added some docs on feature flags. --- serde_json_path/CHANGELOG.md | 2 ++ serde_json_path/Cargo.toml | 5 +++-- serde_json_path/src/lib.rs | 8 ++++++++ .../src/parser/selector/function/registry.rs | 14 +++++++++----- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/serde_json_path/CHANGELOG.md b/serde_json_path/CHANGELOG.md index d233e8b..92b0ec4 100644 --- a/serde_json_path/CHANGELOG.md +++ b/serde_json_path/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased - **fixed**: edge case where `.` in regexes for `match` and `search` functions was matching `\r\n` properly ([#92]) +- **breaking**: added `regex` feature flag that gates regex functions `match` and `search` + - Feature is enabled by default, but if you have `default-features = false` you'll need to explicitly add it to retain access to these functions [#92]: https://github.com/hiltontj/serde_json_path/pull/92 diff --git a/serde_json_path/Cargo.toml b/serde_json_path/Cargo.toml index cd3cb71..efc966e 100644 --- a/serde_json_path/Cargo.toml +++ b/serde_json_path/Cargo.toml @@ -11,7 +11,8 @@ readme = "README.md" keywords = ["json", "jsonpath", "json_path", "serde", "serde_json"] [features] -default = ["functions"] +default = ["functions", "regex"] +regex = ["dep:regex"] trace = ["dep:tracing", "serde_json_path_core/trace"] functions = ["serde_json_path_core/functions"] @@ -19,7 +20,7 @@ functions = ["serde_json_path_core/functions"] inventory = { version = "0.3.4" } nom = "7.1.3" once_cell = { version = "1.17.1" } -regex = "1.7.1" +regex = { version="1.7.1", optional = true } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_json_path_core = { path = "../serde_json_path_core", version = "0.1.6" } diff --git a/serde_json_path/src/lib.rs b/serde_json_path/src/lib.rs index b4a0f98..dcad14d 100644 --- a/serde_json_path/src/lib.rs +++ b/serde_json_path/src/lib.rs @@ -311,6 +311,14 @@ //! # Ok(()) //! # } //! ``` +//! +//! ## Feature Flags +//! +//! The following feature flags are supported: +//! +//! - `tracing` - Enable internal tracing via [tracing] +//! - `functions` - Enable user-defined functions +//! - `regex` - Enable the `match` and `search` functions #![warn( clippy::all, diff --git a/serde_json_path/src/parser/selector/function/registry.rs b/serde_json_path/src/parser/selector/function/registry.rs index 1a9e88d..a5644b1 100644 --- a/serde_json_path/src/parser/selector/function/registry.rs +++ b/serde_json_path/src/parser/selector/function/registry.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use once_cell::sync::Lazy; -use regex::Regex; use serde_json::Value; use serde_json_path_core::spec::functions::{Function, LogicalType, NodesType, ValueType}; @@ -20,8 +19,11 @@ pub(crate) static REGISTRY: Lazy> = Laz let mut m = HashMap::new(); m.insert("length", &LENGTH_FUNC); m.insert("count", &COUNT_FUNC); - m.insert("match", &MATCH_FUNC); - m.insert("search", &SEARCH_FUNC); + #[cfg(feature = "regex")] + { + m.insert("match", &MATCH_FUNC); + m.insert("search", &SEARCH_FUNC); + } m.insert("value", &VALUE_FUNC); m }); @@ -50,11 +52,12 @@ fn count(nodes: NodesType) -> ValueType { nodes.len().into() } +#[cfg(feature = "regex")] #[serde_json_path_macros::register(name = "match", target = MATCH_FUNC)] fn match_func(value: ValueType, rgx: ValueType) -> LogicalType { match (value.as_value(), rgx.as_value()) { (Some(Value::String(s)), Some(Value::String(r))) => { - Regex::new(format!("(?R)^({r})$").as_str()) + regex::Regex::new(format!("(?R)^({r})$").as_str()) .map(|r| r.is_match(s)) .map(Into::into) .unwrap_or_default() @@ -63,11 +66,12 @@ fn match_func(value: ValueType, rgx: ValueType) -> LogicalType { } } +#[cfg(feature = "regex")] #[serde_json_path_macros::register(target = SEARCH_FUNC)] fn search(value: ValueType, rgx: ValueType) -> LogicalType { match (value.as_value(), rgx.as_value()) { (Some(Value::String(s)), Some(Value::String(r))) => { - Regex::new(format!("(?R)({r})").as_str()) + regex::Regex::new(format!("(?R)({r})").as_str()) .map(|r| r.is_match(s)) .map(Into::into) .unwrap_or_default() From 6e5074e62920fa0f753c64318ba32e823685d41c Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Fri, 2 Aug 2024 23:46:13 -0700 Subject: [PATCH 2/2] docs: fix tracing crate link in feature flags description --- serde_json_path/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serde_json_path/src/lib.rs b/serde_json_path/src/lib.rs index dcad14d..2f9e05c 100644 --- a/serde_json_path/src/lib.rs +++ b/serde_json_path/src/lib.rs @@ -316,7 +316,7 @@ //! //! The following feature flags are supported: //! -//! - `tracing` - Enable internal tracing via [tracing] +//! - `tracing` - Enable internal tracing via [tracing](https://docs.rs/tracing/latest/tracing/) //! - `functions` - Enable user-defined functions //! - `regex` - Enable the `match` and `search` functions