use-event-dispatch 0.1.0

Dispatch status and outcome primitives for RustUse events.
Documentation
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum DispatchStatus {
    Dispatched,
    Skipped,
    Failed,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DispatchOutcome {
    pub status: DispatchStatus,
    pub message: Option<String>,
}

impl DispatchOutcome {
    pub fn dispatched() -> Self {
        Self {
            status: DispatchStatus::Dispatched,
            message: None,
        }
    }

    pub fn skipped(message: impl Into<String>) -> Self {
        Self {
            status: DispatchStatus::Skipped,
            message: Some(message.into()),
        }
    }

    pub fn failed(message: impl Into<String>) -> Self {
        Self {
            status: DispatchStatus::Failed,
            message: Some(message.into()),
        }
    }

    pub const fn is_success(&self) -> bool {
        matches!(self.status, DispatchStatus::Dispatched)
    }
}

#[cfg(test)]
mod tests {
    use super::{DispatchOutcome, DispatchStatus};

    #[test]
    fn creates_successful_outcome() {
        let outcome = DispatchOutcome::dispatched();

        assert_eq!(outcome.status, DispatchStatus::Dispatched);
        assert_eq!(outcome.message, None);
        assert!(outcome.is_success());
    }

    #[test]
    fn creates_skipped_and_failed_outcomes() {
        let skipped = DispatchOutcome::skipped("filtered");
        let failed = DispatchOutcome::failed("handler error");

        assert_eq!(skipped.status, DispatchStatus::Skipped);
        assert_eq!(skipped.message.as_deref(), Some("filtered"));
        assert_eq!(failed.status, DispatchStatus::Failed);
        assert!(!failed.is_success());
    }
}