#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Outcome<S, C> {
Handled,
Super,
Transition {
to: S,
cmd: C,
},
Internal(C),
}
impl<S, C> Outcome<S, C> {
#[inline]
pub const fn handled() -> Self {
Self::Handled
}
#[inline]
pub const fn super_() -> Self {
Self::Super
}
#[inline]
pub const fn transition(to: S, cmd: C) -> Self {
Self::Transition { to, cmd }
}
#[inline]
pub const fn internal(cmd: C) -> Self {
Self::Internal(cmd)
}
#[inline]
pub const fn defers(&self) -> bool {
matches!(self, Self::Super)
}
pub fn or_else(self, parent: impl FnOnce() -> Self) -> Self {
match self {
Self::Super => parent(),
other => other,
}
}
pub fn into_parts(self) -> (Option<S>, C)
where
C: crate::combine::Combine,
{
match self {
Self::Handled | Self::Super => (None, C::none()),
Self::Internal(cmd) => (None, cmd),
Self::Transition { to, cmd } => (Some(to), cmd),
}
}
pub fn map_target<T>(self, f: impl FnOnce(S) -> T) -> Outcome<T, C> {
match self {
Self::Handled => Outcome::Handled,
Self::Super => Outcome::Super,
Self::Transition { to, cmd } => Outcome::Transition { to: f(to), cmd },
Self::Internal(cmd) => Outcome::Internal(cmd),
}
}
pub fn map_cmd<D>(self, f: impl FnOnce(C) -> D) -> Outcome<S, D> {
match self {
Self::Handled => Outcome::Handled,
Self::Super => Outcome::Super,
Self::Transition { to, cmd } => Outcome::Transition { to, cmd: f(cmd) },
Self::Internal(cmd) => Outcome::Internal(f(cmd)),
}
}
}