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
}
}