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
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::convert::From;
use std::fmt::{Debug, Display, Error, Formatter};
use std::{fmt, io};
use thiserror::Error;
pub type IonResult<T> = Result<T, IonError>;
#[cfg(feature = "ion_c")]
pub type IonCErrorSource = ion_c_sys::result::IonCError;
#[cfg(not(feature = "ion_c"))]
pub type IonCErrorSource = ErrorStub;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ErrorStub;
impl Display for ErrorStub {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "ion-c error support was not enabled at build time")
}
}
impl std::error::Error for ErrorStub {}
#[derive(Debug, Error)]
pub enum IonError {
#[error("{source:?}")]
IoError {
#[from]
source: io::Error,
},
#[error("ran out of input while reading {label} at offset {offset}")]
Incomplete { label: &'static str, offset: usize },
#[error("{description}")]
EncodingError { description: String },
#[error("{description}")]
DecodingError { description: String },
#[error(
"The user has performed an operation that is not legal in the current state: {operation}"
)]
IllegalOperation { operation: String },
#[error("{source:?}")]
IonCError {
#[from]
source: IonCErrorSource,
},
}
impl From<fmt::Error> for IonError {
fn from(error: Error) -> Self {
IonError::EncodingError {
description: error.to_string(),
}
}
}
impl Clone for IonError {
fn clone(&self) -> Self {
use IonError::*;
match self {
IoError { source } => IoError {
source: io::Error::from(source.kind()),
},
Incomplete { label, offset } => Incomplete {
label,
offset: *offset,
},
EncodingError { description } => EncodingError {
description: description.clone(),
},
DecodingError { description } => DecodingError {
description: description.clone(),
},
IllegalOperation { operation } => IllegalOperation {
operation: operation.clone(),
},
IonCError { source } => IonCError { source: *source },
}
}
}
impl PartialEq for IonError {
fn eq(&self, other: &Self) -> bool {
use IonError::*;
match (self, other) {
(IoError { source: s1 }, IoError { source: s2 }) => s1.kind() == s2.kind(),
(
Incomplete {
label: l1,
offset: o1,
},
Incomplete {
label: l2,
offset: o2,
},
) => l1 == l2 && o1 == o2,
(EncodingError { description: s1 }, EncodingError { description: s2 }) => s1 == s2,
(DecodingError { description: s1 }, DecodingError { description: s2 }) => s1 == s2,
(IllegalOperation { operation: s1 }, IllegalOperation { operation: s2 }) => s1 == s2,
(IonCError { source: s1 }, IonCError { source: s2 }) => s1 == s2,
_ => false,
}
}
}
pub fn incomplete_data_error<T>(label: &'static str, offset: usize) -> IonResult<T> {
Err(incomplete_data_error_raw(label, offset))
}
pub fn incomplete_data_error_raw(label: &'static str, offset: usize) -> IonError {
IonError::Incomplete { label, offset }
}
pub fn decoding_error<T, S: AsRef<str>>(description: S) -> IonResult<T> {
Err(decoding_error_raw(description))
}
#[inline(never)]
pub fn decoding_error_raw<S: AsRef<str>>(description: S) -> IonError {
IonError::DecodingError {
description: description.as_ref().to_string(),
}
}
pub fn illegal_operation<T, S: AsRef<str>>(operation: S) -> IonResult<T> {
Err(illegal_operation_raw(operation))
}
#[inline(never)]
pub fn illegal_operation_raw<S: AsRef<str>>(operation: S) -> IonError {
IonError::IllegalOperation {
operation: operation.as_ref().to_string(),
}
}
#[cfg(all(test, feature = "ion_c"))]
mod test {
use ion_c_sys::result::*;
use ion_c_sys::{ion_error_code_IERR_EOF, ion_error_code_IERR_INVALID_ARG};
use super::*;
#[test]
fn ion_c_error_eq() {
let e1: IonError = IonCError::from(ion_error_code_IERR_EOF).into();
let e2: IonError = IonCError::from(ion_error_code_IERR_INVALID_ARG).into();
assert_eq!(e1, e1.clone());
assert_ne!(e1, e2);
}
}