datex_core/global/protocol_structures/
instructions.rs

1use crate::values::core_values::decimal::decimal::Decimal;
2use crate::values::core_values::{
3    decimal::utils::decimal_to_string, endpoint::Endpoint,
4};
5use binrw::{BinRead, BinWrite};
6use std::fmt::Display;
7
8#[derive(Clone, Debug, PartialEq)]
9pub enum Instruction {
10    Int8(Int8Data),
11    Int16(Int16Data),
12    Int32(Int32Data),
13    Int64(Int64Data),
14    Int128(Int128Data),
15    UInt128(UInt128Data),
16    Endpoint(Endpoint),
17
18    DecimalF32(Float32Data),
19    DecimalF64(Float64Data),
20    DecimalAsInt16(FloatAsInt16Data),
21    DecimalAsInt32(FloatAsInt32Data),
22    Decimal(DecimalData),
23
24    ExecutionBlock(ExecutionBlockData),
25    RemoteExecution,
26
27    ShortText(ShortTextData),
28    Text(TextData),
29    True,
30    False,
31    Null,
32    ScopeStart,
33    ArrayStart,
34    ObjectStart,
35    TupleStart,
36    ScopeEnd,
37    KeyValueDynamic,
38    KeyValueShortText(ShortTextData),
39    CloseAndStore,
40    Add,
41    Subtract,
42    Multiply,
43    Divide,
44    Is,
45    StructuralEqual,
46    Equal,
47    NotStructuralEqual,
48    NotEqual,
49
50    CreateRef,
51
52    AllocateSlot(SlotAddress),
53    GetSlot(SlotAddress),
54    DropSlot(SlotAddress),
55    SetSlot(SlotAddress),
56}
57
58impl Display for Instruction {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            Instruction::Int8(data) => write!(f, "INT_8 {}", data.0),
62            Instruction::Int16(data) => write!(f, "INT_16 {}", data.0),
63            Instruction::Int32(data) => write!(f, "INT_32 {}", data.0),
64            Instruction::Int64(data) => write!(f, "INT_64 {}", data.0),
65            Instruction::Int128(data) => write!(f, "INT_128 {}", data.0),
66            Instruction::UInt128(data) => write!(f, "UINT_128 {}", data.0),
67            Instruction::Endpoint(data) => {
68                write!(f, "ENDPOINT {data}")
69            }
70
71            Instruction::DecimalAsInt16(data) => {
72                write!(f, "DECIMAL_AS_INT_16 {}", data.0)
73            }
74            Instruction::DecimalAsInt32(data) => {
75                write!(f, "DECIMAL_AS_INT_32 {}", data.0)
76            }
77            Instruction::DecimalF32(data) => {
78                write!(f, "DECIMAL_F32 {}", decimal_to_string(data.0, false))
79            }
80            Instruction::DecimalF64(data) => {
81                write!(f, "DECIMAL_F64 {}", decimal_to_string(data.0, false))
82            }
83            Instruction::Decimal(data) => {
84                write!(f, "DECIMAL_BIG {}", data.0)
85            }
86            Instruction::ShortText(data) => write!(f, "SHORT_TEXT {}", data.0),
87            Instruction::Text(data) => write!(f, "TEXT {}", data.0),
88            Instruction::True => write!(f, "TRUE"),
89            Instruction::False => write!(f, "FALSE"),
90            Instruction::Null => write!(f, "NULL"),
91            Instruction::ScopeStart => write!(f, "SCOPE_START"),
92            Instruction::ArrayStart => write!(f, "ARRAY_START"),
93            Instruction::ObjectStart => write!(f, "OBJECT_START"),
94            Instruction::TupleStart => write!(f, "TUPLE_START"),
95            Instruction::ScopeEnd => write!(f, "SCOPE_END"),
96            Instruction::KeyValueDynamic => write!(f, "KEY_VALUE_DYNAMIC"),
97            Instruction::KeyValueShortText(data) => {
98                write!(f, "KEY_VALUE_SHORT_TEXT {}", data.0)
99            }
100            Instruction::CloseAndStore => write!(f, "CLOSE_AND_STORE"),
101
102            // operations
103            Instruction::Add => write!(f, "ADD"),
104            Instruction::Subtract => write!(f, "SUBTRACT"),
105            Instruction::Multiply => write!(f, "MULTIPLY"),
106            Instruction::Divide => write!(f, "DIVIDE"),
107
108            // equality checks
109            Instruction::StructuralEqual => write!(f, "STRUCTURAL_EQUAL"),
110            Instruction::Equal => write!(f, "EQUAL"),
111            Instruction::NotStructuralEqual => {
112                write!(f, "NOT_STRUCTURAL_EQUAL")
113            }
114            Instruction::NotEqual => write!(f, "NOT_EQUAL"),
115            Instruction::Is => write!(f, "IS"),
116
117            Instruction::AllocateSlot(address) => {
118                write!(f, "ALLOCATE_SLOT {}", address.0)
119            }
120            Instruction::GetSlot(address) => {
121                write!(f, "GET_SLOT {}", address.0)
122            }
123            Instruction::DropSlot(address) => {
124                write!(f, "DROP_SLOT {}", address.0)
125            }
126            Instruction::SetSlot(address) => {
127                write!(f, "SET_SLOT {}", address.0)
128            }
129            Instruction::CreateRef => write!(f, "CREATE_REF"),
130            Instruction::ExecutionBlock(block) => {
131                write!(
132                    f,
133                    "EXECUTION_BLOCK (length: {}, injected_slot_count: {})",
134                    block.length, block.injected_slot_count
135                )
136            }
137            Instruction::RemoteExecution => write!(f, "REMOTE_EXECUTION"),
138        }
139    }
140}
141
142#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
143#[brw(little)]
144pub struct Int8Data(pub i8);
145
146#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
147#[brw(little)]
148pub struct Int16Data(pub i16);
149
150#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
151#[brw(little)]
152pub struct Int32Data(pub i32);
153
154#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
155#[brw(little)]
156pub struct Int64Data(pub i64);
157
158#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
159#[brw(little)]
160pub struct Int128Data(pub i128);
161
162#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
163#[brw(little)]
164pub struct UInt8Data(pub u8);
165
166#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
167#[brw(little)]
168pub struct UInt16Data(pub u16);
169
170#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
171#[brw(little)]
172pub struct UInt32Data(pub u32);
173
174#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
175#[brw(little)]
176pub struct UInt64Data(pub u64);
177
178#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
179#[brw(little)]
180pub struct UInt128Data(pub u128);
181
182#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
183#[brw(little)]
184pub struct Float32Data(pub f32);
185
186#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
187#[brw(little)]
188pub struct Float64Data(pub f64);
189
190#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
191#[brw(little)]
192pub struct FloatAsInt16Data(pub i16);
193
194#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
195#[brw(little)]
196pub struct FloatAsInt32Data(pub i32);
197
198#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
199#[brw(little)]
200pub struct DecimalData(pub Decimal);
201
202#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
203#[brw(little)]
204pub struct ShortTextDataRaw {
205    pub length: u8,
206    #[br(count = length)]
207    pub text: Vec<u8>,
208}
209
210#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
211#[brw(little)]
212pub struct TextDataRaw {
213    pub length: u32,
214    #[br(count = length)]
215    pub text: Vec<u8>,
216}
217
218#[derive(Clone, Debug, PartialEq)]
219pub struct ShortTextData(pub String);
220
221#[derive(Clone, Debug, PartialEq)]
222pub struct TextData(pub String);
223
224#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
225#[brw(little)]
226pub struct InstructionCloseAndStore {
227    pub instruction: Int8Data,
228}
229
230#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
231#[brw(little)]
232pub struct SlotAddress(pub u32);
233
234#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
235#[brw(little)]
236pub struct ExecutionBlockData {
237    pub length: u32,
238    pub injected_slot_count: u32,
239    #[br(count = injected_slot_count)]
240    pub injected_slots: Vec<u32>,
241    #[br(count = length)]
242    pub body: Vec<u8>,
243}