1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::fmt::{self, Display, Formatter};

use serde::{Deserialize, Serialize};

use crate::{eval, eval_with_context, Result, Value, VariableMap};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Function(String);

impl Function {
    pub fn new(body: &str) -> Self {
        Function(body.to_string())
    }

    pub fn run(&self) -> Result<Value> {
        eval(&self.0)
    }

    pub fn run_with_context(&self, context: &mut VariableMap) -> Result<Value> {
        eval_with_context(&self.0, context)
    }
}

impl Display for Function {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}