2019-03-18 17:21:07 +00:00
|
|
|
use error::{self, Error};
|
2019-03-15 18:26:25 +00:00
|
|
|
use value::Value;
|
|
|
|
|
|
|
|
pub struct Function {
|
2019-03-18 17:21:07 +00:00
|
|
|
argument_amount: usize,
|
|
|
|
function: Box<Fn(&[Value]) -> Result<Value, Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
|
|
|
pub fn new(
|
|
|
|
argument_amount: usize,
|
|
|
|
function: Box<Fn(&[Value]) -> Result<Value, Error>>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
argument_amount,
|
|
|
|
function,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn call(&self, arguments: &[Value]) -> Result<Value, Error> {
|
2019-03-19 14:54:52 +00:00
|
|
|
error::expect_function_argument_amount(arguments.len(), self.argument_amount)?;
|
2019-03-18 17:21:07 +00:00
|
|
|
(self.function)(arguments)
|
|
|
|
}
|
2019-03-18 16:02:45 +00:00
|
|
|
}
|