Struct Evaluator

Source
pub struct Evaluator<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Evaluator<'a>

Source

pub fn new() -> Self

Source

pub fn with_transform<F>(self, name: &str, transform: F) -> Self
where F: Fn(&[Value]) -> Result<'_, Value, Error> + Send + Sync + 'a,

Adds a custom transform function This is meant as a way to allow consumers to add their own custom functionality to the expression language. Note that the name added here has to match with the name that the transform will have when it’s a part of the expression statement

§Arguments:
  • name: The name of the transfrom
  • transform: The actual function. A closure the implements Fn(&serde_json::Value) -> Result<Value, anyhow::Error>
§Example:
use jexl_eval::Evaluator;
use serde_json::{json as value, Value};

let mut evaluator = Evaluator::new().with_transform("lower", |v: &[Value]| {
   let s = v
           .first()
           .expect("Should have 1 argument!")
           .as_str()
           .expect("Should be a string!");
      Ok(value!(s.to_lowercase()))
 });

assert_eq!(evaluator.eval("'JOHN DOe'|lower").unwrap(), value!("john doe"))
Source

pub fn eval<'b>(&self, input: &'b str) -> Result<'b, Value>

Source

pub fn eval_in_context<'b, T: Serialize>( &self, input: &'b str, context: T, ) -> Result<'b, Value>

Trait Implementations§

Source§

impl<'a> Default for Evaluator<'a>

Source§

fn default() -> Evaluator<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Evaluator<'a>

§

impl<'a> !RefUnwindSafe for Evaluator<'a>

§

impl<'a> Send for Evaluator<'a>

§

impl<'a> Sync for Evaluator<'a>

§

impl<'a> Unpin for Evaluator<'a>

§

impl<'a> !UnwindSafe for Evaluator<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.