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-19 16:58:53 +00:00
|
|
|
argument_amount: Option<usize>,
|
2019-03-18 17:21:07 +00:00
|
|
|
function: Box<Fn(&[Value]) -> Result<Value, Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
|
|
|
pub fn new(
|
2019-03-19 16:58:53 +00:00
|
|
|
argument_amount: Option<usize>,
|
2019-03-18 17:21:07 +00:00
|
|
|
function: Box<Fn(&[Value]) -> Result<Value, Error>>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
argument_amount,
|
|
|
|
function,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn call(&self, arguments: &[Value]) -> Result<Value, Error> {
|
2019-03-19 16:58:53 +00:00
|
|
|
if let Some(argument_amount) = self.argument_amount {
|
|
|
|
error::expect_function_argument_amount(arguments.len(), argument_amount)?;
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:21:07 +00:00
|
|
|
(self.function)(arguments)
|
|
|
|
}
|
2019-03-18 16:02:45 +00:00
|
|
|
}
|