From d6055b5ac4f3ff5016bc4881cf1cc109a22c40ba Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sun, 16 Jun 2024 21:36:13 +0900 Subject: Implement serialize --- src/error.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs index 6d7aba4..9252762 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,25 +2,27 @@ use std::error::Error; use std::fmt::Display; use std::io; -pub type ArchiveResult = Result>; +pub type ArchiveResult = Result>; #[derive(Debug)] pub enum ArchiveError { - IO(io::Error), - Driver { name: &'static str, error: E }, + IO { error: io::Error }, + Serde { message: String }, + Archivator { module: String, error: E }, } impl From for ArchiveError { fn from(value: io::Error) -> Self { - Self::IO(value) + Self::IO { error: value } } } impl Display for ArchiveError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - ArchiveError::IO(error) => write!(f, "{error}"), - ArchiveError::Driver { name, error } => write!(f, "{name}: {error}"), + Self::IO { error } => writeln!(f, "IO: {error}"), + Self::Serde { message } => writeln!(f, "Serde: {message}"), + Self::Archivator { module, error } => writeln!(f, "{module}: {error}"), } } } @@ -28,8 +30,25 @@ impl Display for ArchiveError { impl Error for ArchiveError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self { - Self::IO(error) => Some(error), - _ => None, + Self::IO { error } => Some(error), + Self::Serde { .. } => None, + Self::Archivator { module, error } => None, + } + } +} + +impl serde::ser::Error for ArchiveError { + fn custom(msg: T) -> Self { + Self::Serde { + message: msg.to_string(), + } + } +} + +impl serde::de::Error for ArchiveError { + fn custom(msg: T) -> Self { + Self::Serde { + message: msg.to_string(), } } } -- cgit v1.2.3