Struct balena_temen::Engine
source · pub struct Engine { /* private fields */ }
Expand description
An expression evaluation engine
Implementations
sourceimpl Engine
impl Engine
sourcepub fn eval(
&self,
expression: &str,
position: &Identifier,
data: &Value,
context: &mut Context
) -> Result<Value>
pub fn eval(
&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)
);
sourcepub fn eval_as_bool(
&self,
expression: &str,
position: &Identifier,
data: &Value,
context: &mut Context
) -> Result<bool>
pub fn eval_as_bool(
&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
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 Engine
impl Send for Engine
impl Sync for Engine
impl Unpin for Engine
impl UnwindSafe for Engine
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