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
//! Error types shared by the whole crate.
/// A convenient alias for results produced by this crate.
pub type Result<T> = std::result::Result<T, Error>;
/// Every failure `stepq` can report.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// The input is not well-formed ISO 10303-21.
#[error("syntax error at line {line}, column {column}: {message}")]
Syntax {
/// 1-based line of the offending token.
line: usize,
/// 1-based column of the offending token, counted in bytes.
column: usize,
/// 0-based byte offset of the offending token in the input.
offset: usize,
/// What went wrong.
message: String,
},
/// An entity instance references an `#id` that does not exist.
#[error("entity #{from} references undefined instance #{to}")]
UnresolvedReference {
/// The referencing instance.
from: u64,
/// The missing instance.
to: u64,
},
/// The same `#id` is declared more than once.
#[error("entity #{0} is defined more than once")]
DuplicateId(u64),
/// An external reference to another file could not be resolved.
#[error("external reference to {file}: {message}")]
ExternalReference {
/// The file referred to.
file: String,
/// What went wrong.
message: String,
},
/// An I/O failure while reading or writing a file.
#[error(transparent)]
Io(#[from] std::io::Error),
/// A unit, or the units of a context or item, cannot be read as stated.
#[error(transparent)]
Unit(#[from] crate::units::UnitError),
/// A point, direction, placement, surface or curve cannot be read as
/// stated.
#[error(transparent)]
Geometry(#[from] crate::geometry::GeometryError),
/// An assembly usage, or an occurrence, cannot be placed as stated.
#[error(transparent)]
Placement(#[from] crate::model::PlacementError),
/// A solid's topology cannot be read as stated.
#[error(transparent)]
Topology(#[from] crate::topology::TopologyError),
}
impl Error {
/// Builds an [`Error::Syntax`] located at byte `offset` of `src`.
///
/// Line and column are computed here, on the error path only, so the
/// lexer does not track them for every byte.
pub(crate) fn syntax(src: &[u8], offset: usize, message: impl Into<String>) -> Self {
let offset = offset.min(src.len());
let before = &src[..offset];
let line_start = before
.iter()
.rposition(|&b| b == b'\n')
.map_or(0, |i| i + 1);
Self::Syntax {
line: before.split(|&b| b == b'\n').count(),
column: offset - line_start + 1,
offset,
message: message.into(),
}
}
}