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::error::FsError;
pub struct AsyncCopyFailure {
error: FsError,
state: CopyFailureState,
partial_stats: CopyStats,
}
impl AsyncCopyFailure {
pub(crate) fn new(error: FsError, state: CopyFailureState, partial_stats: CopyStats) -> Self {
Self {
error,
state,
partial_stats,
}
}
#[inline]
#[must_use]
pub const fn error(&self) -> &FsError {
&self.error
}
#[inline]
#[must_use]
pub const fn state(&self) -> CopyFailureState {
self.state
}
#[inline]
#[must_use]
pub const fn partial_stats(&self) -> &CopyStats {
&self.partial_stats
}
#[inline]
#[must_use]
pub fn into_parts(self) -> (FsError, CopyFailureState, CopyStats) {
(self.error, self.state, self.partial_stats)
}
}
impl Debug for AsyncCopyFailure {
#[inline]
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter
.debug_struct("AsyncCopyFailure")
.field("error", &self.error)
.field("state", &self.state)
.field("partial_stats", &self.partial_stats)
.finish()
}
}
impl Display for AsyncCopyFailure {
#[inline]
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
Display::fmt(self.error(), formatter)
}
}
impl Error for AsyncCopyFailure {
#[inline]
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(self.error())
}
}
#[cfg(test)]
mod tests {
use super::AsyncCopyFailure;
use crate::copy::CopyFailureState;
use crate::copy::CopyStats;
use crate::error::FsError;
use crate::error::FsErrorKind;
use crate::error::FsOperation;
#[test]
fn owned_parts_are_executed_at_runtime() {
let failure = AsyncCopyFailure::new(
FsError::new(FsErrorKind::NotFound, FsOperation::Copy, "missing source"),
CopyFailureState::Unchanged,
CopyStats {
files: 1,
..CopyStats::default()
},
);
let (error, state, stats) = failure.into_parts();
assert_eq!(error.kind(), FsErrorKind::NotFound);
assert_eq!(state, CopyFailureState::Unchanged);
assert_eq!(stats.files, 1);
}
}