laractions 0.1.0

Actions for Laravel Rust: encapsulate business logic in classes that run now or on the queue.
Documentation
use std::fmt;

/// A failure of the action machinery itself: an argument that does not fit `handle`, a rule that
/// the validator does not know, or an action that was never registered.
///
/// These are the errors PHP raises as `TypeError` and `LogicException`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ActionError {
    /// A value cannot become the parameter it was matched with.
    Argument {
        /// The action's name.
        action: String,
        /// The parameter, without a `$`.
        parameter: String,
        /// What serde said.
        reason: String,
    },
    /// A single array whose keys match none of `handle`'s parameters left one of them empty.
    UnboundArray {
        /// The action's name.
        action: String,
        /// The array's keys, up to five.
        keys: String,
        /// The parameters of `handle`.
        parameters: String,
        /// The parameter that was left empty.
        parameter: String,
    },
    /// A validation rule the validator cannot express.
    Rule {
        /// The action's name.
        action: String,
        /// The rule as written.
        rule: String,
    },
    /// No action is registered under this name.
    Unknown {
        /// The name that was asked for.
        name: String,
    },
    /// The model does not list the action under this key.
    NotDefined {
        /// The key that was asked for.
        key: String,
    },
}

impl fmt::Display for ActionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Argument {
                action,
                parameter,
                reason,
            } => write!(f, "{action}::handle() argument `{parameter}` is invalid: {reason}"),
            Self::UnboundArray {
                action,
                keys,
                parameters,
                parameter,
            } => write!(
                f,
                "{action}::handle() received a single array whose keys ({keys}) match none of its parameters ({parameters}), so `{parameter}` was left empty. If the array is meant as one argument, pass it by name: run(args! {{ {parameter}: .. }})."
            ),
            Self::Rule { action, rule } => write!(
                f,
                "{action} uses the validation rule `{rule}`, which Laractions does not support."
            ),
            Self::Unknown { name } => write!(
                f,
                "No action is registered as `{name}`. Did you forget `#[action]` on its impl?"
            ),
            Self::NotDefined { key } => write!(f, "Action `{key}` is not defined in the model's actions array."),
        }
    }
}

impl std::error::Error for ActionError {}