use crate::error::NoError;
pub trait ActionMap<State, FromAction> {
type ToAction;
type ActionMapError;
type ToActions<'a>: IntoIterator<Item = Result<Self::ToAction, Self::ActionMapError>>
where
Self: 'a,
Self::ToAction: 'a,
Self::ActionMapError: 'a,
State: 'a,
FromAction: 'a;
fn map_action<'a>(&'a self, from_state: State, from_action: FromAction) -> Self::ToActions<'a>
where
FromAction: 'a,
State: 'a;
}
pub struct ActionInto<ToAction> {
_ignore: std::marker::PhantomData<ToAction>,
}
impl<ToAction> ActionInto<ToAction> {
pub fn new() -> Self {
Self {
_ignore: Default::default(),
}
}
}
impl<S, FromAction, ToAction> ActionMap<S, FromAction> for ActionInto<ToAction>
where
FromAction: Into<ToAction>,
{
type ToAction = ToAction;
type ActionMapError = NoError;
type ToActions<'a>
= std::option::IntoIter<Result<ToAction, NoError>>
where
ToAction: 'a,
S: 'a,
FromAction: 'a;
fn map_action<'a>(&'a self, _: S, from_action: FromAction) -> Self::ToActions<'a>
where
FromAction: 'a,
S: 'a,
{
Some(Ok(from_action.into())).into_iter()
}
}
pub struct MaybeActionInto<ToAction> {
_ignore: std::marker::PhantomData<ToAction>,
}
impl<ToAction> MaybeActionInto<ToAction> {
pub fn new() -> Self {
Self {
_ignore: Default::default(),
}
}
}
impl<S, FromAction, ToAction> ActionMap<S, FromAction> for MaybeActionInto<ToAction>
where
FromAction: Into<Option<ToAction>>,
{
type ToAction = ToAction;
type ActionMapError = NoError;
type ToActions<'a>
= std::option::IntoIter<Result<ToAction, NoError>>
where
ToAction: 'a,
S: 'a,
FromAction: 'a;
fn map_action<'a>(&'a self, _: S, from_action: FromAction) -> Self::ToActions<'a>
where
FromAction: 'a,
S: 'a,
{
Ok(from_action.into()).transpose().into_iter()
}
}
pub struct NoActionMap;
impl<State, Action> ActionMap<State, Action> for NoActionMap {
type ActionMapError = NoError;
type ToAction = Action;
type ToActions<'a>
= Option<Result<Action, NoError>>
where
State: 'a,
Action: 'a;
fn map_action<'a>(&'a self, _: State, from_action: Action) -> Self::ToActions<'a>
where
Action: 'a,
State: 'a,
{
Some(Ok(from_action))
}
}