Struct balena_temen::Engine

source ·
pub struct Engine { /* private fields */ }
Expand description

An expression evaluation engine

Implementations

Evaluates an expression

Result can be any valid JSON value.

Arguments
  • expression - An expression to evaluate
  • position - An initial position for relative identifiers
  • data - A JSON with variable values
  • context - An evaluation context
Examples
use balena_temen::{
    ast::Identifier,
    Engine, Context, Value
};
use serde_json::json;

let engine = Engine::default();       // Default functions, filters
let mut ctx = Context::default();     // Default context
let position = Identifier::default(); // Evaluate from the root
let data = json!({
  "numbers": {
    "one": 1,
    "two": 2
  },
  "names": [
    "zero",
    "one",
    "two"
  ]
});

// Math expression

assert_eq!(
    engine.eval("2 + 3", &position, &data, &mut ctx).unwrap(),
    json!(5)
);

// Filters

assert_eq!(
    engine.eval("`Balena is great!` | slugify", &position, &data, &mut ctx).unwrap(),
    json!("balena-is-great")
);

// Variables

assert_eq!(
    engine.eval("numbers.one + numbers.two", &position, &data, &mut ctx).unwrap(),
    json!(3)
);
assert_eq!(
    engine.eval("numbers[`one`] * numbers[`two`]", &position, &data, &mut ctx).unwrap(),
    json!(2)
);

// Indirect / nested variables

assert_eq!(
    engine.eval("numbers[names[1]] + numbers[names[2]]", &position, &data, &mut ctx).unwrap(),
    json!(3)
);
assert_eq!(
    engine.eval("numbers[names.1] + numbers[names.2]", &position, &data, &mut ctx).unwrap(),
    json!(3)
);

Evaluates an expression as a boolean

Result must evaluate to a boolean value otherwise it fails. Numbers, strings, … do not evaluate to a boolean like in other languages.

Arguments
  • expression - An expression to evaluate
  • position - An initial position for relative identifiers
  • data - A JSON with variable values
  • context - An evaluation context
Examples
use balena_temen::{
    ast::Identifier,
    Engine, Context, Value
};
use serde_json::json;

let engine = Engine::default();       // Default functions, filters
let mut ctx = Context::default();     // Default context
let position = Identifier::default(); // Evaluate from the root
let data = Value::Null;               // No data (variables)

assert_eq!(
    engine.eval_as_bool("2 == 2 + 3", &position, &data, &mut ctx).unwrap(),
    json!(false)
);

// An expression MUST evaluate to a boolean otherwise the evaluation fails

assert!(
    engine.eval_as_bool("1", &position, &data, &mut ctx).is_err()
);

// Invalid syntax leads to a failure too

assert!(
    engine.eval_as_bool("true ==", &position, &data, &mut ctx).is_err()
);

Trait Implementations

Creates new Engine with default set of functions, filters and the evaluation keyword.

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.