diff options
Diffstat (limited to 'src/structs/error.rs')
| -rw-r--r-- | src/structs/error.rs | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/src/structs/error.rs b/src/structs/error.rs index f07163c..deb10b3 100644 --- a/src/structs/error.rs +++ b/src/structs/error.rs | |||
| @@ -1,37 +1,42 @@ | |||
| 1 | use crate::{ArchiveError, ArchiveResult}; | 1 | use serde::{de, ser}; |
| 2 | use std::error::Error; | 2 | use std::error::Error; |
| 3 | use std::fmt::Display; | 3 | use std::fmt::Display; |
| 4 | 4 | ||
| 5 | pub type StructResult<T> = ArchiveResult<T, StructError>; | 5 | pub type StructResult<T> = Result<T, StructError>; |
| 6 | 6 | ||
| 7 | #[derive(Debug)] | 7 | #[derive(Debug, PartialEq, Eq)] |
| 8 | pub enum StructError { | 8 | pub enum StructError { |
| 9 | SerializationNotSupported { type_name: &'static str }, | 9 | SerializationNotSupported(&'static str), |
| 10 | DeserializationNotSupported { type_name: &'static str }, | 10 | DeserializationNotSupported(&'static str), |
| 11 | Serde(String), | ||
| 11 | UnexpectedEOF, | 12 | UnexpectedEOF, |
| 12 | } | 13 | } |
| 13 | 14 | ||
| 14 | impl From<StructError> for ArchiveError<StructError> { | ||
| 15 | fn from(value: StructError) -> Self { | ||
| 16 | Self::Archivator { | ||
| 17 | module: "Struct serializer", | ||
| 18 | error: value, | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | impl Display for StructError { | 15 | impl Display for StructError { |
| 24 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 16 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 25 | match self { | 17 | match self { |
| 26 | Self::SerializationNotSupported { type_name } => { | 18 | Self::SerializationNotSupported(name) => { |
| 27 | writeln!(f, "Serialization for type '{type_name}' not supported") | 19 | writeln!(f, "Serialization for type '{}' not supported", name) |
| 28 | } | 20 | } |
| 29 | Self::DeserializationNotSupported { type_name } => { | 21 | Self::DeserializationNotSupported(name) => { |
| 30 | writeln!(f, "Deserialization for type '{type_name}' not supported") | 22 | writeln!(f, "Deserialization for type '{}' not supported", name) |
| 31 | } | 23 | } |
| 24 | Self::Serde(message) => writeln!(f, "Serde error: {}", message), | ||
| 32 | Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"), | 25 | Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"), |
| 33 | } | 26 | } |
| 34 | } | 27 | } |
| 35 | } | 28 | } |
| 36 | 29 | ||
| 37 | impl Error for StructError {} | 30 | impl Error for StructError {} |
| 31 | |||
| 32 | impl ser::Error for StructError { | ||
| 33 | fn custom<T: Display>(msg: T) -> Self { | ||
| 34 | Self::Serde(msg.to_string()) | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | impl de::Error for StructError { | ||
| 39 | fn custom<T: Display>(msg: T) -> Self { | ||
| 40 | Self::Serde(msg.to_string()) | ||
| 41 | } | ||
| 42 | } | ||
