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
//! The core has one error type, `Error`. The command-line interface (CLI)
//! exits with code 2 for every variant.
use std::path::PathBuf;
use crate::RepoPath;
/// `Result` is the result type of this crate, with `Error` as its error.
pub type Result<T> = std::result::Result<T, Error>;
/// `Error` holds the errors of this crate. Every variant renders a message
/// that the CLI prints unchanged after `error: `.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// The tool finds no repository root (CLI-1_AC-5).
#[error("not in a Git repository; pass --repo-root")]
NoRepository,
/// The tool refuses a `config.toml` file (CONF-1_AC-3, CONF-1_AC-4,
/// CONF-2_AC-6, CONF-2_AC-7, CONF-3_AC-5, CONF-3_AC-6).
#[error("{path}: {message}")]
Config {
/// `path` is the path of the configuration file that the tool refuses.
path: RepoPath,
/// The message says what is wrong with the file.
message: String,
},
/// The tool refuses a `formats.yaml` file.
#[error("{path}: {message}")]
FormatsFile {
/// `path` is the path of the formats file that the tool refuses.
path: RepoPath,
/// The message says what is wrong with the file.
message: String,
},
/// The tool refuses a `PATHS` argument whose path is not in the
/// repository (CLI-4_AC-15).
#[error("{0}: no such path in the repository")]
Path(String),
/// `init` refuses to run (contract-cli CLI-2), and the message identifies
/// the path or the option at fault.
#[error("init: {0}")]
Init(String),
/// `harness` refuses to run (contract-cli CLI-3), and the message
/// identifies the harness, part, event, or file at fault.
#[error("harness: {0}")]
Harness(String),
/// `new` refuses to run (contract-cli CLI-6).
#[error("new: {0}")]
New(String),
/// `info` refuses to run (contract-cli CLI-5).
#[error("info: {0}")]
Info(String),
/// `pack` refuses to run (contract-cli CLI-13), and the message names
/// the pack, the source, or the file at fault.
#[error("pack: {0}")]
Pack(String),
/// `graph` refuses a `START` argument, and the message lists each way the
/// tool can read the argument (CLI-7_AC-19).
#[error("graph: {0}")]
Start(String),
/// `locate` refuses an `id`, and the message gives the `id` and the paths
/// of the documents that declare it (CLI-8_AC-6).
#[error("locate: {0}")]
Locate(String),
/// A JMESPath expression doesn't compile (CLI-4_AC-17,
/// CLI-7_AC-21).
#[error("query: {0}")]
Query(String),
/// An input or output (I/O) operation fails at a path.
#[error("I/O error at {path}: {source}")]
Io {
/// `path` is the path at which the operation fails.
path: PathBuf,
/// `source` is the I/O error that causes the failure.
#[source]
source: std::io::Error,
},
/// The code breaks one of its own invariants.
#[error("internal: {0}")]
Internal(String),
}
impl From<tramlines_md::Error> for Error {
fn from(e: tramlines_md::Error) -> Self {
match e {
tramlines_md::Error::Internal(m) => Error::Internal(m),
}
}
}
/// An error of agent-harness-adapter is an I/O error at its path, or else a
/// refusal of `harness`.
impl From<agent_harness_adapter_core::Error> for Error {
fn from(e: agent_harness_adapter_core::Error) -> Self {
match e {
agent_harness_adapter_core::Error::Io { path, source } => Error::Io { path, source },
agent_harness_adapter_core::Error::Internal(m) => Error::Internal(m),
other => Error::Harness(other.to_string()),
}
}
}
impl Error {
/// `io` builds an [`Error::Io`] for `path` from a [`std::io::Error`].
pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Error::Io {
path: path.into(),
source,
}
}
/// `config` builds an [`Error::Config`].
pub fn config(path: RepoPath, message: impl Into<String>) -> Self {
Error::Config {
path,
message: message.into(),
}
}
/// `formats` builds an [`Error::FormatsFile`].
pub fn formats(path: RepoPath, message: impl Into<String>) -> Self {
Error::FormatsFile {
path,
message: message.into(),
}
}
}
#[cfg(test)]
mod tests {
use std::{error::Error as _, io, path::Path};
use super::*;
#[test]
fn io_constructor_and_display() {
let e = Error::io(
Path::new("/tmp/x"),
io::Error::new(io::ErrorKind::PermissionDenied, "denied"),
);
assert!(matches!(e, Error::Io { .. }));
let msg = e.to_string();
assert!(
msg.contains("/tmp/x"),
"the message contains the path: {msg}"
);
assert!(
msg.contains("denied"),
"the message contains the source error: {msg}"
);
assert!(e.source().is_some());
}
#[test]
fn a_kit_error_is_an_io_error_or_a_harness_refusal() {
let e: Error = agent_harness_adapter_core::Error::io(
"/tmp/x",
io::Error::new(io::ErrorKind::PermissionDenied, "denied"),
)
.into();
assert!(matches!(e, Error::Io { .. }), "{e}");
let e: Error = agent_harness_adapter_core::Error::file("/.sokf/config.toml", "bad").into();
assert_eq!(e.to_string(), "harness: /.sokf/config.toml: bad");
let e: Error = agent_harness_adapter_core::Error::Internal("bug".into()).into();
assert_eq!(e.to_string(), "internal: bug");
let e: Error = agent_harness_adapter_core::Error::UnknownHarness {
harness: "cline".into(),
}
.into();
assert_eq!(e.to_string(), "harness: no harness named `cline`");
}
#[test]
fn every_message_names_its_subject() {
let path = RepoPath::parse("/.sokf/config.toml").unwrap();
assert_eq!(
Error::config(path.clone(), "unknown key").to_string(),
"/.sokf/config.toml: unknown key"
);
assert_eq!(
Error::formats(path, "bad").to_string(),
"/.sokf/config.toml: bad"
);
assert_eq!(
Error::Path("x".into()).to_string(),
"x: no such path in the repository"
);
assert_eq!(Error::New("no schema".into()).to_string(), "new: no schema");
assert_eq!(Error::Info("x".into()).to_string(), "info: x");
assert_eq!(Error::Init("x".into()).to_string(), "init: x");
assert_eq!(Error::Harness("x".into()).to_string(), "harness: x");
assert_eq!(Error::Pack("x".into()).to_string(), "pack: x");
assert_eq!(Error::Start("x".into()).to_string(), "graph: x");
assert_eq!(Error::Locate("x".into()).to_string(), "locate: x");
assert_eq!(Error::Query("x".into()).to_string(), "query: x");
assert_eq!(Error::Internal("x".into()).to_string(), "internal: x");
assert!(Error::NoRepository.to_string().contains("--repo-root"));
assert!(Error::NoRepository.source().is_none());
}
}