1use crate::{
2 encoding::{AnyTir, TirVersion},
3 model::v1beta0,
4 Visitor,
5};
6use serde::{de::DeserializeOwned, Serialize};
7
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10 #[error("unsupported TIR version: {0}")]
11 UnsupportedTirVersion(TirVersion),
12
13 #[error("error coercing {0} into {1}")]
14 CoerceError(String, String),
15
16 #[error("format error {0}")]
17 FormatError(String),
18
19 #[error("missing expression: {0}")]
20 MissingExpression(String),
21
22 #[error("consistency error: {0}")]
23 ConsistencyError(String),
24}
25
26#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
27pub struct CompiledTx {
28 pub payload: Vec<u8>,
29 pub hash: Vec<u8>,
30 pub fee: u64,
31 pub ex_units: u64,
32}
33
34pub trait Compiler: Serialize + DeserializeOwned {
35 type CompilerOp;
36 type Expression;
37
38 fn compile(&mut self, tir: &AnyTir) -> Result<CompiledTx, Error>;
39 fn reduce_op(&self, op: Self::CompilerOp) -> Result<Self::Expression, crate::reduce::Error>;
40}
41
42impl<C> Visitor for C
43where
44 C: Compiler<Expression = v1beta0::Expression, CompilerOp = v1beta0::CompilerOp>,
45{
46 fn reduce(
47 &mut self,
48 expr: v1beta0::Expression,
49 ) -> Result<v1beta0::Expression, crate::reduce::Error> {
50 match expr {
51 v1beta0::Expression::EvalCompiler(op) => Ok(self.reduce_op(*op)?),
52 _ => Ok(expr),
53 }
54 }
55}