use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use crate::error::FsError;
use crate::path::Path;
use crate::temp::PersistFailureState;
#[derive(Debug)]
pub struct PersistFailure {
error: FsError,
state: PersistFailureState,
publication_target: Option<Path>,
}
impl PersistFailure {
#[inline]
#[must_use]
pub fn new(error: FsError, state: PersistFailureState) -> Self {
Self {
error,
state,
publication_target: None,
}
}
pub(crate) fn with_publication_target(mut self, target: Option<&Path>) -> Self {
self.publication_target = target.cloned();
self
}
#[inline]
#[must_use]
pub const fn state(&self) -> PersistFailureState {
self.state
}
#[inline]
#[must_use]
pub const fn error(&self) -> &FsError {
&self.error
}
#[inline]
#[must_use]
pub fn publication_target(&self) -> Option<&Path> {
self.publication_target.as_ref()
}
#[inline]
#[must_use]
pub fn into_error(self) -> FsError {
self.error
}
#[inline]
#[must_use]
pub fn into_parts(self) -> (FsError, PersistFailureState) {
(self.error, self.state)
}
#[inline]
#[must_use]
pub fn into_recovery_parts(self) -> (FsError, PersistFailureState, Option<Path>) {
(self.error, self.state, self.publication_target)
}
}
impl Display for PersistFailure {
#[inline]
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
write!(formatter, "persist {:?}: {}", self.state, self.error)
}
}
impl Error for PersistFailure {
#[inline]
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.error)
}
}