use datumesh_core::{Arg, Args, Cell, Error, ErrorCode, FunctionDescriptor, Scalar};
use crate::error::EngineError;
pub(crate) fn check_no_slice(args: &Args) -> Result<(), EngineError> {
for (k, a) in args {
if *a == Arg::Slice {
return Err(EngineError::Invalid(Error::new(
ErrorCode::Slice,
format!("$.args.{k}"),
"a cell is asked for, not a slice",
)));
}
}
Ok(())
}
pub(crate) fn check_args(cell: &Cell, d: &FunctionDescriptor) -> Result<(), Error> {
let decl = &d.signature.args;
for k in cell.args.keys() {
if !decl.contains_key(k) {
return Err(Error::new(
ErrorCode::ArgUnknown,
format!("$.args.{k}"),
"the function declares no such argument",
));
}
}
for k in decl.keys() {
if !cell.args.contains_key(k) {
return Err(Error::new(ErrorCode::ArgMissing, format!("$.args.{k}"), "a declared argument is not bound"));
}
}
for (k, a) in &cell.args {
if a.scalar().map(Scalar::ty) != decl.get(k).map(|x| x.ty) {
return Err(Error::new(ErrorCode::ArgType, format!("$.args.{k}"), "type differs from the declaration"));
}
}
Ok(())
}
pub(crate) fn valid_args(cell: &Cell, d: &FunctionDescriptor) -> Args {
cell.args
.iter()
.filter(|(k, _)| d.signature.args.get(*k).is_some_and(|x| x.valid))
.map(|(k, a)| (k.clone(), a.clone()))
.collect()
}