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
mod bytes;
mod serialize;
mod string;
use crate::Transition;
use console::network::prelude::*;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Execution<N: Network> {
edition: u16,
transitions: Vec<Transition<N>>,
}
impl<N: Network> Execution<N> {
pub fn new() -> Self {
Self { edition: N::EDITION, transitions: Vec::new() }
}
pub fn from(edition: u16, transitions: &[Transition<N>]) -> Result<Self> {
ensure!(!transitions.is_empty(), "Execution cannot initialize from empty list of transitions");
match edition == N::EDITION {
true => Ok(Self { edition, transitions: transitions.to_vec() }),
false => bail!("Execution cannot initialize with a different edition"),
}
}
pub const fn edition(&self) -> u16 {
self.edition
}
}
impl<N: Network> Execution<N> {
pub fn get(&self, index: usize) -> Result<Transition<N>> {
self.transitions.get(index).cloned().ok_or_else(|| anyhow!("Attempted to 'get' missing transition {index}"))
}
pub fn peek(&self) -> Result<Transition<N>> {
self.get(self.len() - 1)
}
pub fn push(&mut self, transition: Transition<N>) {
self.transitions.push(transition);
}
pub fn pop(&mut self) -> Result<Transition<N>> {
self.transitions.pop().ok_or_else(|| anyhow!("No more transitions in the execution"))
}
}
impl<N: Network> Execution<N> {
pub fn into_transitions(self) -> impl Iterator<Item = Transition<N>> {
self.transitions.into_iter()
}
}
impl<N: Network> Deref for Execution<N> {
type Target = [Transition<N>];
fn deref(&self) -> &Self::Target {
&self.transitions
}
}