pub struct EngineBuilder { /* private fields */ }
Expand description

A custom engine builder

Allows to build an Engine with custom filters, functions or the evaluation keyword.

Implementations

Registers custom filter

If a filter with the name already exists, it will be overwritten.

Visit FilterFn to learn more about filters.

Arguments
  • name - Custom filter name
  • filter - Custom filter function
Examples
use balena_temen::{
    ast::Identifier,
    Engine, EngineBuilder, Context, Value,
    error::*
};
use serde_json::json;
use std::collections::HashMap;

fn text_filter(input: &Value, args: &HashMap<String, Value>, _: &mut Context) -> Result<Value> {
    let input = input.as_str()
        .ok_or_else(|| {
            Error::with_message("invalid input type")
                .context("expected", "string")
                .context("value", input.to_string())
    })?;

    let trim = args.get("trim")
        .unwrap_or_else(|| &Value::Bool(false));
    let trim = trim
        .as_bool()
        .ok_or_else(|| {
            Error::with_message("invalid argument type")
                .context("argument", "trim")
                .context("expected", "boolean")
                .context("value", trim.to_string())
        })?;

    let upper = args.get("upper")
        .unwrap_or_else(|| &Value::Bool(false));
    let upper = upper
        .as_bool()
        .ok_or_else(|| {
            Error::with_message("invalid argument type")
                .context("argument", "upper")
                .context("expected", "boolean")
                .context("value", trim.to_string())
        })?;

    let result = match (trim, upper) {
        (false, false) => input.to_string(),
        (true, false) => input.trim().to_string(),
        (false, true) => input.to_uppercase(),
        (true, true) => input.trim().to_uppercase(),
    };

    Ok(Value::String(result))
};

let engine: Engine = EngineBuilder::default()
    .filter("text", text_filter)
    .into();
let mut ctx = Context::default();
let position = Identifier::default();
let data = Value::Null;

assert_eq!(
    engine.eval("` abc ` | text", &position, &data, &mut ctx).unwrap(),
    json!(" abc ")
);
assert_eq!(
    engine.eval("` abc ` | text(trim=true)", &position, &data, &mut ctx).unwrap(),
    json!("abc")
);
assert_eq!(
    engine.eval("` abc ` | text(trim=true, upper=true)", &position, &data, &mut ctx).unwrap(),
    json!("ABC")
);

Registers custom function

If a function with the name already exists, it will be overwritten.

Visit FunctionFn to learn more about functions.

Arguments
  • name - Custom function name
  • function - Custom function function
Examples
use balena_temen::{
    ast::Identifier,
    Engine, EngineBuilder, Context, Value,
    error::*
};
use serde_json::json;
use std::collections::HashMap;

fn echo_function(args: &HashMap<String, Value>, _: &mut Context) -> Result<Value> {
    let value = match args.get("value") {
        Some(x) => {
            x.as_str().ok_or_else(|| {
                Error::with_message("invalid argument type")
                    .context("expect", "string")
                    .context("value", x.to_string())
            })?
        },
        None => "echo"
    };

    Ok(Value::String(value.to_string()))
};

let engine: Engine = EngineBuilder::default()
    .function("echo", echo_function)
    .into();
let mut ctx = Context::default();
let position = Identifier::default();
let data = Value::Null;

assert_eq!(
    engine.eval("echo()", &position, &data, &mut ctx).unwrap(),
    json!("echo")
);
assert_eq!(
    engine.eval("echo(value=`Hallo`)", &position, &data, &mut ctx).unwrap(),
    json!("Hallo")
);
assert!(
    engine.eval("echo(value=1)", &position, &data, &mut ctx).is_err()
);

Registers custom evaluation keyword

Defaults to $$eval if no keyword is registered.

Arguments
  • keyword - An evaluation keyword
Examples

Trait Implementations

Creates new EngineBuilder with default filters, functions 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.