[][src]Struct balena_temen::EngineBuilder

pub struct EngineBuilder { /* fields omitted */ }

A custom engine builder

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

Methods

impl EngineBuilder
[src]

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

impl From<EngineBuilder> for Engine
[src]

impl Default for EngineBuilder
[src]

Creates new EngineBuilder with default filters, functions and the evaluation keyword

Auto Trait Implementations

Blanket Implementations

impl<T> From for T
[src]

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

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