Skip to main content

Environment

Struct Environment 

Source
pub struct Environment { /* private fields */ }
Expand description

What every evaluation shares: parameters, calculated fields, custom functions, the culture and the clock. Keep one per run; it is not thread-safe.

Implementations§

Source§

impl Environment

Source

pub fn new() -> Self

An environment in US English, on the system clock.

Source

pub fn culture(self, culture: Culture) -> Self

Sets the culture for formats.

Source

pub fn formats(&self) -> &Culture

The culture in effect, for formatting outside expressions.

Source

pub fn at(self, now: DateTime) -> Self

Fixes the clock (Today(), Now()), for tests and reproducible runs.

Source

pub fn parameter(self, name: impl Into<String>, value: impl Into<Value>) -> Self

Sets a parameter, read as ?name.

Source

pub fn calculated(self, name: impl Into<String>, expression: Criteria) -> Self

Adds a calculated field, read as [name].

Source

pub fn register( self, name: &str, function: impl Fn(&[Value]) -> Result<Value, String> + 'static, ) -> Self

Adds a function to the language; names are case-insensitive.

use ravenrs_criteria::{Criteria, Environment, Scope, Value};

let env = Environment::new().register("Double", |args| {
    Ok(Value::Number(args[0].as_number().unwrap_or(0.0) * 2.0))
});
let c = Criteria::parse("double(21)").expect("valid");
let row = serde_json::json!({});
assert_eq!(Scope::new(&env).row(&row).evaluate(&c), Ok(Value::Number(42.0)));
Source

pub fn has_function(&self, name: &str) -> bool

Whether a custom function with this name is registered.

Trait Implementations§

Source§

impl Debug for Environment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Environment

Source§

fn default() -> Self

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

Auto Trait Implementations§

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.