use crate::rtc::Storm;
use crate::sub::Sub;
#[derive(Clone, Debug)]
pub struct Boot<M: Machine> {
pub machine: M,
pub model: M::Model,
pub history: M::History,
pub cmd: M::Cmd,
}
impl<M: Machine> Boot<M> {
#[inline]
pub const fn new(machine: M, model: M::Model, history: M::History, cmd: M::Cmd) -> Self {
Self {
machine,
model,
history,
cmd,
}
}
}
#[doc(alias = "Elm")]
#[doc(alias = "TEA")]
#[doc(alias = "MVU")]
pub trait Machine {
type Flags;
type Model;
type Msg;
type Cmd;
type View;
type History;
type NodeId;
fn init(flags: Self::Flags) -> Boot<Self>
where
Self: Sized;
fn update(
&mut self,
model: &mut Self::Model,
history: &mut Self::History,
msg: Self::Msg,
) -> Self::Cmd;
fn try_update(
&mut self,
model: &mut Self::Model,
history: &mut Self::History,
msg: Self::Msg,
) -> Result<Self::Cmd, Storm> {
Ok(self.update(model, history, msg))
}
fn view(&self, model: &Self::Model) -> Self::View;
fn subscriptions(&self, _model: &Self::Model) -> Sub<Self::Msg> {
Sub::none()
}
fn in_state(&self, id: Self::NodeId) -> bool;
fn configuration(&self, out: &mut [Self::NodeId]) -> usize {
let _ = out;
0
}
fn project(&self) -> crate::bits::Bits {
crate::bits::Bits::EMPTY
}
}
#[inline]
pub fn apply<M: Machine>(
machine: &mut M,
model: &mut M::Model,
history: &mut M::History,
msg: M::Msg,
) -> M::Cmd {
machine.update(model, history, msg)
}
#[inline]
pub fn try_apply<M: Machine>(
machine: &mut M,
model: &mut M::Model,
history: &mut M::History,
msg: M::Msg,
) -> Result<M::Cmd, Storm> {
machine.try_update(model, history, msg)
}
#[inline]
pub fn step<M: Machine>(
mut machine: M,
mut model: M::Model,
mut history: M::History,
msg: M::Msg,
) -> (M, M::Model, M::History, M::Cmd) {
let cmd = machine.update(&mut model, &mut history, msg);
(machine, model, history, cmd)
}
#[inline]
#[allow(clippy::type_complexity)]
pub fn try_step<M: Machine>(
mut machine: M,
mut model: M::Model,
mut history: M::History,
msg: M::Msg,
) -> Result<(M, M::Model, M::History, M::Cmd), Storm> {
let cmd = machine.try_update(&mut model, &mut history, msg)?;
Ok((machine, model, history, cmd))
}