use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use crate::copy::CopyFailureState;
use crate::copy::CopyStats;
use crate::copy::internal::CopyFailureParts;
use crate::error::FsError;
use crate::write::WriterRecovery;
#[must_use = "inspect or explicitly recover from the copy failure"]
pub struct CopyFailure {
parts: Box<CopyFailureParts>,
}
impl CopyFailure {
pub(crate) fn new(
error: FsError,
state: CopyFailureState,
partial_stats: CopyStats,
writer: Option<WriterRecovery>,
) -> Self {
Self {
parts: Box::new(CopyFailureParts {
error,
state,
partial_stats,
writer,
}),
}
}
#[inline]
#[must_use = "inspect the filesystem error instead of silently discarding it"]
pub const fn error(&self) -> &FsError {
&self.parts.error
}
#[inline]
pub const fn state(&self) -> CopyFailureState {
self.parts.state
}
#[inline]
#[must_use]
pub const fn partial_stats(&self) -> &CopyStats {
&self.parts.partial_stats
}
#[inline]
#[must_use]
pub const fn has_recovery(&self) -> bool {
self.parts.writer.is_some()
}
#[inline]
#[must_use = "inspect the recovery state before abandoning the retained writer"]
pub fn recovery(&self) -> Option<&WriterRecovery> {
self.parts.writer.as_ref()
}
#[inline]
#[must_use = "inspect the recovery state before abandoning the retained writer"]
pub fn recovery_mut(&mut self) -> Option<&mut WriterRecovery> {
self.parts.writer.as_mut()
}
#[inline]
pub fn take_recovery(&mut self) -> Option<WriterRecovery> {
self.parts.writer.take()
}
#[inline]
pub fn into_parts(self) -> (FsError, CopyFailureState, CopyStats, Option<WriterRecovery>) {
let mut parts = self.parts;
(parts.error, parts.state, parts.partial_stats, parts.writer.take())
}
}
impl Debug for CopyFailure {
#[inline]
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter
.debug_struct("CopyFailure")
.field("error", &self.parts.error)
.field("state", &self.parts.state)
.field("partial_stats", &self.parts.partial_stats)
.field("has_recovery", &self.parts.writer.is_some())
.finish()
}
}
impl Display for CopyFailure {
#[inline]
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
Display::fmt(self.error(), formatter)
}
}
impl Error for CopyFailure {
#[inline]
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(self.error())
}
}