Skip to content

Commit c81c7d1

Browse files
committed
Add support for passing flags as strings
resolves #51 There is no support for `Flags` structs as provided by the `regress` crate. But a string can be passed down and is converted to Flags by the `with_flags` constructor. Exposing flags as a struct/enum adds significant complexity deemed not worth the additional code at this time. (See #51 for more details.) A python test verifies that the alternate constructor pattern works and provides support for the `"u"` flag.
1 parent 3819d34 commit c81c7d1

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/lib.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ struct RegexPy {
1212
#[pymethods]
1313
impl RegexPy {
1414
#[new]
15-
fn init(value: &str) -> PyResult<Self> {
16-
match Regex::new(value) {
17-
Ok(inner) => Ok(RegexPy { inner }),
18-
Err(e) => Err(RegressError::new_err(e.to_string())),
15+
#[pyo3(signature = (value, flags=None))]
16+
fn init(value: &str, flags: Option<&str>) -> PyResult<Self> {
17+
match flags {
18+
Some(f) => match Regex::with_flags(value, f) {
19+
Ok(inner) => Ok(RegexPy { inner }),
20+
Err(e) => Err(RegressError::new_err(e.to_string())),
21+
},
22+
None => match Regex::new(value) {
23+
Ok(inner) => Ok(RegexPy { inner }),
24+
Err(e) => Err(RegressError::new_err(e.to_string())),
25+
},
1926
}
2027
}
2128

tests/test_regress.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
import regress
43

54

@@ -60,3 +59,16 @@ def test_error_handling():
6059
pass
6160
else:
6261
pytest.fail("error not reached")
62+
63+
64+
def test_with_flags():
65+
# "L" for letters, "Z" for spaces, "N" for numerics
66+
pattern = r"^\p{L}\p{Z}\p{N}$"
67+
68+
regex = regress.Regex(pattern, flags="u")
69+
flagless_regex = regress.Regex(pattern)
70+
71+
match = regex.find("a 0")
72+
flagless_match = flagless_regex.find("a 0")
73+
assert match is not None
74+
assert flagless_match is None

0 commit comments

Comments
 (0)