aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-06-16 21:36:13 +0900
committerIgor Tolmachev <me@igorek.dev>2024-06-23 15:34:34 +0900
commitd6055b5ac4f3ff5016bc4881cf1cc109a22c40ba (patch)
tree40a9044f923945e6effc13c627261630dddc574c /src/error.rs
parent6444bee8f3e188be014841ea8cd7cfb53eb03ed9 (diff)
downloadarchivator-d6055b5ac4f3ff5016bc4881cf1cc109a22c40ba.tar.gz
archivator-d6055b5ac4f3ff5016bc4881cf1cc109a22c40ba.zip
Implement serialize
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs35
1 files changed, 27 insertions, 8 deletions
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;
2use std::fmt::Display; 2use std::fmt::Display;
3use std::io; 3use std::io;
4 4
5pub type ArchiveResult<R, E> = Result<R, ArchiveError<E>>; 5pub type ArchiveResult<T, E> = Result<T, ArchiveError<E>>;
6 6
7#[derive(Debug)] 7#[derive(Debug)]
8pub enum ArchiveError<E: Error> { 8pub enum ArchiveError<E: Error> {
9 IO(io::Error), 9 IO { error: io::Error },
10 Driver { name: &'static str, error: E }, 10 Serde { message: String },
11 Archivator { module: String, error: E },
11} 12}
12 13
13impl<E: Error> From<io::Error> for ArchiveError<E> { 14impl<E: Error> From<io::Error> for ArchiveError<E> {
14 fn from(value: io::Error) -> Self { 15 fn from(value: io::Error) -> Self {
15 Self::IO(value) 16 Self::IO { error: value }
16 } 17 }
17} 18}
18 19
19impl<E: Error> Display for ArchiveError<E> { 20impl<E: Error> Display for ArchiveError<E> {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 match self { 22 match self {
22 ArchiveError::IO(error) => write!(f, "{error}"), 23 Self::IO { error } => writeln!(f, "IO: {error}"),
23 ArchiveError::Driver { name, error } => write!(f, "{name}: {error}"), 24 Self::Serde { message } => writeln!(f, "Serde: {message}"),
25 Self::Archivator { module, error } => writeln!(f, "{module}: {error}"),
24 } 26 }
25 } 27 }
26} 28}
@@ -28,8 +30,25 @@ impl<E: Error> Display for ArchiveError<E> {
28impl<E: Error> Error for ArchiveError<E> { 30impl<E: Error> Error for ArchiveError<E> {
29 fn source(&self) -> Option<&(dyn Error + 'static)> { 31 fn source(&self) -> Option<&(dyn Error + 'static)> {
30 match self { 32 match self {
31 Self::IO(error) => Some(error), 33 Self::IO { error } => Some(error),
32 _ => None, 34 Self::Serde { .. } => None,
35 Self::Archivator { module, error } => None,
36 }
37 }
38}
39
40impl<E: Error> serde::ser::Error for ArchiveError<E> {
41 fn custom<T: Display>(msg: T) -> Self {
42 Self::Serde {
43 message: msg.to_string(),
44 }
45 }
46}
47
48impl<E: Error> serde::de::Error for ArchiveError<E> {
49 fn custom<T: Display>(msg: T) -> Self {
50 Self::Serde {
51 message: msg.to_string(),
33 } 52 }
34 } 53 }
35} 54}