Skip to main content

mod_events/
result.rs

1//! Event dispatch result types
2
3use crate::ListenerError;
4
5/// Result of event dispatch.
6///
7/// Contains information about the success or failure of event dispatch,
8/// including any errors that occurred during listener execution.
9#[derive(Debug)]
10#[must_use = "DispatchResult carries listener errors that will be silently dropped if ignored"]
11pub struct DispatchResult {
12    results: Vec<Result<(), ListenerError>>,
13    blocked: bool,
14    listener_count: usize,
15}
16
17impl DispatchResult {
18    pub(crate) fn new(results: Vec<Result<(), ListenerError>>) -> Self {
19        let listener_count = results.len();
20        Self {
21            results,
22            blocked: false,
23            listener_count,
24        }
25    }
26
27    pub(crate) fn blocked() -> Self {
28        Self {
29            results: Vec::new(),
30            blocked: true,
31            listener_count: 0,
32        }
33    }
34
35    /// Check if the event was blocked by middleware.
36    #[must_use]
37    pub fn is_blocked(&self) -> bool {
38        self.blocked
39    }
40
41    /// Get the total number of listeners that were called.
42    #[must_use]
43    pub fn listener_count(&self) -> usize {
44        self.listener_count
45    }
46
47    /// Get the number of successful handlers.
48    #[must_use]
49    pub fn success_count(&self) -> usize {
50        self.results.iter().filter(|r| r.is_ok()).count()
51    }
52
53    /// Get the number of failed handlers.
54    #[must_use]
55    pub fn error_count(&self) -> usize {
56        self.results.iter().filter(|r| r.is_err()).count()
57    }
58
59    /// Borrow every error produced by failing listeners, in dispatch order.
60    #[must_use]
61    pub fn errors(&self) -> Vec<&ListenerError> {
62        self.results
63            .iter()
64            .filter_map(|r| r.as_ref().err())
65            .collect()
66    }
67
68    /// Check if all handlers succeeded.
69    ///
70    /// Returns `false` if any listener errored or if the event was
71    /// blocked by middleware.
72    #[must_use]
73    pub fn all_succeeded(&self) -> bool {
74        !self.blocked && self.results.iter().all(|r| r.is_ok())
75    }
76
77    /// Check if any handlers failed.
78    #[must_use]
79    pub fn has_errors(&self) -> bool {
80        self.results.iter().any(|r| r.is_err())
81    }
82}