Struct balena_temen::EngineBuilder
source · 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
sourceimpl EngineBuilder
impl EngineBuilder
sourcepub fn filter<S>(self, name: S, filter: FilterFn) -> EngineBuilderwhere
S: Into<String>,
pub fn filter<S>(self, name: S, filter: FilterFn) -> EngineBuilderwhere
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: &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")
);
sourcepub fn function<S>(self, name: S, function: FunctionFn) -> EngineBuilderwhere
S: Into<String>,
pub fn function<S>(self, name: S, function: FunctionFn) -> EngineBuilderwhere
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: &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()
);
sourcepub fn eval_keyword<S>(self, keyword: S) -> EngineBuilderwhere
S: Into<String>,
pub fn eval_keyword<S>(self, keyword: S) -> EngineBuilderwhere
S: Into<String>,
Trait Implementations
sourceimpl Default for EngineBuilder
impl Default for EngineBuilder
sourcefn default() -> EngineBuilder
fn default() -> EngineBuilder
Creates new EngineBuilder
with default filters, functions and the evaluation keyword
sourceimpl From<EngineBuilder> for Engine
impl From<EngineBuilder> for Engine
sourcefn from(builder: EngineBuilder) -> Engine
fn from(builder: EngineBuilder) -> Engine
Converts to this type from the input type.
Auto Trait Implementations
impl RefUnwindSafe for EngineBuilder
impl Send for EngineBuilder
impl Sync for EngineBuilder
impl Unpin for EngineBuilder
impl UnwindSafe for EngineBuilder
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more