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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::rust::fmt::{ self, Display, Formatter };
#[cfg(not(feature = "no_std"))]
use crate::rust::error::Error;
#[doc(hidden)]
#[macro_export]
macro_rules! e {
() => (concat!("@", file!(), ":", line!()));
($str:expr) => (concat!($str, " @", file!(), ":", line!()));
}
#[doc(hidden)]
#[macro_export]
macro_rules! eio {
($str:expr) => (
$crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::InOutError(e!($str)))
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! einval {
($str:expr) => (
$crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::InvalidData(e!($str)))
);
}
#[doc(hidden)]
#[macro_export]
macro_rules! eunsupported {
($str:expr) => (
$crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::Unsupported(e!($str)))
);
}
#[doc(hidden)]
#[macro_export]
macro_rules! eother {
($str:expr) => (
$crate::error::Asn1DerError::new($crate::error::Asn1DerErrorVariant::Other(e!($str)))
);
}
pub trait ErrorChain {
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(feature = "no_std", feature = "no_panic"))]
return self;
#[cfg(not(any(feature = "no_std", 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)) }
})
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Asn1DerErrorVariant {
InOutError(&'static str),
InvalidData(&'static str),
Unsupported(&'static str),
Other(&'static str)
}
impl Asn1DerErrorVariant {
#[inline(never)] #[allow(improper_ctypes_definitions)]
extern "C" fn write(f: &mut Formatter, kind: &str, desc: &str) -> fmt::Result {
write!(f, "{}: {}", kind, desc)
}
}
impl Display for Asn1DerErrorVariant {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Asn1DerErrorVariant::InOutError(desc) => Self::write(f, "I/O error", desc),
Asn1DerErrorVariant::InvalidData(desc) => Self::write(f, "Invalid encoding", desc),
Asn1DerErrorVariant::Unsupported(desc) => Self::write(f, "Unsupported", desc),
Asn1DerErrorVariant::Other(desc) => Self::write(f, "Other", desc)
}
}
}
#[doc(hidden)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ErrorSource {
#[cfg(any(feature = "no_std", feature = "no_panic"))]
inner: &'static str,
#[cfg(not(any(feature = "no_std", feature = "no_panic")))]
inner: Box<Asn1DerError>
}
impl ErrorSource {
#[cfg(not(any(feature = "no_std", feature = "no_panic")))]
pub fn new(e: Asn1DerError) -> Self {
Self{ inner: Box::new(e) }
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Asn1DerError {
#[doc(hidden)]
pub error: Asn1DerErrorVariant,
#[doc(hidden)]
pub source: Option<ErrorSource>
}
impl Asn1DerError {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
pub fn new(variant: Asn1DerErrorVariant) -> Self {
Self{ error: variant, source: None }
}
#[inline(never)] #[allow(improper_ctypes_definitions)]
extern "C" fn write(f: &mut Formatter, variant: &Asn1DerErrorVariant,
source: &Option<ErrorSource>) -> fmt::Result
{
match source.as_ref() {
Some(source) => write!(f, "{}\n caused by: {}", variant, source.inner),
None => write!(f, "{}", variant)
}
}
}
impl Display for Asn1DerError {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Self::write(f, &self.error, &self.source)
}
}
#[cfg(not(feature = "no_std"))]
impl Error for Asn1DerError {
#[cfg_attr(feature = "no_panic", no_panic::no_panic)]
fn source(&self) -> Option<&(dyn Error + 'static)> {
#[cfg(any(feature = "no_std", feature = "no_panic"))]
return None;
#[cfg(not(any(feature = "no_std", feature = "no_panic")))]
return self.source.as_ref().map(|s| s.inner.as_ref() as _);
}
}