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
//! Why a command could not answer, and the exit code each reason gets.
//!
//! The kinds mirror the typed refusals of the library: unsupported,
//! invalid, missing-reference and budget-exceeded stay distinct, so a
//! script reading stderr can tell "this build cannot" from "this file is
//! broken". Every kind exits 2; findings are not errors and exit 1 through
//! [`Outcome`].
use std::fmt;
/// How a command that ran to completion ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Outcome {
/// Done, nothing to report against the input.
Clean,
/// `validate` or `lint` reported findings that fail the run.
Findings,
/// A multi-file command could not check some file; each such file was
/// already reported in the output and on stderr.
Incomplete,
/// `edit` or `import-table` refused a planned change; every refusal
/// was reported and nothing was written.
Refused,
}
impl Outcome {
/// The process exit code.
pub(crate) fn code(self) -> u8 {
match self {
Self::Clean => 0,
Self::Findings => 1,
Self::Incomplete => 2,
Self::Refused => 3,
}
}
}
/// A command refused or failed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum CliError {
/// The arguments are consistent with the grammar but not with each
/// other or with the input (an output format no extension implies).
Usage(String),
/// Reading or writing a file failed.
Io {
/// The file, or `-` for a standard stream.
path: String,
/// The operating system's reason.
detail: String,
},
/// The input is not a STEP or ifcXML file, or does not parse as one.
Parse {
/// The input file.
path: String,
/// The codec's reason.
detail: String,
},
/// The header declares no schema, or one this build bundles no tables
/// for; the command needs them and does not guess another release.
UnsupportedSchema(String),
/// Something this tool or the library does not support for this input.
Unsupported(String),
/// An entity id or `GlobalId` the model does not hold.
MissingEntity(String),
/// An entity of a type the command cannot answer for.
WrongEntityType(String),
/// A reference in the model names an entity the model does not hold.
MissingReference(String),
/// The model is malformed or ambiguous where the command needs an
/// exact answer.
InvalidModel(String),
/// A traversal hit its budget before it could answer exactly.
BudgetExceeded(String),
/// Writing the output format refused the model.
Write(String),
/// A selector or value path does not follow the grammar, or names a
/// class or attribute the declared release does not declare; the
/// detail names the byte span it refuses.
InvalidSelector(String),
}
impl CliError {
/// The process exit code: 2 for every refusal and failure.
pub(crate) fn exit_code(&self) -> u8 {
2
}
/// A short, stable name for the kind, printed before the detail.
pub(crate) fn kind(&self) -> &'static str {
match self {
Self::Usage(_) => "usage",
Self::Io { .. } => "io",
Self::Parse { .. } => "parse",
Self::UnsupportedSchema(_) => "unsupported-schema",
Self::Unsupported(_) => "unsupported",
Self::MissingEntity(_) => "missing-entity",
Self::WrongEntityType(_) => "wrong-entity-type",
Self::MissingReference(_) => "missing-reference",
Self::InvalidModel(_) => "invalid-model",
Self::BudgetExceeded(_) => "budget-exceeded",
Self::Write(_) => "write",
Self::InvalidSelector(_) => "invalid-selector",
}
}
/// A write to standard output failed.
pub(crate) fn stdout(error: &std::io::Error) -> Self {
Self::Io {
path: "-".to_owned(),
detail: error.to_string(),
}
}
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: ", self.kind())?;
match self {
Self::Io { path, detail } | Self::Parse { path, detail } => {
write!(f, "{path}: {detail}")
}
Self::Usage(detail)
| Self::UnsupportedSchema(detail)
| Self::Unsupported(detail)
| Self::MissingEntity(detail)
| Self::WrongEntityType(detail)
| Self::MissingReference(detail)
| Self::InvalidModel(detail)
| Self::BudgetExceeded(detail)
| Self::Write(detail)
| Self::InvalidSelector(detail) => f.write_str(detail),
}
}
}
/// Shorthand for command results.
pub(crate) type CliResult<T> = Result<T, CliError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn findings_and_refusals_exit_differently() {
assert_eq!(Outcome::Clean.code(), 0);
assert_eq!(Outcome::Findings.code(), 1);
assert_eq!(Outcome::Incomplete.code(), 2);
assert_eq!(Outcome::Refused.code(), 3);
let refusal = CliError::UnsupportedSchema("IFC9".into());
assert_eq!(refusal.exit_code(), 2);
assert_eq!(refusal.to_string(), "unsupported-schema: IFC9");
}
}