aboutsummaryrefslogtreecommitdiff
path: root/src/structs/error.rs
blob: deb10b30f86b6ecf5bc48879deb3a0f4d021ab2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use serde::{de, ser};
use std::error::Error;
use std::fmt::Display;

pub type StructResult<T> = Result<T, StructError>;

#[derive(Debug, PartialEq, Eq)]
pub enum StructError {
    SerializationNotSupported(&'static str),
    DeserializationNotSupported(&'static str),
    Serde(String),
    UnexpectedEOF,
}

impl Display for StructError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SerializationNotSupported(name) => {
                writeln!(f, "Serialization for type '{}' not supported", name)
            }
            Self::DeserializationNotSupported(name) => {
                writeln!(f, "Deserialization for type '{}' not supported", name)
            }
            Self::Serde(message) => writeln!(f, "Serde error: {}", message),
            Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"),
        }
    }
}

impl Error for StructError {}

impl ser::Error for StructError {
    fn custom<T: Display>(msg: T) -> Self {
        Self::Serde(msg.to_string())
    }
}

impl de::Error for StructError {
    fn custom<T: Display>(msg: T) -> Self {
        Self::Serde(msg.to_string())
    }
}