[−][src]Struct balena_temen::Engine
An expression evaluation engine
Methods
impl Engine
[src]
pub fn eval(
&self,
expression: &str,
position: &Identifier,
data: &Value,
context: &mut Context
) -> Result<Value>
[src]
&self,
expression: &str,
position: &Identifier,
data: &Value,
context: &mut Context
) -> Result<Value>
Evaluates an expression
Result can be any valid JSON value.
Arguments
expression
- An expression to evaluateposition
- An initial position for relative identifiersdata
- A JSON with variable valuescontext
- An evaluation context
Examples
use balena_temen::{ ast::Identifier, Engine, Context, Value }; use serde_json::json; let engine = Engine::default(); // Default functions, filters let mut ctx = Context::default(); // Default context let position = Identifier::default(); // Evaluate from the root let data = json!({ "numbers": { "one": 1, "two": 2 }, "names": [ "zero", "one", "two" ] }); // Math expression assert_eq!( engine.eval("2 + 3", &position, &data, &mut ctx).unwrap(), json!(5) ); // Filters assert_eq!( engine.eval("`Balena is great!` | SLUGIFY", &position, &data, &mut ctx).unwrap(), json!("balena-is-great") ); // Variables assert_eq!( engine.eval("numbers.one + numbers.two", &position, &data, &mut ctx).unwrap(), json!(3) ); assert_eq!( engine.eval("numbers[`one`] * numbers[`two`]", &position, &data, &mut ctx).unwrap(), json!(2) ); // Indirect / nested variables assert_eq!( engine.eval("numbers[names[1]] + numbers[names[2]]", &position, &data, &mut ctx).unwrap(), json!(3) ); assert_eq!( engine.eval("numbers[names.1] + numbers[names.2]", &position, &data, &mut ctx).unwrap(), json!(3) );
pub fn eval_as_bool(
&self,
expression: &str,
position: &Identifier,
data: &Value,
context: &mut Context
) -> Result<bool>
[src]
&self,
expression: &str,
position: &Identifier,
data: &Value,
context: &mut Context
) -> Result<bool>
Evaluates an expression as a boolean
Result must evaluate to a boolean value otherwise it fails. Numbers, strings, ... do not evaluate to a boolean like in other languages.
Arguments
expression
- An expression to evaluateposition
- An initial position for relative identifiersdata
- A JSON with variable valuescontext
- An evaluation context
Examples
use balena_temen::{ ast::Identifier, Engine, Context, Value }; use serde_json::json; let engine = Engine::default(); // Default functions, filters let mut ctx = Context::default(); // Default context let position = Identifier::default(); // Evaluate from the root let data = Value::Null; // No data (variables) assert_eq!( engine.eval_as_bool("2 == 2 + 3", &position, &data, &mut ctx).unwrap(), json!(false) ); // An expression MUST evaluate to a boolean otherwise the evaluation fails assert!( engine.eval_as_bool("1", &position, &data, &mut ctx).is_err() ); // Invalid syntax leads to a failure too assert!( engine.eval_as_bool("true ==", &position, &data, &mut ctx).is_err() );
Trait Implementations
impl Default for Engine
[src]
fn default() -> Engine
[src]
Creates new Engine
with default set of functions, filters and the evaluation keyword.
impl From<EngineBuilder> for Engine
[src]
fn from(builder: EngineBuilder) -> Engine
[src]
Auto Trait Implementations
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 = Infallible
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> 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>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,