use std::error::Error;
use std::fmt;
use crate::Progress;
use crate::error::CompletionError;
use crate::error::TerminalError;
#[allow(clippy::large_enum_variant)]
pub enum RecoverableFinishError<'reporter> {
Incomplete {
progress: Progress<'reporter>,
source: CompletionError,
},
Terminal(TerminalError),
}
impl<'reporter> RecoverableFinishError<'reporter> {
#[must_use]
pub fn completion_error(&self) -> Option<&CompletionError> {
match self {
Self::Incomplete { source, .. } => Some(source),
Self::Terminal(_) => None,
}
}
pub fn into_progress(self) -> Result<Progress<'reporter>, TerminalError> {
match self {
Self::Incomplete { progress, .. } => Ok(progress),
Self::Terminal(error) => Err(error),
}
}
pub fn into_parts(
self,
) -> Result<(Progress<'reporter>, CompletionError), TerminalError> {
match self {
Self::Incomplete { progress, source } => Ok((progress, source)),
Self::Terminal(error) => Err(error),
}
}
}
impl fmt::Debug for RecoverableFinishError<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Incomplete { source, .. } => formatter
.debug_struct("RecoverableFinishError::Incomplete")
.field("source", source)
.finish(),
Self::Terminal(error) => formatter
.debug_tuple("RecoverableFinishError::Terminal")
.field(error)
.finish(),
}
}
}
impl fmt::Display for RecoverableFinishError<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Incomplete { source, .. } => source.fmt(formatter),
Self::Terminal(error) => error.fmt(formatter),
}
}
}
impl Error for RecoverableFinishError<'_> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Incomplete { source, .. } => Some(source),
Self::Terminal(error) => Some(error),
}
}
}