1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
mod decrement;
pub use decrement::*;
mod finalize;
pub use finalize::*;
mod increment;
pub use increment::*;
use crate::{program::Instruction, FinalizeRegisters, ProgramStorage, ProgramStore, Stack};
use console::network::prelude::*;
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Command<N: Network> {
Decrement(Decrement<N>),
Instruction(Instruction<N>),
Increment(Increment<N>),
}
impl<N: Network> Command<N> {
#[inline]
pub fn evaluate_finalize<P: ProgramStorage<N>>(
&self,
stack: &Stack<N>,
store: &ProgramStore<N, P>,
registers: &mut FinalizeRegisters<N>,
) -> Result<()> {
match self {
Command::Decrement(decrement) => decrement.evaluate_finalize(stack, store, registers),
Command::Instruction(_) => bail!("Instructions in 'finalize' are not supported (yet)."),
Command::Increment(increment) => increment.evaluate_finalize(stack, store, registers),
}
}
}
impl<N: Network> FromBytes for Command<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let variant = u8::read_le(&mut reader)?;
match variant {
0 => Ok(Self::Decrement(Decrement::read_le(&mut reader)?)),
1 => Ok(Self::Instruction(Instruction::read_le(&mut reader)?)),
2 => Ok(Self::Increment(Increment::read_le(&mut reader)?)),
3.. => Err(error(format!("Invalid command variant: {}", variant))),
}
}
}
impl<N: Network> ToBytes for Command<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
match self {
Self::Decrement(decrement) => {
0u8.write_le(&mut writer)?;
decrement.write_le(&mut writer)
}
Self::Instruction(instruction) => {
1u8.write_le(&mut writer)?;
instruction.write_le(&mut writer)
}
Self::Increment(increment) => {
2u8.write_le(&mut writer)?;
increment.write_le(&mut writer)
}
}
}
}
impl<N: Network> Parser for Command<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
alt((
map(Decrement::parse, |decrement| Self::Decrement(decrement)),
map(Instruction::parse, |instruction| Self::Instruction(instruction)),
map(Increment::parse, |increment| Self::Increment(increment)),
))(string)
}
}
impl<N: Network> FromStr for Command<N> {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network> Debug for Command<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for Command<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Decrement(decrement) => Display::fmt(decrement, f),
Self::Instruction(instruction) => Display::fmt(instruction, f),
Self::Increment(increment) => Display::fmt(increment, f),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_command_bytes() {
let expected = "decrement object[r0] by r1;";
let command = Command::<CurrentNetwork>::parse(expected).unwrap().1;
let bytes = command.to_bytes_le().unwrap();
assert_eq!(command, Command::from_bytes_le(&bytes).unwrap());
let expected = "add r0 r1 into r2;";
let command = Command::<CurrentNetwork>::parse(expected).unwrap().1;
let bytes = command.to_bytes_le().unwrap();
assert_eq!(command, Command::from_bytes_le(&bytes).unwrap());
let expected = "increment object[r0] by r1;";
let command = Command::<CurrentNetwork>::parse(expected).unwrap().1;
let bytes = command.to_bytes_le().unwrap();
assert_eq!(command, Command::from_bytes_le(&bytes).unwrap());
}
#[test]
fn test_command_parse() {
let expected = "decrement object[r0] by r1;";
let command = Command::<CurrentNetwork>::parse(expected).unwrap().1;
assert_eq!(Command::Decrement(Decrement::from_str(expected).unwrap()), command);
assert_eq!(expected, command.to_string());
let expected = "add r0 r1 into r2;";
let command = Command::<CurrentNetwork>::parse(expected).unwrap().1;
assert_eq!(Command::Instruction(Instruction::from_str(expected).unwrap()), command);
assert_eq!(expected, command.to_string());
let expected = "increment object[r0] by r1;";
let command = Command::<CurrentNetwork>::parse(expected).unwrap().1;
assert_eq!(Command::Increment(Increment::from_str(expected).unwrap()), command);
assert_eq!(expected, command.to_string());
}
}