1use std::collections::{HashMap, HashSet};
12
13use serde::{Deserialize, Serialize};
14
15use crate::{Utxo, UtxoRef};
16
17#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
18pub struct StructExpr {
19 pub constructor: usize,
20 pub fields: Vec<Expression>,
21}
22
23impl StructExpr {
24 pub fn unit() -> Self {
25 Self {
26 constructor: 0,
27 fields: vec![],
28 }
29 }
30}
31
32#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
33pub enum BinaryOpKind {
34 Add,
35 Sub,
36}
37
38#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
39pub struct BinaryOp {
40 pub left: Expression,
41 pub right: Expression,
42 pub op: BinaryOpKind,
43}
44
45#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
46pub struct AssetExpr {
47 pub policy: Expression,
48 pub asset_name: Expression,
49 pub amount: Expression,
50}
51
52#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
59pub struct AdHocDirective {
60 pub name: String,
61 pub data: HashMap<String, Expression>,
62}
63
64#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
65pub enum ScriptSource {
66 Embedded(Expression),
67 UtxoRef {
68 r#ref: Expression,
69 source: Option<Expression>,
70 },
71}
72
73impl ScriptSource {
74 pub fn as_utxo_ref(&self) -> Option<&Expression> {
75 match self {
76 Self::UtxoRef { r#ref, .. } => Some(r#ref),
77 _ => None,
78 }
79 }
80}
81
82#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
83pub struct PolicyExpr {
84 pub name: String,
85 pub hash: Expression,
86 pub script: Option<ScriptSource>,
87}
88
89#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
90pub enum Type {
91 Undefined,
92 Unit,
93 Int,
94 Bool,
95 Bytes,
96 Address,
97 UtxoRef,
98 AnyAsset,
99 Custom(String),
100}
101
102#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
103pub struct PropertyAccess {
104 pub object: Box<Expression>,
105 pub field: String,
106}
107
108#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
109pub enum Expression {
110 None,
111 Struct(StructExpr),
112 Bytes(Vec<u8>),
113 Number(i128),
114 Bool(bool),
115 String(String),
116 Address(Vec<u8>),
117 Hash(Vec<u8>),
118 UtxoRefs(Vec<UtxoRef>),
119 UtxoSet(HashSet<Utxo>),
120 Assets(Vec<AssetExpr>),
121
122 EvalParameter(String, Type),
123 EvalProperty(Box<PropertyAccess>),
124 EvalInputDatum(String),
125 EvalInputAssets(String),
126 EvalCustom(Box<BinaryOp>),
127
128 FeeQuery,
130
131 AdHocDirective(Box<AdHocDirective>),
133}
134
135impl Expression {
136 pub fn is_none(&self) -> bool {
137 matches!(self, Self::None)
138 }
139}
140
141#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
142pub struct InputQuery {
143 pub address: Option<Expression>,
144 pub min_amount: Option<Expression>,
145 pub r#ref: Option<Expression>,
146}
147
148#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
149pub struct Input {
150 pub name: String,
151 pub query: Option<InputQuery>,
152 pub refs: HashSet<UtxoRef>,
153 pub redeemer: Option<Expression>,
154 pub policy: Option<PolicyExpr>,
155}
156
157#[derive(Serialize, Deserialize, Debug, Clone)]
158pub struct Output {
159 pub address: Option<Expression>,
160 pub datum: Option<Expression>,
161 pub amount: Option<Expression>,
162}
163
164#[derive(Serialize, Deserialize, Debug, Clone)]
165pub struct Mint {
166 pub amount: Option<Expression>,
167 pub redeemer: Option<Expression>,
168}
169
170#[derive(Serialize, Deserialize, Debug, Clone)]
171pub struct Tx {
172 pub fees: Expression,
173 pub inputs: Vec<Input>,
174 pub outputs: Vec<Output>,
175 pub mint: Option<Mint>,
176 pub adhoc: Vec<AdHocDirective>,
177}