expressive/src/function/mod.rs

28 lines
668 B
Rust
Raw Normal View History

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 {
argument_amount: Option<usize>,
2019-03-18 17:21:07 +00:00
function: Box<Fn(&[Value]) -> Result<Value, Error>>,
}
impl Function {
pub fn new(
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> {
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
}