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
use std::fmt;
/// An esf/1 error, and where: a line of text, or a fit and entry, counting from 1.
///
/// Every error is one of three [`ErrorKind`]s. Check `kind` to handle one kind.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Error {
/// What kind of error this is.
pub kind: ErrorKind,
/// What is wrong.
pub message: String,
/// The line of the text.
pub line: Option<usize>,
/// The fit, for `save()` and binary documents.
pub fit: Option<usize>,
/// The entry of that fit.
pub entry: Option<usize>,
}
/// What kind of error an [`Error`] is.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
/// The data is not a well-formed esf/1 document.
///
/// The text does not follow the grammar, the binary form or base64url is broken, or a fit
/// name cannot be written.
Format,
/// The catalog does not know a name or ID, or a name matches more than one type.
///
/// This often means the catalog is older than the fit.
Catalog,
/// The fit breaks a rule of esf/1.
///
/// The document is well formed and the catalog knows every type, but the fit is not valid.
/// For example, a module is in the wrong rack.
Rule,
}
impl Error {
fn new(kind: ErrorKind, message: impl Into<String>) -> Error {
Error {
kind,
message: message.into(),
line: None,
fit: None,
entry: None,
}
}
pub(crate) fn format(message: impl Into<String>) -> Error {
Error::new(ErrorKind::Format, message)
}
pub(crate) fn catalog(message: impl Into<String>) -> Error {
Error::new(ErrorKind::Catalog, message)
}
pub(crate) fn rule(message: impl Into<String>) -> Error {
Error::new(ErrorKind::Rule, message)
}
pub(crate) fn at(self, line: usize) -> Error {
Error {
line: Some(line),
..self
}
}
pub(crate) fn in_fit(self, fit: usize, entry: Option<usize>) -> Error {
Error {
line: None,
fit: Some(fit),
entry,
..self
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (self.fit, self.entry, self.line) {
(Some(fit), Some(entry), _) => write!(f, "fit {fit}, entry {entry}: ")?,
(Some(fit), None, _) => write!(f, "fit {fit}: ")?,
(None, _, Some(line)) => write!(f, "line {line}: ")?,
(None, _, None) => {}
}
f.write_str(&self.message)
}
}
impl std::error::Error for Error {}