[][src]Struct balena_temen::Engine

pub struct Engine { /* fields omitted */ }

An expression evaluation engine

Methods

impl Engine[src]

pub fn eval(
    &self,
    expression: &str,
    position: &Identifier,
    data: &Value,
    context: &mut Context
) -> Result<Value>
[src]

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)
);

pub fn eval_as_bool(
    &self,
    expression: &str,
    position: &Identifier,
    data: &Value,
    context: &mut Context
) -> Result<bool>
[src]

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

impl Default for Engine[src]

fn default() -> Engine[src]

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

impl From<EngineBuilder> for Engine[src]

Auto Trait Implementations

impl Send for Engine

impl Sync for Engine

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]