[][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]

pub fn filter<S>(self, name: S, filter: FilterFn) -> EngineBuilder where
    S: Into<String>, 
[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: &[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(0)
        .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(1)
        .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(true)", &position, &data, &mut ctx).unwrap(),
    json!("abc")
);
assert_eq!(
    engine.eval("` abc ` | TEXT(true, true)", &position, &data, &mut ctx).unwrap(),
    json!("ABC")
);

pub fn function<S>(self, name: S, function: FunctionFn) -> EngineBuilder where
    S: Into<String>, 
[src]

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: &[Value], _: &mut Context) -> Result<Value> {
    let value = match args.first() {
        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(`Hallo`)", &position, &data, &mut ctx).unwrap(),
    json!("Hallo")
);
assert!(
    engine.eval("ECHO(1)", &position, &data, &mut ctx).is_err()
);

pub fn eval_keyword<S>(self, keyword: S) -> EngineBuilder where
    S: Into<String>, 
[src]

Registers custom evaluation keyword

Defaults to $$formula if no keyword is registered.

Arguments

  • keyword - An evaluation keyword

Examples

Trait Implementations

impl Default for EngineBuilder[src]

fn default() -> EngineBuilder[src]

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

impl From<EngineBuilder> for Engine[src]

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> 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> BorrowMut<T> for T where
    T: ?Sized
[src]

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

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