[−][src]Struct balena_temen::EngineBuilder
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]
S: Into<String>,
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 namefilter
- 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]
S: Into<String>,
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 namefunction
- 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]
S: Into<String>,
Trait Implementations
impl From<EngineBuilder> for Engine
[src]
fn from(builder: EngineBuilder) -> Engine
[src]
impl Default for EngineBuilder
[src]
fn default() -> EngineBuilder
[src]
Creates new EngineBuilder
with default filters, functions and the evaluation keyword
Auto Trait Implementations
impl Send for EngineBuilder
impl Sync for EngineBuilder
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
🔬 This is a nightly-only experimental API. (
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,