Register tests as callable definitions.

Also move the registering of validators into the same place as they
  other and define a little cute function to avoid code-duplication.
This commit is contained in:
KtorZ
2024-03-03 18:26:37 +01:00
parent c2dc47fa0b
commit 1134b8d7d0
2 changed files with 81 additions and 66 deletions

View File

@@ -148,6 +148,16 @@ impl TypedModule {
);
}
Definition::Test(test) => {
functions.insert(
FunctionAccessKey {
module_name: self.name.clone(),
function_name: test.name.clone(),
},
test.clone().into(),
);
}
Definition::DataType(dt) => {
data_types.insert(
DataTypeKey {
@@ -158,11 +168,19 @@ impl TypedModule {
);
}
Definition::TypeAlias(_)
| Definition::ModuleConstant(_)
| Definition::Test(_)
| Definition::Validator(_)
| Definition::Use(_) => {}
Definition::Validator(v) => {
let module_name = self.name.as_str();
if let Some((k, v)) = v.into_function_definition(module_name, |f, _| Some(f)) {
functions.insert(k, v);
}
if let Some((k, v)) = v.into_function_definition(module_name, |_, f| f) {
functions.insert(k, v);
}
}
Definition::TypeAlias(_) | Definition::ModuleConstant(_) | Definition::Use(_) => {}
}
}
}
@@ -513,6 +531,39 @@ pub struct Validator<T, Expr> {
pub params: Vec<Arg<T>>,
}
impl TypedValidator {
pub fn into_function_definition<'a, F>(
&'a self,
module_name: &str,
select: F,
) -> Option<(FunctionAccessKey, TypedFunction)>
where
F: Fn(&'a TypedFunction, Option<&'a TypedFunction>) -> Option<&'a TypedFunction> + 'a,
{
match select(&self.fun, self.other_fun.as_ref()) {
None => None,
Some(fun) => {
let mut fun = fun.clone();
fun.arguments = self
.params
.clone()
.into_iter()
.chain(fun.arguments)
.collect();
Some((
FunctionAccessKey {
module_name: module_name.to_string(),
function_name: fun.name.clone(),
},
fun,
))
}
}
}
}
pub type TypedDefinition = Definition<Rc<Type>, TypedExpr, String>;
pub type UntypedDefinition = Definition<(), UntypedExpr, ()>;