Skip to main content

expr/functions/
mod.rs

1pub mod array;
2pub mod bitwise;
3pub mod collection;
4pub mod convert;
5pub mod json;
6pub mod misc;
7pub mod number;
8pub mod string;
9pub mod temporal;
10
11use crate::Result;
12
13use crate::ast::program::Program;
14use crate::{bail, ContextProvider, Environment, Value};
15
16pub type Function<'a> = Box<dyn Fn(ExprCall) -> Result<Value> + 'a + Sync + Send>;
17
18pub struct ExprCall<'a, 'b> {
19    pub ident: String,
20    pub args: Vec<Value>,
21    pub predicate: Option<&'a Program>,
22    pub ctx: &'a dyn ContextProvider,
23    pub env: &'a Environment<'b>,
24}
25
26impl Environment<'_> {
27    pub fn eval_func(
28        &self,
29        ctx: &dyn ContextProvider,
30        ident: &str,
31        args: Vec<Value>,
32        predicate: Option<&Program>,
33    ) -> Result<Value> {
34        let call = ExprCall {
35            ident: ident.to_string(),
36            args,
37            predicate,
38            ctx,
39            env: self,
40        };
41        if let Some(f) = self.functions.get(&call.ident) {
42            f(call)
43        } else {
44            bail!("Unknown function: {}", call.ident)
45        }
46    }
47}