use super::*;
pub mod prelude
{
pub use super::{FileError, FileResult};
}
pub type FileResult<T = ()> = Result<T, FileError>;
#[derive(Default)]
pub struct FileError
{
pub path: Option<PathBuf>,
pub kind: FileErrorKind,
}
impl FileError
{
pub fn new(kind: impl Into<FileErrorKind>) -> Self { Self { kind: kind.into(), path: None } }
pub fn with_path(mut self, path: Option<PathBuf>) -> Self
{
self.path = path;
self
}
}
impl Deref for FileError
{
type Target = FileErrorKind;
fn deref(&self) -> &Self::Target { &self.kind }
}
impl DerefMut for FileError
{
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.kind }
}
impl Debug for FileError
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult
{
match &self.path
{
Some(path) => write!(f, "{:?} at {:?}", self.kind, path.display()),
None => write!(f, "{:?} at unknow path", self.kind),
}
}
}
impl Display for FileError
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult
{
match &self.path
{
Some(path) => write!(f, "{:?} at {:?}", self.kind, path.display()),
None => write!(f, "{:?} at unknow path", self.kind),
}
}
}
impl std::error::Error for FileError {}
#[cfg(feature = "serde")]
impl serde::ser::Error for FileError
{
fn custom<T>(msg: T) -> Self
where
T: Display,
{
FileError { path: None, kind: FileErrorKind::Encode(EncodeError::Custom(msg.to_string())) }
}
}
#[cfg(feature = "serde")]
impl serde::de::Error for FileError
{
fn custom<T>(msg: T) -> Self
where
T: Display,
{
FileError { path: None, kind: FileErrorKind::Encode(EncodeError::Custom(msg.to_string())) }
}
}
pub enum FileErrorKind
{
Encode(EncodeError),
Io(IoError),
}
impl Default for FileErrorKind
{
fn default() -> Self {
FileErrorKind::Encode(EncodeError::Unknow)
}
}
impl From<IoError> for FileErrorKind
{
fn from(value: IoError) -> Self { Self::Io(value) }
}
impl From<IoErrorKind> for FileErrorKind
{
fn from(value: IoErrorKind) -> Self { Self::Io(IoError::from(value)) }
}
impl From<EncodeError> for FileErrorKind
{
fn from(value: EncodeError) -> Self { Self::Encode(value) }
}
impl Debug for FileErrorKind
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult
{
match self
{
Self::Encode(v) => write!(f, "{:?}", v),
Self::Io(v) => write!(f, "{:?}", v),
}
}
}
impl FileErrorKind
{
pub fn is_io(&self) -> bool { matches!(self, Self::Io(_)) }
pub fn is_encode(&self) -> bool { matches!(self, Self::Encode(_)) }
}