| 1 | //! Regex provider seam: the three regex builtins (`matches`/`replace`/`split`) |
| 2 | //! route through a [`FeelRegex`] trait so each target can use its **host's** |
| 3 | //! regex engine — and therefore its host's regex *dialect*. This is a deliberate, |
| 4 | //! scoped relaxation of the cardinal rule "same core, same result" (ADR-1), |
| 5 | //! recorded as **ADR-16**: only these three builtins are dialect-dependent; |
| 6 | //! everything else stays bit-identical across targets. |
| 7 | //! |
| 8 | //! Dialects: `feel-wasm` injects a JS (`RegExp`, ECMAScript) provider; `feel-pg` |
| 9 | //! and the default `feel-core` build use [`RustRegex`] (Rust `regex` crate); a |
| 10 | //! purist `xpath-regex` build re-bundles `RustRegex` everywhere. The Rust engine |
| 11 | //! is an optional dependency behind the default `rust-regex` feature, so a target |
| 12 | //! that brings its own engine (wasm) can drop it from the build entirely. |
| 13 | |
| 14 | use crate::Value; |
| 15 | |
| 16 | /// A host regex engine, used by the `matches`/`replace`/`split` builtins. |
| 17 | /// |
| 18 | /// Each method returns `None` when the pattern/flags are invalid *in this |
| 19 | /// dialect* — the caller maps that to a (value) FEEL `null`, since the spec maps |
| 20 | /// regex errors to null. Flag validation and replacement-string syntax (`$N` |
| 21 | /// group references) are dialect-owned and live in the implementation. |
| 22 | pub trait FeelRegex { |
| 23 | /// Whether this provider has a working engine. A provider returning `false` |
| 24 | /// makes the three regex builtins an *error-null* (`Value::Error`, |
| 25 | /// "unsupported in this build") rather than a value-null, so the strict |
| 26 | /// boundary surfaces it instead of letting it pass silently (ADR-14). Real |
| 27 | /// engines keep the default `true`; only [`NoRegex`] overrides it. |
| 28 | fn available(&self) -> bool { |
| 29 | true |
| 30 | } |
| 31 | |
| 32 | /// XPath `matches(input, pattern, flags)`: does `pattern` match anywhere in |
| 33 | /// `input`? `None` => invalid pattern/flags. |
| 34 | fn is_match(&self, input: &str, pattern: &str, flags: &str) -> Option<bool>; |
| 35 | |
| 36 | /// XPath `replace(input, pattern, replacement, flags)`: replace **all** |
| 37 | /// matches. `None` => invalid pattern/flags. |
| 38 | fn replace(&self, input: &str, pattern: &str, replacement: &str, flags: &str) |
| 39 | -> Option<String>; |
| 40 | |
| 41 | /// FEEL `split(input, delimiter)`: split on the regex `delimiter` (no flags). |
| 42 | /// `None` => invalid delimiter pattern. |
| 43 | fn split(&self, input: &str, delimiter: &str) -> Option<Vec<String>>; |
| 44 | } |
| 45 | |
| 46 | /// Apply a provider result to the FEEL `matches` builtin (`Some` → boolean, |
| 47 | /// `None` → null). Kept here so all three builtins share one mapping point. |
| 48 | pub(crate) fn match_value(result: Option<bool>) -> Value { |
| 49 | match result { |
| 50 | Some(m) => Value::Boolean(m), |
| 51 | None => Value::Null, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// The provider used by the no-argument `evaluate` / `CompiledExpr::evaluate` |
| 56 | /// path: `RustRegex` when the `rust-regex` feature is on (default), else |
| 57 | /// `NoRegex`. Callers that want a different dialect use `evaluate_with_env`. |
| 58 | #[cfg(feature = "rust-regex")] |
| 59 | pub(crate) const DEFAULT: RustRegex = RustRegex; |
| 60 | #[cfg(not(feature = "rust-regex"))] |
| 61 | pub(crate) const DEFAULT: NoRegex = NoRegex; |
| 62 | |
| 63 | // --------------------------------------------------------------------------- // |
| 64 | // Default provider when no host engine is injected. |
| 65 | // --------------------------------------------------------------------------- // |
| 66 | |
| 67 | /// The provider used when the crate is built **without** `rust-regex` and no |
| 68 | /// host engine is supplied via `evaluate_with_env`. The three regex builtins then |
| 69 | /// evaluate to an *error-null* (there is no engine to consult — see |
| 70 | /// [`FeelRegex::available`]). Real targets always inject a provider (`feel-wasm`) |
| 71 | /// or keep `rust-regex` on (`feel-pg`, default core), so this is only reached by a |
| 72 | /// bare `--no-default-features` core build. |
| 73 | #[cfg(not(feature = "rust-regex"))] |
| 74 | #[derive(Debug, Default, Clone, Copy)] |
| 75 | pub(crate) struct NoRegex; |
| 76 | |
| 77 | #[cfg(not(feature = "rust-regex"))] |
| 78 | impl FeelRegex for NoRegex { |
| 79 | fn available(&self) -> bool { |
| 80 | false |
| 81 | } |
| 82 | fn is_match(&self, _: &str, _: &str, _: &str) -> Option<bool> { |
| 83 | None |
| 84 | } |
| 85 | fn replace(&self, _: &str, _: &str, _: &str, _: &str) -> Option<String> { |
| 86 | None |
| 87 | } |
| 88 | fn split(&self, _: &str, _: &str) -> Option<Vec<String>> { |
| 89 | None |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // --------------------------------------------------------------------------- // |
| 94 | // Rust `regex` provider (default feature). Carries the dialect that was the |
| 95 | // project's original behaviour, so the default core/pg results are unchanged. |
| 96 | // --------------------------------------------------------------------------- // |
| 97 | |
| 98 | #[cfg(feature = "rust-regex")] |
| 99 | pub use rust::RustRegex; |
| 100 | |
| 101 | #[cfg(feature = "rust-regex")] |
| 102 | mod rust { |
| 103 | use super::FeelRegex; |
| 104 | use regex::Regex; |
| 105 | |
| 106 | /// The Rust `regex` engine. Its dialect differs from XQuery/XPath 2.0 F&O in |
| 107 | /// the corners (backreferences, class subtraction, XSD block names), but the |
| 108 | /// common cases the TCK exercises match — see ADR-15's `regex-xpath-dialect`. |
| 109 | #[derive(Debug, Default, Clone, Copy)] |
| 110 | pub struct RustRegex; |
| 111 | |
| 112 | impl FeelRegex for RustRegex { |
| 113 | fn is_match(&self, input: &str, pattern: &str, flags: &str) -> Option<bool> { |
| 114 | build_regex(pattern, flags).map(|re| re.is_match(input)) |
| 115 | } |
| 116 | |
| 117 | fn replace( |
| 118 | &self, |
| 119 | input: &str, |
| 120 | pattern: &str, |
| 121 | replacement: &str, |
| 122 | flags: &str, |
| 123 | ) -> Option<String> { |
| 124 | let re = build_regex(pattern, flags)?; |
| 125 | let repl = xpath_replacement_to_rust(replacement); |
| 126 | Some(re.replace_all(input, repl.as_str()).into_owned()) |
| 127 | } |
| 128 | |
| 129 | fn split(&self, input: &str, delimiter: &str) -> Option<Vec<String>> { |
| 130 | let re = Regex::new(delimiter).ok()?; |
| 131 | Some(re.split(input).map(|p| p.to_string()).collect()) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Build a `regex::Regex` from a FEEL pattern + flags. Only XPath F&O flag |
| 136 | /// letters (`i s m x q`) are accepted; `q` switches to literal (escaped) |
| 137 | /// matching. An invalid pattern/flag yields `None` (→ FEEL null). |
| 138 | fn build_regex(pattern: &str, flags: &str) -> Option<Regex> { |
| 139 | if flags.chars().any(|c| !matches!(c, 'i' | 's' | 'm' | 'x' | 'q')) { |
| 140 | return None; |
| 141 | } |
| 142 | let base = if flags.contains('q') { |
| 143 | regex::escape(pattern) |
| 144 | } else { |
| 145 | pattern.to_string() |
| 146 | }; |
| 147 | let supported: String = flags |
| 148 | .chars() |
| 149 | .filter(|c| matches!(c, 'i' | 's' | 'm' | 'x')) |
| 150 | .collect(); |
| 151 | let full = if supported.is_empty() { |
| 152 | base |
| 153 | } else { |
| 154 | format!("(?{supported}){base}") |
| 155 | }; |
| 156 | Regex::new(&full).ok() |
| 157 | } |
| 158 | |
| 159 | /// Translate a FEEL/XPath replacement string to Rust's `regex` replacement |
| 160 | /// syntax. XPath group references are strictly numeric (`$1`), so `$1c` is |
| 161 | /// "group 1 then a literal c"; rewriting each `$N` to `${N}` removes the |
| 162 | /// ambiguity Rust would otherwise read as a named group. (Part of the |
| 163 | /// `regex-xpath-dialect` deviation — see ADR-15.) |
| 164 | fn xpath_replacement_to_rust(repl: &str) -> String { |
| 165 | let re = Regex::new(r"\$([0-9]+)").unwrap(); |
| 166 | re.replace_all(repl, "$${$1}").into_owned() |
| 167 | } |
| 168 | } |