use super::CompilationError;
use super::Context;
use super::TxTmplIt;
use crate::contract::actions::ConditionallyCompileIfList;
use crate::contract::actions::GuardList;
use crate::contract::actions::{FinishOrFunc, WebAPIDisabled};
use crate::template::Template;
use sapio_base::Clause;
use std::marker::PhantomData;
use std::sync::Arc;
pub struct ThenFuncTypeTag(pub(crate) ());
impl ThenFuncTypeTag {
pub fn coerce_args<StatefulArguments>(_f: StatefulArguments) -> Result<Self, CompilationError> {
Ok(ThenFuncTypeTag(()))
}
}
pub type ThenFuncAsFinishOrFunc<'a, ContractSelf, StatefulArguments> =
FinishOrFunc<'a, ContractSelf, StatefulArguments, ThenFuncTypeTag, WebAPIDisabled>;
pub struct ThenFunc<'a, ContractSelf> {
pub guard: GuardList<'a, ContractSelf>,
pub conditional_compile_if: ConditionallyCompileIfList<'a, ContractSelf>,
pub func: fn(&ContractSelf, Context, ThenFuncTypeTag) -> TxTmplIt,
pub name: Arc<String>,
}
impl<'a, ContractSelf, StatefulArgs> From<ThenFunc<'a, ContractSelf>>
for ThenFuncAsFinishOrFunc<'a, ContractSelf, StatefulArgs>
{
fn from(f: ThenFunc<'a, ContractSelf>) -> Self {
FinishOrFunc {
guard: f.guard,
conditional_compile_if: f.conditional_compile_if,
func: f.func,
name: f.name,
coerce_args: ThenFuncTypeTag::coerce_args,
schema: None,
f: PhantomData::default(),
returned_txtmpls_modify_guards: true,
extract_clause_from_txtmpl: ctv_clause_extractor,
simp_gen: None,
}
}
}
fn ctv_clause_extractor(t: &Template, ctx: &Context) -> Result<Option<Clause>, CompilationError> {
let h = t.hash();
if t.guards.is_empty() {
ctx.ctv_emulator(h)
} else {
let mut g = t.guards.clone();
g.push(ctx.ctv_emulator(h)?);
Ok(Clause::And(g))
}
.map(Some)
}