Skip to main content

use_event_dispatch/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub enum DispatchStatus {
6    Dispatched,
7    Skipped,
8    Failed,
9}
10
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct DispatchOutcome {
13    pub status: DispatchStatus,
14    pub message: Option<String>,
15}
16
17impl DispatchOutcome {
18    pub fn dispatched() -> Self {
19        Self {
20            status: DispatchStatus::Dispatched,
21            message: None,
22        }
23    }
24
25    pub fn skipped(message: impl Into<String>) -> Self {
26        Self {
27            status: DispatchStatus::Skipped,
28            message: Some(message.into()),
29        }
30    }
31
32    pub fn failed(message: impl Into<String>) -> Self {
33        Self {
34            status: DispatchStatus::Failed,
35            message: Some(message.into()),
36        }
37    }
38
39    pub const fn is_success(&self) -> bool {
40        matches!(self.status, DispatchStatus::Dispatched)
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::{DispatchOutcome, DispatchStatus};
47
48    #[test]
49    fn creates_successful_outcome() {
50        let outcome = DispatchOutcome::dispatched();
51
52        assert_eq!(outcome.status, DispatchStatus::Dispatched);
53        assert_eq!(outcome.message, None);
54        assert!(outcome.is_success());
55    }
56
57    #[test]
58    fn creates_skipped_and_failed_outcomes() {
59        let skipped = DispatchOutcome::skipped("filtered");
60        let failed = DispatchOutcome::failed("handler error");
61
62        assert_eq!(skipped.status, DispatchStatus::Skipped);
63        assert_eq!(skipped.message.as_deref(), Some("filtered"));
64        assert_eq!(failed.status, DispatchStatus::Failed);
65        assert!(!failed.is_success());
66    }
67}