#[cfg(doc)]
use crate::*;
#[cfg(all(doc, not(doctest)))]
#[macro_export]
macro_rules! essm {
(state $name:ident;) => {};
(state $name:ident { $($field:ident: $ty:ty),* $(,)? }) => {};
(state $name:ident ( $($ty:ty),* $(,)? );) => {};
(state $name:ident < $($generics:tt)* > { $($fields:tt)* }) => {};
(state $name:ident < $($generics:tt)* > where $($where:tt)* { $($fields:tt)* }) => {};
(state $name:ident; impl { $($body:tt)* }) => {};
(state $name:ident { $($fields:tt)* }; impl { $($body:tt)* }) => {};
(state $name:ident < $($generics:tt)* > { $($fields:tt)* }; impl { $($body:tt)* }) => {};
($vis:vis state $($rest:tt)*) => {};
}
#[doc(hidden)]
#[cfg(any(not(doc), doctest))]
#[macro_export]
macro_rules! essm {
($(#[$($mattr:tt)*])* $vis:vis state$(<$err:ty>)? $name:ident; $($rest:tt)*) => {
$crate::essm!{
@unify_derive()
$(#[$($mattr)*])* $vis struct $name ;
}
$crate::essm! {
@state_impl($(<$err>)? $name)
$($rest)*
}
};
($(#[$($mattr:tt)*])* $vis:vis state$(<$err:ty>)? $name:ident{} $($rest:tt)*) => {
$crate::essm!{
@unify_derive()
$(#[$($mattr)*])* $vis struct $name{}
}
$crate::essm! {
@state_impl($(<$err>)? $name)
$($rest)*
}
};
($(#[$($mattr:tt)*])* $vis:vis state$(<$err:ty>)? $name:ident(); $($rest:tt)*) => {
$crate::essm!{
@unify_derive()
$(#[$($mattr)*])* $vis struct $name();
}
$crate::essm! {
@state_impl($(<$err>)? $name)
$($rest)*
}
};
(
$(#[$($mattr:tt)*])*
$vis:vis
state$(<$err:ty>)? $name:ident
$(<
$($lifetimes:lifetime),*$(,)?
$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)? $(= $default:ty)?),*$(,)?
>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
{$($mvis:vis $entry:ident: $type:ty),+}
$($rest:tt)*
) => {
$(#[$($mattr)*])*
$vis
struct $name
$(<
$($lifetimes,)*
$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)? $(= $default)?),*
>)?
$(where
$($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{
$($mvis $entry: $type),+
}
$crate::essm! {
@state_impl($(<$err>)? $name $(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$($(where $where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
)
$($rest)*
}
};
(
$(#[$($mattr:tt)*])*
$vis:vis
state$(<$err:ty>)? $name:ident
$(<
$($lifetimes:lifetime),*$(,)?
$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)? $(= $default:ty)?),*$(,)?
>)?
($($mvis:vis $type:ty),+)
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)? ;
$($rest:tt)*
) => {
$(#[$($mattr)*])*
$vis
struct $name
$(<
$($lifetimes,)*
$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)? $(= $default)?),*
>)?
(
$($mvis $type),+
)
$(where
$($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)? ;
$crate::essm! {
@state_impl($(<$err>)? $name $(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$($(where $where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
)
$($rest)*
}
};
($(#[$($mattr:tt)*])* $vis:vis state $($here:tt $($rest:tt)*)?) => {
compile_error!(concat!( "essm macro invalid syntax at: state ", $(stringify!($here), " ...")?))
};
(transition
$(<
$($lifetimes:lifetime),*$(,)?
$terror:ty$(,$oerror:ty$(,$nerror:ty
$(,$generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*$(,)?
)?)?
>)?
$org:ident$(::$orgcont:ident)*
$(<
$($orglt:lifetime),*$(,)?$($orggen:ident),*$(,)?
>)?
->
$next:ident$(::$nextcont:ident)*
$(<
$($nextlt:lifetime),*$(,)?$($nextgen:ident),*$(,)?
>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
; $($rest:tt)*
) => {
impl
$(<
$($lifetimes,)*
$($($($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*)?)?
>)?
$crate::Transition<$next$(::$nextcont)*$(<$($nextlt),* $($nextgen),*>)?$(,$terror$(,$oerror$(,$nerror)?)?)?>
for $org$(::$orgcont)*$(<$($orglt),* $($orggen),*>)?
where $next$(::$nextcont)*$(<$($nextlt),* $($nextgen),*>)?: Default,
$($($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{
fn change(self, _: &dyn Any)
-> $crate::TransitionResult<$org$(::$orgcont)*$(<$($orglt),* $($orggen),*>)?,
$next$(::$nextcont)*$(<$($nextlt),* $($nextgen),*>)?
$(,$terror)?
>
{
Ok(<$next$(::$nextcont)*$(<$($nextlt),* $($nextgen),*>)?>::default())
}
}
$crate::essm! {$($rest)*}
};
(transition $($here:tt $($rest:tt)*)?) => {
compile_error!(concat!( "essm macro invalid syntax at: transition ", $(stringify!($here), " ...")?))
};
(initial
$(<
$($lifetimes:lifetime),*$(,)?
$error:ty
$(,$generic:ident$(: $genfirst:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*$(,)?
>)?
$name:ident$(::$namecont:ident)*$(<$($namelt:lifetime),*$(,)?$($namegen:ident),*$(,)?>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
;
$($rest:tt)*
) => {
impl$(<
$($lifetimes,)*
$($generic$(: $genfirst $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*
>)?
$crate::Transition<$name$(::$namecont)*$(<$($namelt),* $($namegen),*>)? $(,$error)?>
for $crate::Primordial
where
$name$(::$namecont)*$(<$($namelt),* $($namegen),*>)?: Debug + Default,
$($($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{
fn change(self, _: &dyn Any) -> $crate::essm!{
@complete_transitionresult($crate::Primordial, $name$(::$namecont)*$(<$($namelt),* $($namegen),*>)?) $($error)?
}
{
Ok(<$name$(::$namecont)*$(<$($namelt),* $($namegen),*>)?>::default())
}
}
$crate::essm! {$($rest)*}
};
(initial $($here:tt $($rest:tt)*)?) => {
compile_error!(concat!( "essm macro invalid syntax at: initial ", $(stringify!($here), " ...")?))
};
(terminal
$(<
$($lifetimes:lifetime),*$(,)?
$error:ty
$(,$generic:ident$(: $genfirst:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*$(,)?
>)?
$name:ident$(::$namecont:ident)*$(<$($namelt:lifetime),*$(,)?$($namegen:ident),*$(,)?>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
;
$($rest:tt)*
) => {
impl$(<
$($lifetimes,)*
$($generic$(: $genfirst $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*
>)?
$crate::Transition<$crate::Terminated $(,$error)?>
for $name$(::$namecont)*$(<$($namelt),* $($namegen),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{
fn change(self, _: &dyn Any) -> $crate::essm!{
@complete_transitionresult($name$(::$namecont)*$(<$($namelt),* $($namegen),*>)?, $crate::Terminated) $($error)?
}
{
Ok($crate::Terminated)
}
}
$crate::essm! {$($rest)*}
};
(terminal $($here:tt $($rest:tt)*)?) => {
compile_error!(concat!( "essm macro invalid syntax at: terminal ", $(stringify!($here), " ...")?))
};
(
$(#[$($mattr:tt)*])*
$vis:vis
error $name:ident
$(<
$($lifetimes:lifetime),*$(,)?
$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)? $(= $default:ty)?),*$(,)?
>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
;
$($rest:tt)*
) => {
$(#[$($mattr)*])*
$vis
struct $name
$(<
$($lifetimes,)*
$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)? $(= $default)?),*
>)?
$(where
$($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
;
impl StateError for $name
$(<
$($lifetimes,)*
$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*
>)?
$(where
$($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{}
$crate::essm! {$($rest)*}
};
(
$(#[$($mattr:tt)*])*
$vis:vis
error $($here:tt $($rest:tt)*)?
) => {
compile_error!(concat!( "essm macro invalid syntax at: error ", $(stringify!($here), " ...")?))
};
(
impl $($here:tt $($rest:tt)*)?
) => {
compile_error!(concat!( "essm macro invalid syntax at: impl ", $(stringify!($here), " ...")?))
};
(@state_impl($(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
) impl { $($body:tt)* } $($rest:tt)*) => {
$crate::essm! {
@filter_methods
{} {}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($body)*
}
$crate::essm! { $($rest)* }
};
(@state_impl($(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where
$($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
) $($rest:tt)*) => {
impl $(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$crate::State$(<$err>)? for
$name $(<$($lifetimes,)*$($generic),*>)?
$(where
$($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{}
$crate::essm! { $($rest)* }
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
fn invariant(&self) -> Result {$($body:tt)*}
$($rest:tt)*
) => {
$crate::essm! {
@filter_methods
{
$($state_acc)*
fn invariant(&self) -> $crate::essm!{@complete_result $($err)?} {$($body)*}
}
{$($impl_acc)*}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($rest)*
}
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
fn enter(&self) -> Result {$($body:tt)*}
$($rest:tt)*
) => {
$crate::essm! {
@filter_methods
{
$($state_acc)*
fn enter(&self, _ctx: &dyn Any) -> $crate::essm!{@complete_result $($err)?} {
debug_assert!(_ctx.is::<()>(), "enter(&self) called with non unit context");
$($body)*
}
}
{$($impl_acc)*}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($rest)*
}
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
fn enter(&self, $ctx:ident $(: &dyn Any)?) -> Result {$($body:tt)*}
$($rest:tt)*
) => {
$crate::essm! {
@filter_methods
{
$($state_acc)*
fn enter(&self, $ctx: &dyn Any) -> $crate::essm!{@complete_result $($err)?} {$($body)*}
}
{$($impl_acc)*}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($rest)*
}
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
fn leave(&self) -> Result {$($body:tt)*}
$($rest:tt)*
) => {
$crate::essm! {
@filter_methods
{
$($state_acc)*
fn leave(&self, _ctx: &dyn Any) -> $crate::essm!{@complete_result $($err)?} {
debug_assert!(_ctx.is::<()>(), "leave(&self) called with non unit context");
$($body)*
}
}
{$($impl_acc)*}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($rest)*
}
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
fn leave(&self, $ctx:ident $(: &dyn Any)?) -> Result {$($body:tt)*}
$($rest:tt)*
) => {
$crate::essm! {
@filter_methods
{
$($state_acc)*
fn leave(&self, $ctx: &dyn Any) -> $crate::essm!{@complete_result $($err)?} {$($body)*}
}
{$($impl_acc)*}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($rest)*
}
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
fn $fnname:ident$(<$($fngenerics:tt)+>)? ($($fnargs:tt)*) $(-> $return:ty)?
$(where $($fnwhere:ident: $fnwfirst:tt $(:: $fnwrest:ident)* $(+ $fnwmore:tt $(:: $fnwmrest:ident)*)*),*$(,)?)?
{$($body:tt)*}
$($rest:tt)*
) => {
$crate::essm! {
@filter_methods
{$($state_acc)*}
{
$($impl_acc)*
fn $fnname $(<$($fngenerics)+>)? ($($fnargs)*) $(-> $return)?
$(where $($fnwhere: $fnwfirst $(:: $fnwrest)* $(+ $fnwmore $(:: $fnwmrest)*)*),*)?
{$($body)*}
}
$(<$err>)? $name
$(<$($lifetimes,)*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
$($rest)*
}
};
(@filter_methods
{$($state_acc:tt)*} {$($impl_acc:tt)*}
$(<$err:ty>)? $name:ident
$(<$($lifetimes:lifetime),*$(,)?$($generic:ident$(: $first:tt $(:: $prest:ident)* $(+ $more:tt $(:: $pmrest:ident)*)*)?),*>)?
$(where $($where:ident: $wfirst:tt $(:: $wrest:ident)* $(+ $wmore:tt $(:: $wmrest:ident)*)*),*$(,)?)?
) => {
impl$(<$($lifetimes),*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$crate::State$(<$err>)? for $name$(<$($lifetimes),*$($generic),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{$($state_acc)*}
impl$(<$($lifetimes),*$($generic$(: $first $(:: $prest)* $(+ $more $(:: $pmrest)*)*)?),*>)?
$name$(<$($lifetimes),*$($generic),*>)?
$(where $($where: $wfirst $(:: $wrest)* $(+ $wmore $(:: $wmrest)*)*),*)?
{$($impl_acc)*}
};
(@complete_result $err:ty) => {
Result<(), $err>
};
(@complete_result) => {
Result<(), Infallible>
};
(@complete_transitionresult($origin:ty, $next:ty) $($err:ty)?) => {
$crate::TransitionResult<$origin, $next $(,$err)?>
};
(@unify_derive($($acc:tt)*) $vis:vis struct $($structdef:tt)*) => {
$($acc)*
#[derive(Debug, Default)]
$vis struct $($structdef)*
};
(@unify_derive($($acc:tt)*) #[derive($($derive:tt)*)] $($rest:tt)*) => {
$crate::essm!{
@filter_derive($($acc)*) () $($derive)* @end $($rest)*
}
};
(@unify_derive($($acc:tt)*) #[$(other:tt)*] $($rest:tt)*) => {
$crate::essm!{
@unify_derive($($acc)* #[$(other)*])
$($rest)*
}
};
(@filter_derive($($acc:tt)*) () @end $($rest:tt)*) => {
$crate::essm!{
@unify_derive($($acc)*)
$($rest)*
}
};
(@filter_derive($($acc:tt)*) ($($derive:tt)+) @end $($rest:tt)*) => {
$crate::essm!{
@unify_derive($($acc)* #[derive($($derive)+)])
$($rest)*
}
};
(@filter_derive($($acc:tt)*) ($($derive:tt)*) Default $($rest:tt)*) => {
$crate::essm!{
@filter_derive($($acc)*) ($($derive)*) $($rest)*
}
};
(@filter_derive($($acc:tt)*) ($($derive:tt)*) Debug $($rest:tt)*) => {
$crate::essm!{
@filter_derive($($acc)*) ($($derive)*) $($rest)*
}
};
(@filter_derive($($acc:tt)*) ($($derive:tt)*) $trait:ident $($rest:tt)*) => {
$crate::essm!{
@filter_derive($($acc)*) ($($derive)* $trait ,) $($rest)*
}
};
(@filter_derive($($acc:tt)*) ($($derive:tt)*) , $($rest:tt)*) => {
$crate::essm!{
@filter_derive($($acc)*) ($($derive)*) $($rest)*
}
};
(@remove_token($_:ty)($($acc:ty),*) $($list:ty),*) => {
($($list)*)
};
(@default_to($($_:tt)*) $($else:tt)+) => {
$($else)+
};
(@default_to($($this:tt)*) ) => {
$($this)*
};
() => {};
(
$($here:tt $($rest:tt)*)?
) => {
compile_error!(concat!( "essm macro syntax error at: ", $(stringify!($here), " ...")?))
};
}