pub struct ErrorState<Error, Origin, Next> {
pub err: Error,
pub at: FailedAt<Origin, Next>,
}Expand description
Wraps an error with information about what state survived the failed transition.
ErrorState implements State, it can be used as a state in error recovery transitions.
Note that we do not depend on core::error::Error, for all our use cases requiring only
Debug is sufficient.
Fields§
§err: ErrorActual error
at: FailedAt<Origin, Next>Recovered state
Implementations§
Source§impl<E, Origin, Next> ErrorState<E, Origin, Next>
ErrorState implements constructors that ergonomically create Result::Err instances.
impl<E, Origin, Next> ErrorState<E, Origin, Next>
ErrorState implements constructors that ergonomically create Result::Err instances.
Sourcepub const fn err(err: E) -> Result<Next, Self>
pub const fn err(err: E) -> Result<Next, Self>
Error with no state recoverable state. The at field is set to None.
§Example
#[derive(Debug)]
enum MyError { Bad }
impl StateError for MyError {}
#[derive(Debug)]
struct Src;
impl State<MyError> for Src {}
#[derive(Debug)]
struct Dst;
impl State<MyError> for Dst {}
impl Transition<Dst, MyError> for Src {
fn change(self, _ctx: &dyn Any) -> TransitionResult<Src, Dst, MyError> {
// Change fails, source consumed, nothing survived
ErrorState::err(MyError::Bad)
}
}
let result = Src.try_transition();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(error.at, FailedAt::None));Sourcepub const fn err_origin(err: E, origin: Origin) -> Result<Next, Self>
pub const fn err_origin(err: E, origin: Origin) -> Result<Next, Self>
Error with origin state preserved, invariant/leave/guard failed.
The at field will contain FailedAt::Origin(self).
§Example
#[derive(Debug)]
enum MyError { Bad }
impl StateError for MyError {}
#[derive(Debug)]
struct Src;
impl State<MyError> for Src {}
#[derive(Debug)]
struct Dst;
impl State<MyError> for Dst {}
impl Transition<Dst, MyError> for Src {
fn change(self, _ctx: &dyn Any) -> TransitionResult<Src, Dst, MyError> {
// Change fails, origin state preserved for recovery
ErrorState::err_origin(MyError::Bad, self)
}
}
let result = Src.try_transition();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(error.at, FailedAt::Origin(_)));Examples found in repository?
133 fn change(self, item: &dyn Any) -> TransitionResult<HasCredit, Dispensing, VendError> {
134 let item = item.downcast_ref::<Item>().unwrap();
135 if self.amount >= item.cost {
136 Ok(Dispensing {
137 item: item.name,
138 price: item.cost,
139 change: self.amount - item.cost,
140 })
141 } else {
142 ErrorState::err_origin(VendError::NotEnoughCredit, self)
143 }
144 }Sourcepub const fn err_next(err: E, next: Next) -> Result<Next, Self>
pub const fn err_next(err: E, next: Next) -> Result<Next, Self>
Error with next state preserved, enter/invariant on target failed.
The at field will contain FailedAt::Next(next).
§Example
#[derive(Debug)]
enum MyError { Bad }
impl StateError for MyError {}
#[derive(Debug)]
struct Src;
impl State<MyError> for Src {}
#[derive(Debug)]
struct Dst;
impl State<MyError> for Dst {}
impl Transition<Dst, MyError> for Src {
fn change(self, _ctx: &dyn Any) -> TransitionResult<Src, Dst, MyError> {
let next = Dst;
// Target constructed but invalid; return it so caller can recover
ErrorState::err_next(MyError::Bad, next)
}
}
let result = Src.try_transition();
assert!(result.is_err());
let error = result.unwrap_err();
assert!(matches!(error.at, FailedAt::Next(_)));Trait Implementations§
Source§impl<Error: PartialEq, Origin: PartialEq, Next: PartialEq> PartialEq for ErrorState<Error, Origin, Next>
impl<Error: PartialEq, Origin: PartialEq, Next: PartialEq> PartialEq for ErrorState<Error, Origin, Next>
Source§impl<Error, Origin, Next> State for ErrorState<Error, Origin, Next>
Any ErrorState itself implements State<Infallible> thus they can be used in a
statemachine to recover from errors.
impl<Error, Origin, Next> State for ErrorState<Error, Origin, Next>
Any ErrorState itself implements State<Infallible> thus they can be used in a
statemachine to recover from errors.