Expand description
maplibre_expr — a pure-Rust parser and evaluator for
MapLibre GL style expressions.
The crate turns a MapLibre expression (a serde_json::Value such as
["*", ["get", "x"], 2]) into an Expr tree with parse, then
evaluates that tree against an EvaluationContext with evaluate.
use maplibre_expr::{parse, evaluate, EvaluationContext, Feature, Value};
use std::collections::BTreeMap;
let json: serde_json::Value = serde_json::json!(["*", ["get", "x"], 2]);
let expr = parse(&json).unwrap();
let mut props = BTreeMap::new();
props.insert("x".to_string(), Value::Number(21.0));
let ctx = EvaluationContext::new().with_feature(Feature {
properties: props,
..Feature::default()
});
assert_eq!(evaluate(&expr, &ctx).unwrap(), Value::Number(42.0));Conformance is validated against a vendored snapshot of the
maplibre-style-spec expression test fixtures; see tests/spec.rs.
Re-exports§
pub use filter::convert_legacy_filter;pub use filter::is_expression_filter;pub use filter::FilterError;
Modules§
- convert
- Converting legacy MapLibre function objects into modern expressions.
- filter
- Converting legacy MapLibre filters into modern expressions.
Structs§
- Color
- An RGBA color with channels stored as floats in the
0.0..=1.0range. - Eval
Error - An error raised while evaluating a well-formed expression.
- Evaluation
Context - Everything an expression can read while evaluating: global parameters such
as
zoom, the currentFeature, and the shared global-state map read by theglobal-stateoperator. - Feature
- The feature (and its properties) an expression is evaluated against.
- Function
- An eval-time function:
body(raw JSON) is evaluated withparamsbound to the argument values. May reference itself or other functions (recursion is bounded at runtime). - Macro
- A parse-time macro:
bodyis expanded withparamsbound to the call arguments (as alet).bodyis raw JSON in the expression grammar. - Options
- Parser/runtime extension registry.
- Parse
Error - An error raised while turning JSON into an
Expr.
Enums§
- Eval
Error Kind - The semantic cause of an evaluation error.
- Expr
- A parsed MapLibre expression.
- Interp
Kind - The interpolation curve used by an
interpolateexpression. - Interp
Space - The color space an
interpolateexpression blends in. - Parse
Error Kind - The semantic cause of a parse/compile error.
- Projection
- A projection definition value.
- Type
- A type in the MapLibre expression type lattice.
- Value
- A value in the MapLibre expression type system.
Functions§
- evaluate
- Evaluate a parsed expression against an evaluation context.
- evaluate_
with - Evaluate with user
Options, so calls to user functions are dispatched to their (recursion-limited) bodies. - parse
- Parse a MapLibre expression from its JSON representation.
- parse_
with - Parse an expression with user
Options(macros expand at parse time; function names are accepted as callable operators). - typecheck
- Statically type-check a parsed expression, optionally against the type a
property expects. On success returns a rewritten tree with type-directed
coercion/assertion nodes inserted (evaluate this returned expression so the
coercions take effect). Returns a
ParseErrorfor expressions the reference implementation rejects at compile time (bad comparisons, malformedmatchbranches, non-interpolatable outputs, misusedzoom, and so on).