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
29
30
31
32
33
34
35
36
37

use std::fmt;
use serde_json::Value;
use error::Error;


/// Custom function
pub struct Function {
    /// Maximum number of arguments.
    pub max_args: Option<usize>,
    /// Minimum number of arguments.
    pub min_args: Option<usize>,
    /// Accept values and return a result which contains a value.
    pub compiled: Box<Fn(Vec<Value>) -> Result<Value, Error> + Sync + Send>,
}

impl Function {
    /// Create a function with a closure.
    pub fn new<F>(closure: F) -> Function
        where F: 'static + Fn(Vec<Value>) -> Result<Value, Error> + Sync + Send
    {
        Function {
            max_args: None,
            min_args: None,
            compiled: Box::new(closure),
        }
    }
}

impl fmt::Debug for Function {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "Function {{ max_args: {:?}, min_args: {:?} }}",
               self.max_args,
               self.min_args)
    }
}