sare_core/format/
error.rs

1use std::fmt;
2
3use bson::de::Error as BsonError;
4use pem::PemError;
5
6#[derive(Debug)]
7pub enum ErrSection {
8    PEM(PemError),
9    BSON(BsonError),
10    HEADER,
11}
12
13impl fmt::Display for ErrSection {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        match self {
16            ErrSection::PEM(err) => write!(f, "PEM Error: {}", err),
17            ErrSection::BSON(err) => write!(f, "BSON Error: {}", err),
18            ErrSection::HEADER => write!(f, "Header Error"),
19        }
20    }
21}
22
23#[derive(Debug)]
24pub enum FormatError {
25    FailedToDecode(ErrSection),
26}
27
28impl fmt::Display for FormatError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            FormatError::FailedToDecode(err) => write!(f, "Failed to decode: {}", err),
32        }
33    }
34}
35
36impl From<BsonError> for FormatError {
37    fn from(err: BsonError) -> Self {
38        FormatError::FailedToDecode(ErrSection::BSON(err))
39    }
40}
41
42impl From<PemError> for FormatError {
43    fn from(err: PemError) -> Self {
44        FormatError::FailedToDecode(ErrSection::PEM(err))
45    }
46}