Skip to main content

Crate maplibre_expr

Crate maplibre_expr 

Source
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.0 range.
EvalError
An error raised while evaluating a well-formed expression.
EvaluationContext
Everything an expression can read while evaluating: global parameters such as zoom, the current Feature, and the shared global-state map read by the global-state operator.
Feature
The feature (and its properties) an expression is evaluated against.
Function
An eval-time function: body (raw JSON) is evaluated with params bound to the argument values. May reference itself or other functions (recursion is bounded at runtime).
Macro
A parse-time macro: body is expanded with params bound to the call arguments (as a let). body is raw JSON in the expression grammar.
Options
Parser/runtime extension registry.
ParseError
An error raised while turning JSON into an Expr.

Enums§

EvalErrorKind
The semantic cause of an evaluation error.
Expr
A parsed MapLibre expression.
InterpKind
The interpolation curve used by an interpolate expression.
InterpSpace
The color space an interpolate expression blends in.
ParseErrorKind
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 ParseError for expressions the reference implementation rejects at compile time (bad comparisons, malformed match branches, non-interpolatable outputs, misused zoom, and so on).