use std::{
fmt,
time::Duration,
};
use qubit_progress::model::ProgressCounters;
use crate::{
BatchOutcomeBuilder,
BatchTaskFailure,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BatchOutcome<E> {
task_count: usize,
completed_count: usize,
succeeded_count: usize,
failed_count: usize,
panicked_count: usize,
elapsed: Duration,
failures: Vec<BatchTaskFailure<E>>,
}
impl<E> BatchOutcome<E> {
#[inline]
pub(crate) fn new(builder: BatchOutcomeBuilder<E>) -> Self {
Self {
task_count: builder.task_count,
completed_count: builder.completed_count,
succeeded_count: builder.succeeded_count,
failed_count: builder.failed_count,
panicked_count: builder.panicked_count,
elapsed: builder.elapsed,
failures: builder.failures,
}
}
#[inline]
pub const fn task_count(&self) -> usize {
self.task_count
}
#[inline]
pub const fn completed_count(&self) -> usize {
self.completed_count
}
#[inline]
pub const fn succeeded_count(&self) -> usize {
self.succeeded_count
}
#[inline]
pub const fn failed_count(&self) -> usize {
self.failed_count
}
#[inline]
pub const fn panicked_count(&self) -> usize {
self.panicked_count
}
#[inline]
pub const fn failure_count(&self) -> usize {
self.failed_count + self.panicked_count
}
#[inline]
pub fn progress_counters(&self) -> ProgressCounters {
ProgressCounters::new(Some(self.task_count()))
.with_completed_count(self.completed_count())
.with_succeeded_count(self.succeeded_count())
.with_failed_count(self.failure_count())
}
#[inline]
pub const fn elapsed(&self) -> Duration {
self.elapsed
}
#[inline]
pub fn failures(&self) -> &[BatchTaskFailure<E>] {
self.failures.as_slice()
}
#[inline]
pub const fn is_success(&self) -> bool {
self.completed_count == self.task_count
&& self.failed_count == 0
&& self.panicked_count == 0
}
#[inline]
pub fn into_failures(self) -> Vec<BatchTaskFailure<E>> {
self.failures
}
}
impl<E> fmt::Display for BatchOutcome<E> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"BatchOutcome {{ task_count: {}, completed_count: {}, succeeded_count: {}, failed_count: {}, panicked_count: {}, elapsed: {:?} }}",
self.task_count(),
self.completed_count(),
self.succeeded_count(),
self.failed_count(),
self.panicked_count(),
self.elapsed(),
)
}
}