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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "std")]
use std::error::Error;

/// Creates a static error description with file and line information
#[doc(hidden)]
#[macro_export]
macro_rules! e {
    () => {
        concat!("@", file!(), ":", line!())
    };
    ($str:expr) => {
        concat!($str, " @", file!(), ":", line!())
    };
}

/// Creates an `InOutError` variant
#[doc(hidden)]
#[macro_export]
macro_rules! eio {
    ($str:expr) => {
        $crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::InOutError(e!($str)))
    };
}
/// Creates an `InvalidData` variant
#[doc(hidden)]
#[macro_export]
macro_rules! einval {
    ($str:expr) => {
        $crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::InvalidData(e!($str)))
    };
}
/// Creates an `Unsupported` variant
#[doc(hidden)]
#[macro_export]
macro_rules! eunsupported {
    ($str:expr) => {
        $crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::Unsupported(e!($str)))
    };
}
/// Creates an `Other` variant
#[doc(hidden)]
#[macro_export]
macro_rules! eother {
    ($str:expr) => {
        $crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::Other(e!($str)))
    };
}

/// A trait to chain errors
pub trait ErrorChain {
    /// Chains another error to `self`
    ///
    /// _Info: does nothing if not build with `std`_
    fn propagate(self, desc: &'static str) -> Self;
}
impl<T> ErrorChain for Result<T, Asn1DerError> {
    #[cfg_attr(feature = "no_panic", no_panic::no_panic)]
    fn propagate(self, _desc: &'static str) -> Self {
        #[cfg(any(not(feature = "std"), feature = "no_panic"))]
        return self;
        #[cfg(all(feature = "std", not(feature = "no_panic")))]
        {
            self.map_err(|e| {
                let new_error = match e.error {
                    Asn1DerErrorVariant::InOutError(_) => Asn1DerErrorVariant::InOutError(_desc),
                    Asn1DerErrorVariant::InvalidData(_) => Asn1DerErrorVariant::InvalidData(_desc),
                    Asn1DerErrorVariant::Unsupported(_) => Asn1DerErrorVariant::Unsupported(_desc),
                    Asn1DerErrorVariant::Other(_) => Asn1DerErrorVariant::Other(_desc),
                };
                Asn1DerError { error: new_error, source: Some(ErrorSource::new(e)) }
            })
        }
    }
}

/// An `Asn1DerError` variant
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Asn1DerErrorVariant {
    /// An in-out error occurred (e.g. failed to read/write some bytes)
    InOutError(&'static str),
    /// The data has an invalid encoding
    InvalidData(&'static str),
    /// The object type or length is not supported by this implementation
    Unsupported(&'static str),
    /// An unspecified error
    Other(&'static str),
}
impl Display for Asn1DerErrorVariant {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Asn1DerErrorVariant::InOutError(desc) => write!(f, "I/O error {}", desc),
            Asn1DerErrorVariant::InvalidData(desc) => write!(f, "Invalid encoding {}", desc),
            Asn1DerErrorVariant::Unsupported(desc) => write!(f, "Unsupported {}", desc),
            Asn1DerErrorVariant::Other(desc) => write!(f, "Other {}", desc),
        }
    }
}

/// An error source
#[doc(hidden)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ErrorSource {
    #[cfg(any(not(feature = "std"), feature = "no_panic"))]
    inner: &'static str,
    #[cfg(all(feature = "std", not(feature = "no_panic")))]
    inner: Box<Asn1DerError>,
}
impl ErrorSource {
    /// Creates a new error source
    #[cfg(all(feature = "std", not(feature = "no_panic")))]
    pub fn new(e: Asn1DerError) -> Self {
        Self { inner: Box::new(e) }
    }
}

/// An `asn1_der` error
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Asn1DerError {
    #[doc(hidden)]
    pub error: Asn1DerErrorVariant,
    #[doc(hidden)]
    pub source: Option<ErrorSource>,
}
impl Asn1DerError {
    /// Creates a new error with `variant`
    #[cfg_attr(feature = "no_panic", no_panic::no_panic)]
    pub fn new(variant: Asn1DerErrorVariant) -> Self {
        Self { error: variant, source: None }
    }
}
impl Display for Asn1DerError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self.source.as_ref() {
            Some(source) => write!(f, "{}\n    caused by: {}", self.error, source.inner),
            None => write!(f, "{}", self.error),
        }
    }
}
#[cfg(feature = "std")]
impl Error for Asn1DerError {
    #[cfg_attr(feature = "no_panic", no_panic::no_panic)]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        #[cfg(any(not(feature = "std"), feature = "no_panic"))]
        return None;
        #[cfg(all(feature = "std", not(feature = "no_panic")))]
        return self.source.as_ref().map(|s| s.inner.as_ref() as _);
    }
}