Skip to content

Commit

Permalink
Add support for passing flags as strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sirosen committed Nov 4, 2024
1 parent 3819d34 commit c81c7d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ struct RegexPy {
#[pymethods]
impl RegexPy {
#[new]
fn init(value: &str) -> PyResult<Self> {
match Regex::new(value) {
Ok(inner) => Ok(RegexPy { inner }),
Err(e) => Err(RegressError::new_err(e.to_string())),
#[pyo3(signature = (value, flags=None))]
fn init(value: &str, flags: Option<&str>) -> PyResult<Self> {
match flags {
Some(f) => match Regex::with_flags(value, f) {
Ok(inner) => Ok(RegexPy { inner }),
Err(e) => Err(RegressError::new_err(e.to_string())),
},
None => match Regex::new(value) {
Ok(inner) => Ok(RegexPy { inner }),
Err(e) => Err(RegressError::new_err(e.to_string())),
},
}
}

Expand Down
14 changes: 13 additions & 1 deletion tests/test_regress.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

import regress


Expand Down Expand Up @@ -60,3 +59,16 @@ def test_error_handling():
pass
else:
pytest.fail("error not reached")


def test_with_flags():
# "L" for letters, "Z" for spaces, "N" for numerics
pattern = r"^\p{L}\p{Z}\p{N}$"

regex = regress.Regex(pattern, flags="u")
flagless_regex = regress.Regex(pattern)

match = regex.find("a 0")
flagless_match = flagless_regex.find("a 0")
assert match is not None
assert flagless_match is None

0 comments on commit c81c7d1

Please sign in to comment.