Skip to content

Commit 3179aca

Browse files
committed
Make modules.re methods accept an already compiled pattern
1 parent 36df225 commit 3179aca

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixes
2+
body: Make modules.re methods accept an already compiled pattern
3+
time: 2025-08-24T23:40:19.408475+02:00

crates/dbt-jinja/minijinja-contrib/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[package]
22
name = "minijinja-contrib"
33
version = "2.5.0"
4-
edition = "2021"
4+
edition = "2024"
55
license = "Apache-2.0"
66
authors = ["Armin Ronacher <[email protected]>"]
77
description = "Extra utilities for MiniJinja"
88
homepage = "https://github.com/mitsuhiko/minijinja"
99
repository = "https://github.com/mitsuhiko/minijinja"
1010
keywords = ["jinja", "jinja2", "templates"]
1111
readme = "README.md"
12-
rust-version = "1.70"
12+
rust-version = "1.85.0"
1313

1414
[package.metadata.docs.rs]
1515
rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "doc-header.html"]

crates/dbt-jinja/minijinja-contrib/src/modules/re.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,19 @@ fn get_or_compile_regex_and_text(args: &[Value]) -> Result<(Box<Regex>, &str), E
295295
}
296296

297297
// First arg: either compiled or raw pattern
298-
let pattern = args[0].to_string();
299-
let compiled = Box::new(Regex::new(&pattern).map_err(|e| {
300-
Error::new(
301-
ErrorKind::InvalidOperation,
302-
format!("Failed to compile regex: {e}"),
303-
)
304-
})?);
298+
let compiled = if let Some(object) = args[0].as_object()
299+
&& let Some(pattern) = object.downcast_ref::<Pattern>()
300+
{
301+
Box::new(pattern._compiled.clone())
302+
} else {
303+
let pattern = args[0].to_string();
304+
Box::new(Regex::new(&pattern).map_err(|e| {
305+
Error::new(
306+
ErrorKind::InvalidOperation,
307+
format!("Failed to compile regex: {e}"),
308+
)
309+
})?)
310+
};
305311

306312
// Second arg: the text to match against
307313
let text = args[1].to_string();
@@ -413,4 +419,18 @@ mod tests {
413419
.unwrap();
414420
assert!(!result.is_true());
415421
}
422+
423+
#[test]
424+
fn test_re_search() {
425+
let result = re_search(&[
426+
Value::from(".*".to_string()),
427+
Value::from("xyz".to_string()),
428+
])
429+
.unwrap();
430+
assert!(result.is_true());
431+
432+
let compiled_pattern = re_compile(&[Value::from(".*".to_string())]).unwrap();
433+
let result = re_search(&[compiled_pattern, Value::from("xyz".to_string())]).unwrap();
434+
assert!(result.is_true());
435+
}
416436
}

0 commit comments

Comments
 (0)