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.
Criteriais 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.Scopeevaluates it in memory over anyRecord: JSON, a map, or your own rows.validatechecks it against aSchemabefore it runs.to_sqltranslates it to parameterized SQL.functions::cataloglists 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 (...).
- Arithmetic:
- Functions:
Iif(...),Upper(...),AddMonths(...)and others, in any case. - Aggregates over the rows in scope:
Sum([Total]),Count(), alsosumSum(...). - 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§
- Aggregate
Call - An aggregate call.
- Culture
- Number, currency and date conventions of a language and region.
- Date
Time - 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.
- Number
Style - How to write a number: decimal places, separators and text around it. This is the one place
that lays out digits, for
Cultureand for anything with separators of its own. - Parse
Error - 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§
- Binary
Op - Binary operators.
- Criteria
- A parsed expression or filter.
- Dialect
- A database’s SQL flavor.
- Eval
Error - A problem while evaluating. Type mismatches are not errors: they give
Null, as a missing value would, so a report still prints. - Field
Type - The kind of value a field holds.
- GroupOp
AndorOr.- 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
valuewith a .NET-style format specifier (C2,N0,dd/MM/yyyy,#,##0.00…). An empty specifier gives the plain text. - to_sql
- Translates
criteriato a SQL expression for aWHEREor aSELECTlist. - validate
- Every problem found in
criteria, in order, without repeats.
Type Aliases§
- Custom
Function - A function a program adds to the language.