Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion cql2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ class Expr:
bool: True if the expression matches the item, False otherwise
"""

def reduce(self, item: dict[str, Any] | None = None) -> Expr:
"""Reduces this expression against an item.

Args:
item (dict[str, Any] | None): The item to reduce against

Returns:
Expr: The reduced expression

Examples:
>>> from cql2 import Expr
>>> expr = Expr("true AND true").reduce()
>>> expr.to_text()
'true'
"""

def to_json(self) -> dict[str, Any]:
"""Converts this cql2 expression to a cql2-json dictionary.

Expand Down Expand Up @@ -128,7 +144,7 @@ class Expr:
['LC82030282019133LGN00']
"""

def __add__(self, other: "Expr") -> "Expr":
def __add__(self, other: Expr) -> Expr:
"""Combines two cql2 expressions using the AND operator.

Args:
Expand Down
10 changes: 10 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ impl Expr {
self.0.clone().matches(Some(&value)).map_err(Error::from)
}

#[pyo3(signature = (item=None))]
fn reduce(&self, item: Option<Bound<'_, PyDict>>) -> Result<Expr> {
let value = item.map(|item| pythonize::depythonize(&item)).transpose()?;
self.0
.clone()
.reduce(value.as_ref())
.map(Expr)
.map_err(Error::from)
}

fn to_json<'py>(&self, py: Python<'py>) -> Result<Bound<'py, PyAny>> {
pythonize::pythonize(py, &self.0).map_err(Error::from)
}
Expand Down