use crate::ListenerError;
#[derive(Debug)]
#[must_use = "DispatchResult carries listener errors that will be silently dropped if ignored"]
pub struct DispatchResult {
results: Vec<Result<(), ListenerError>>,
blocked: bool,
listener_count: usize,
}
impl DispatchResult {
pub(crate) fn new(results: Vec<Result<(), ListenerError>>) -> Self {
let listener_count = results.len();
Self {
results,
blocked: false,
listener_count,
}
}
pub(crate) fn blocked() -> Self {
Self {
results: Vec::new(),
blocked: true,
listener_count: 0,
}
}
#[must_use]
pub fn is_blocked(&self) -> bool {
self.blocked
}
#[must_use]
pub fn listener_count(&self) -> usize {
self.listener_count
}
#[must_use]
pub fn success_count(&self) -> usize {
self.results.iter().filter(|r| r.is_ok()).count()
}
#[must_use]
pub fn error_count(&self) -> usize {
self.results.iter().filter(|r| r.is_err()).count()
}
#[must_use]
pub fn errors(&self) -> Vec<&ListenerError> {
self.results
.iter()
.filter_map(|r| r.as_ref().err())
.collect()
}
#[must_use]
pub fn all_succeeded(&self) -> bool {
!self.blocked && self.results.iter().all(|r| r.is_ok())
}
#[must_use]
pub fn has_errors(&self) -> bool {
self.results.iter().any(|r| r.is_err())
}
}