use std::time::Duration;
use serde::{Deserialize, Serialize};
pub trait StateMachine {
type MessageBody;
type Err: IsCritical;
type Output;
fn handle_incoming(&mut self, msg: Msg<Self::MessageBody>) -> Result<(), Self::Err>;
fn message_queue(&mut self) -> &mut Vec<Msg<Self::MessageBody>>;
fn wants_to_proceed(&self) -> bool;
fn proceed(&mut self) -> Result<(), Self::Err>;
fn round_timeout(&self) -> Option<Duration>;
fn round_timeout_reached(&mut self) -> Self::Err;
fn is_finished(&self) -> bool;
fn pick_output(&mut self) -> Option<Result<Self::Output, Self::Err>>;
fn current_round(&self) -> u16;
fn total_rounds(&self) -> Option<u16>;
fn party_ind(&self) -> u16;
fn parties(&self) -> u16;
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Msg<B> {
pub sender: u16,
pub receiver: Option<u16>,
pub body: B,
}
impl<B> Msg<B> {
pub fn map_body<T, F>(self, f: F) -> Msg<T>
where
F: FnOnce(B) -> T,
{
Msg {
sender: self.sender,
receiver: self.receiver,
body: f(self.body),
}
}
}
pub trait IsCritical {
fn is_critical(&self) -> bool;
}