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
impl Environment
Sourcepub fn at(self, now: DateTime) -> Self
pub fn at(self, now: DateTime) -> Self
Fixes the clock (Today(), Now()), for tests and reproducible runs.
Sourcepub fn parameter(self, name: impl Into<String>, value: impl Into<Value>) -> Self
pub fn parameter(self, name: impl Into<String>, value: impl Into<Value>) -> Self
Sets a parameter, read as ?name.
Sourcepub fn calculated(self, name: impl Into<String>, expression: Criteria) -> Self
pub fn calculated(self, name: impl Into<String>, expression: Criteria) -> Self
Adds a calculated field, read as [name].
Sourcepub fn register(
self,
name: &str,
function: impl Fn(&[Value]) -> Result<Value, String> + 'static,
) -> Self
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)));Sourcepub fn has_function(&self, name: &str) -> bool
pub fn has_function(&self, name: &str) -> bool
Whether a custom function with this name is registered.
Trait Implementations§
Source§impl Debug for Environment
impl Debug for Environment
Auto Trait Implementations§
impl !Freeze for Environment
impl !RefUnwindSafe for Environment
impl !Send for Environment
impl !Sync for Environment
impl !UnwindSafe for Environment
impl Unpin for Environment
impl UnsafeUnpin for Environment
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more