mod_events/
result.rs

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