use std::fmt::Debug;
pub struct Transition<S, E>
where
S: PartialEq, E: PartialEq, {
pub event: E,
pub state_in: S,
pub state_out: S,
pub guard: Option<fn() -> bool>,
}
impl<S, E> Transition<S, E>
where
S: PartialEq,
E: PartialEq,
{
pub fn new(input: S, event: E, output: S) -> Transition<S, E> {
Self {
event,
state_in: input,
state_out: output,
guard: None
}
}
pub fn with_guard(mut self, guard: Option<fn() -> bool>) -> Transition<S, E> {
self.guard = guard;
self
}
pub fn is_allowed(&self) -> bool {
match &self.guard {
None => true,
Some(function) => function(),
}
}
pub fn partial_compare(&self, input: Option<&S>, event: Option<&E>, output: Option<&S>) -> bool {
(event.is_none() || &self.event == event.unwrap())
&& (input.is_none() || &self.state_in == input.unwrap())
&& (output.is_none() || &self.state_out == output.unwrap())
}
}
impl<S, E> PartialEq for Transition<S, E>
where
S: PartialEq + Debug,
E: PartialEq + Debug,
{
fn eq(&self, other: &Transition<S, E>) -> bool {
self.event == other.event
&& self.state_in == other.state_in
&& self.state_out == other.state_out
}
}
impl<S, E> Debug for Transition<S, E>
where
S: PartialEq + Debug,
E: PartialEq + Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Transition")
.field("event", &self.event)
.field("state_in", &self.state_in)
.field("state_out", &self.state_out)
.finish()
}
}