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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! The errors of loading a rule set and of validating a document.
use std::fmt;
use std::path::PathBuf;
use einvoice_xpath_eval::LimitExceeded;
/// Why a rule set could not be loaded, or a document could not be validated.
///
/// Validity problems of a document (schema errors, failed asserts) are not errors: they
/// are in the [`Report`](crate::Report). An error means that no complete verdict could
/// be given.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// A file could not be read.
Io {
/// The file.
path: PathBuf,
/// The cause.
source: std::io::Error,
},
/// The bundle is not usable: a manifest that does not follow the format, a missing
/// file, an unknown schema name, a stylesheet that is not well-formed XSLT, or one
/// whose result is not an SVRL report.
Bundle {
/// What is wrong, naming the file and line when known.
message: String,
},
/// A file of the bundle does not have the SHA-256 its manifest records.
Checksum {
/// The file, relative to the bundle.
path: String,
/// The SHA-256 the manifest records (lower-case hexadecimal).
expected: String,
/// The SHA-256 of the file.
actual: String,
},
/// The document is not well-formed XML (or not namespace-well-formed, or with bytes
/// that are not characters of its encoding, or first bytes and an XML declaration
/// that name different encodings).
NotXml {
/// 1-based line.
line: u32,
/// 1-based column.
column: u32,
/// The parser's message.
message: String,
},
/// The document or a rule-set artefact needs a construct that this engine does not
/// implement (an encoding the validator does not read, a document type declaration,
/// an XSLT or
/// XML Schema construct outside the supported subset). Refused rather than answered
/// differently from the reference engine.
Unsupported {
/// What is not supported, and where.
message: String,
},
/// The document exceeds a limit that keeps its validation within a bounded call
/// stack: an element nested deeper than [`ValidateOptions::max_depth`]
/// ([`LimitExceeded::Depth`]), in the document or in a document a stylesheet loads
/// with `document()`; or a stylesheet invoking templates and functions one inside
/// another beyond the nesting that maximum allows ([`LimitExceeded::Nesting`]).
/// Refused rather than risking a stack overflow, which would abort the process.
///
/// [`ValidateOptions::max_depth`]: crate::ValidateOptions::max_depth
Limit {
/// The limit exceeded, with its value and, for a depth, the line and column of
/// the element.
limit: LimitExceeded,
/// Where: the document, or the stylesheet (by its name in the bundle) and the
/// line of its instruction.
message: String,
},
/// An option of the validation beyond what this target allows: on WebAssembly, a
/// [`ValidateOptions::max_depth`] above [`WASM_MAX_DEPTH`] or a
/// [`ValidateOptions::max_stack`] above [`WASM_MAX_STACK`], for which the native stack
/// of the JavaScript engines has no room. Never on other targets.
///
/// [`ValidateOptions::max_depth`]: crate::ValidateOptions::max_depth
/// [`ValidateOptions::max_stack`]: crate::ValidateOptions::max_stack
/// [`WASM_MAX_DEPTH`]: crate::WASM_MAX_DEPTH
/// [`WASM_MAX_STACK`]: crate::WASM_MAX_STACK
OptionOutOfRange {
/// The option: `max_depth` or `max_stack`.
option: &'static str,
/// The value asked for.
value: u32,
/// The largest value allowed.
max: u32,
},
/// A stylesheet of the rule set raised a dynamic error on the document (the
/// reference engine fails on it as well).
Transform {
/// The stylesheet, by its name in the bundle.
stylesheet: String,
/// The XPath or XSLT error code (`FORG0001`, ...).
code: String,
/// The line of the stylesheet instruction, when known.
line: Option<u32>,
/// The explanation.
message: String,
},
/// A bug of this engine: a result it cannot read back.
Internal {
/// What went wrong.
message: String,
},
/// A fetched rule set ([`fetched_rule_sets`](crate::fetched_rule_sets)) that is not
/// in the cache: it must be fetched first (`RuleSetCache::fetch`, feature `fetch`;
/// `einvoice-validate rules fetch NAME`). Nothing is downloaded without being asked.
NotCached {
/// The rule set.
name: String,
/// The cache directory.
dir: PathBuf,
},
/// A rule set could not be fetched: a download failed or did not have its pinned
/// digest, an archive is not as published, or its bundle could not be built (no
/// Java for the Schematron compiler of Peppol BIS Billing, ...).
Fetch {
/// What went wrong, naming the URL or file concerned.
message: String,
},
/// No invoice XML could be extracted from a PDF file ([`crate::pdf::extract`]): its
/// kind is [`ErrorKind::Unsupported`] for an encrypted file, a PDF feature outside the
/// subset read or a limit exceeded, and [`ErrorKind::Io`] otherwise (not a PDF, a
/// malformed file, no embedded file with an invoice name or several, XMP metadata
/// that is not well-formed): the file cannot be read as an e-invoice.
#[cfg(feature = "pdf")]
Pdf(einvoice_pdf::Error),
}
/// The kind of an [`Error`], for callers that only need to classify it (the exit code of
/// a command-line tool, ...).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
/// [`Error::Io`] and [`Error::Fetch`], and an `Error::Pdf` (feature `pdf`) for a PDF
/// file no invoice can be read from.
Io,
/// [`Error::Bundle`], [`Error::Checksum`] and [`Error::NotCached`].
Bundle,
/// [`Error::NotXml`].
NotXml,
/// [`Error::Unsupported`], [`Error::Limit`] and [`Error::OptionOutOfRange`]: input
/// refused rather than processed differently from the reference engine, or with an
/// unbounded stack, and options beyond what the target allows; and
/// an `Error::Pdf` (feature `pdf`) for an encrypted PDF file, a PDF feature outside
/// the subset read, or a limit of the PDF reader.
Unsupported,
/// [`Error::Transform`].
Transform,
/// [`Error::Internal`].
Internal,
}
impl ErrorKind {
/// A stable identifier of the kind: `io`, `bundle`, `not-xml`, `unsupported`,
/// `transform` or `internal`.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Io => "io",
Self::Bundle => "bundle",
Self::NotXml => "not-xml",
Self::Unsupported => "unsupported",
Self::Transform => "transform",
Self::Internal => "internal",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Error {
/// The kind of the error.
#[must_use]
pub const fn kind(&self) -> ErrorKind {
match self {
Self::Io { .. } | Self::Fetch { .. } => ErrorKind::Io,
Self::Bundle { .. } | Self::Checksum { .. } | Self::NotCached { .. } => {
ErrorKind::Bundle
}
Self::NotXml { .. } => ErrorKind::NotXml,
Self::Unsupported { .. } | Self::Limit { .. } | Self::OptionOutOfRange { .. } => {
ErrorKind::Unsupported
}
Self::Transform { .. } => ErrorKind::Transform,
Self::Internal { .. } => ErrorKind::Internal,
#[cfg(feature = "pdf")]
Self::Pdf(
einvoice_pdf::Error::Encrypted
| einvoice_pdf::Error::Unsupported { .. }
| einvoice_pdf::Error::LimitExceeded { .. },
) => ErrorKind::Unsupported,
#[cfg(feature = "pdf")]
Self::Pdf(_) => ErrorKind::Io,
}
}
pub(crate) fn bundle(message: impl Into<String>) -> Self {
Self::Bundle {
message: message.into(),
}
}
pub(crate) fn unsupported(message: impl Into<String>) -> Self {
Self::Unsupported {
message: message.into(),
}
}
pub(crate) fn internal(message: impl Into<String>) -> Self {
Self::Internal {
message: message.into(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
Self::Bundle { message } => write!(f, "invalid rule-set bundle: {message}"),
Self::Checksum {
path,
expected,
actual,
} => write!(
f,
"invalid rule-set bundle: {path}: SHA-256 {actual}, the manifest records {expected}"
),
Self::NotXml {
line,
column,
message,
} => write!(f, "{line}:{column}: not well-formed XML: {message}"),
Self::Unsupported { message } => write!(f, "unsupported: {message}"),
Self::Limit { message, .. } => write!(f, "limit exceeded: {message}"),
Self::OptionOutOfRange { option, value, max } => write!(
f,
"option {option} is {value}, more than {max}, the most this target allows"
),
Self::Transform {
stylesheet,
code,
line,
message,
} => {
write!(f, "stylesheet {stylesheet}")?;
if let Some(line) = line {
write!(f, ", line {line}")?;
}
write!(f, ": {code}: {message}")
}
Self::Internal { message } => write!(f, "internal error: {message}"),
Self::NotCached { name, dir } => write!(
f,
"rule set {name} is not in the cache ({}): it must be fetched first",
dir.display()
),
Self::Fetch { message } => write!(f, "cannot fetch the rule set: {message}"),
#[cfg(feature = "pdf")]
Self::Pdf(error) => error.fmt(f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
#[cfg(feature = "pdf")]
Self::Pdf(error) => Some(error),
_ => None,
}
}
}