Skip to main content

expr/functions/
mod.rs

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