ready-active-safe 0.1.3

Lifecycle engine for externally driven systems
Documentation
//! Crate-level error types.

use core::fmt;

/// Errors that can occur during lifecycle operations.
///
/// All variants include enough context to diagnose the failure without
/// a debugger. Uses `#[non_exhaustive]` so new variants can be added
/// in minor versions without breaking downstream code.
///
/// # Examples
///
/// ```
/// use ready_active_safe::LifecycleError;
///
/// let err: LifecycleError<&str> = LifecycleError::TransitionDenied {
///     from: "active",
///     to: "ready",
///     reason: "backward transitions are not allowed",
/// };
/// assert_eq!(
///     err.to_string(),
///     "transition denied from active to ready: backward transitions are not allowed",
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LifecycleError<M> {
    /// A mode transition was rejected by the [`Policy`](crate::Policy).
    TransitionDenied {
        /// The mode the system was in when the transition was attempted.
        from: M,
        /// The mode the system attempted to transition to.
        to: M,
        /// A human-readable explanation of why the transition was denied.
        reason: &'static str,
    },
}

impl<M: fmt::Display> fmt::Display for LifecycleError<M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TransitionDenied { from, to, reason } => {
                write!(f, "transition denied from {from} to {to}: {reason}")
            }
        }
    }
}

#[cfg(feature = "std")]
impl<M: fmt::Debug + fmt::Display> std::error::Error for LifecycleError<M> {}