Skip to main content

Crate ravenrs_criteria

Crate ravenrs_criteria 

Source
Expand description

A criteria and expression language, independent of any UI: the same expressions filter a grid, compute a report total, guard a security rule or become a SQL WHERE.

  • Criteria is a public tree. Parse it from text, build it in code, walk it, and print it back to text that parses to the same tree.
  • Scope evaluates it in memory over any Record: JSON, a map, or your own rows.
  • validate checks it against a Schema before it runs.
  • to_sql translates it to parameterized SQL.
  • functions::catalog lists every function, for expression editors.

§Syntax

  • Fields and parameters: [Price], [Customer.Name], [^.Number] (parent row), ?MinTotal.
  • Literals: 12.5, 'text' ('' escapes a quote), #2026-11-01#, True, False, Null, {guid}.
  • Operators:
    • Arithmetic: + - * / %; + joins text.
    • Comparison: = <> != < <= > >=, Like '%a_'.
    • Logic: And Or Not (or && || !).
    • Bits: & | ^ ~.
    • Tests: In (a, b), Between(a, b), Is Null, Is Not Null, Not In (...).
  • Functions: Iif(...), Upper(...), AddMonths(...) and others, in any case.
  • Aggregates over the rows in scope: Sum([Total]), Count(), also sumSum(...).
  • Aggregates over a nested collection, optionally filtered: [Items][].Sum([Qty] * [Price]), [Items][[Qty] > 0].Count().
  • Comments: /* like this */.
use ravenrs_criteria::{Criteria, Environment, Scope, Value};
use serde_json::json;

let order = json!({ "Status": "open", "Items": [{ "Qty": 2, "Price": 5.0 }, { "Qty": 0, "Price": 9.0 }] });
let total = Criteria::parse("[Items][[Qty] > 0].Sum([Qty] * [Price])").expect("valid");
let env = Environment::new();
assert_eq!(Scope::new(&env).row(&order).evaluate(&total), Ok(Value::Number(10.0)));

let filter = Criteria::parse("[Status] = ?Wanted").expect("valid");
let env = Environment::new().parameter("Wanted", "open");
assert!(Scope::new(&env).row(&order).matches(&filter));

Re-exports§

pub use functions::Aggregate;
pub use functions::Category;
pub use functions::Function;
pub use functions::FunctionInfo;

Modules§

functions
The function catalog: one table that drives parsing, arity checks and the documentation an expression editor shows.

Structs§

AggregateCall
An aggregate call.
Culture
Number, currency and date conventions of a language and region.
DateTime
A calendar date with an optional time of day, without a time zone.
Environment
What every evaluation shares: parameters, calculated fields, custom functions, the culture and the clock. Keep one per run; it is not thread-safe.
NumberStyle
How to write a number: decimal places, separators and text around it. This is the one place that lays out digits, for Culture and for anything with separators of its own.
ParseError
A syntax error, with the byte offset where it was found.
Schema
What an expression may refer to.
Scope
Where an expression is evaluated: the row, its parent, the rows aggregates run over, and the page.
SqlQuery
Translated SQL: text with placeholders, and what to bind to them, in order.

Enums§

BinaryOp
Binary operators.
Criteria
A parsed expression or filter.
Dialect
A database’s SQL flavor.
EvalError
A problem while evaluating. Type mismatches are not errors: they give Null, as a missing value would, so a report still prints.
FieldType
The kind of value a field holds.
GroupOp
And or Or.
Problem
Something wrong with an expression.
SqlError
Why something cannot run in the database.
SqlParam
A bound parameter of a translated query.
UnaryOp
Unary operators.
Value
A value: what fields hold and expressions compute.

Traits§

Record
A row expressions can read: a JSON object, a grid row, an entity.

Functions§

format_composite
Formats a composite string: {0}, {1:C2}, {{ and }} for braces.
format_value
Formats value with a .NET-style format specifier (C2, N0, dd/MM/yyyy, #,##0.00…). An empty specifier gives the plain text.
to_sql
Translates criteria to a SQL expression for a WHERE or a SELECT list.
validate
Every problem found in criteria, in order, without repeats.

Type Aliases§

CustomFunction
A function a program adds to the language.