1use std::path::PathBuf;
5
6use crate::RepoPath;
7
8pub type Result<T> = std::result::Result<T, Error>;
10
11#[derive(Debug, thiserror::Error)]
14pub enum Error {
15 #[error("not in a Git repository; pass --repo-root")]
17 NoRepository,
18
19 #[error("{path}: {message}")]
23 Config {
24 path: RepoPath,
26 message: String,
28 },
29
30 #[error("{path}: {message}")]
32 FormatsFile {
33 path: RepoPath,
35 message: String,
37 },
38
39 #[error("{0}: no such path in the repository")]
42 Path(String),
43
44 #[error("init: {0}")]
47 Init(String),
48
49 #[error("harness: {0}")]
52 Harness(String),
53
54 #[error("new: {0}")]
56 New(String),
57
58 #[error("info: {0}")]
60 Info(String),
61
62 #[error("graph: {0}")]
65 Start(String),
66
67 #[error("guide: {0}")]
70 Guide(String),
71
72 #[error("locate: {0}")]
75 Locate(String),
76
77 #[error("query: {0}")]
80 Query(String),
81
82 #[error("I/O error at {path}: {source}")]
84 Io {
85 path: PathBuf,
87 #[source]
89 source: std::io::Error,
90 },
91
92 #[error("internal: {0}")]
94 Internal(String),
95}
96
97impl From<tramlines_md::Error> for Error {
98 fn from(e: tramlines_md::Error) -> Self {
99 match e {
100 tramlines_md::Error::Internal(m) => Error::Internal(m),
101 }
102 }
103}
104
105impl From<agent_harness_adapter_core::Error> for Error {
108 fn from(e: agent_harness_adapter_core::Error) -> Self {
109 match e {
110 agent_harness_adapter_core::Error::Io { path, source } => Error::Io { path, source },
111 agent_harness_adapter_core::Error::Internal(m) => Error::Internal(m),
112 other => Error::Harness(other.to_string()),
113 }
114 }
115}
116
117impl Error {
118 pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
120 Error::Io {
121 path: path.into(),
122 source,
123 }
124 }
125
126 pub fn config(path: RepoPath, message: impl Into<String>) -> Self {
128 Error::Config {
129 path,
130 message: message.into(),
131 }
132 }
133
134 pub fn formats(path: RepoPath, message: impl Into<String>) -> Self {
136 Error::FormatsFile {
137 path,
138 message: message.into(),
139 }
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use std::{error::Error as _, io, path::Path};
146
147 use super::*;
148
149 #[test]
150 fn io_constructor_and_display() {
151 let e = Error::io(
152 Path::new("/tmp/x"),
153 io::Error::new(io::ErrorKind::PermissionDenied, "denied"),
154 );
155 assert!(matches!(e, Error::Io { .. }));
156 let msg = e.to_string();
157 assert!(
158 msg.contains("/tmp/x"),
159 "the message contains the path: {msg}"
160 );
161 assert!(
162 msg.contains("denied"),
163 "the message contains the source error: {msg}"
164 );
165 assert!(e.source().is_some());
166 }
167
168 #[test]
169 fn a_kit_error_is_an_io_error_or_a_harness_refusal() {
170 let e: Error = agent_harness_adapter_core::Error::io(
171 "/tmp/x",
172 io::Error::new(io::ErrorKind::PermissionDenied, "denied"),
173 )
174 .into();
175 assert!(matches!(e, Error::Io { .. }), "{e}");
176 let e: Error = agent_harness_adapter_core::Error::file("/.sokf/config.toml", "bad").into();
177 assert_eq!(e.to_string(), "harness: /.sokf/config.toml: bad");
178 let e: Error = agent_harness_adapter_core::Error::Internal("bug".into()).into();
179 assert_eq!(e.to_string(), "internal: bug");
180 let e: Error = agent_harness_adapter_core::Error::UnknownHarness {
181 harness: "cline".into(),
182 }
183 .into();
184 assert_eq!(e.to_string(), "harness: no harness named `cline`");
185 }
186
187 #[test]
188 fn every_message_names_its_subject() {
189 let path = RepoPath::parse("/.sokf/config.toml").unwrap();
190 assert_eq!(
191 Error::config(path.clone(), "unknown key").to_string(),
192 "/.sokf/config.toml: unknown key"
193 );
194 assert_eq!(
195 Error::formats(path, "bad").to_string(),
196 "/.sokf/config.toml: bad"
197 );
198 assert_eq!(
199 Error::Path("x".into()).to_string(),
200 "x: no such path in the repository"
201 );
202 assert_eq!(Error::New("no schema".into()).to_string(), "new: no schema");
203 assert_eq!(Error::Info("x".into()).to_string(), "info: x");
204 assert_eq!(Error::Init("x".into()).to_string(), "init: x");
205 assert_eq!(Error::Harness("x".into()).to_string(), "harness: x");
206 assert_eq!(Error::Start("x".into()).to_string(), "graph: x");
207 assert_eq!(Error::Locate("x".into()).to_string(), "locate: x");
208 assert_eq!(Error::Guide("x".into()).to_string(), "guide: x");
209 assert_eq!(Error::Query("x".into()).to_string(), "query: x");
210 assert_eq!(Error::Internal("x".into()).to_string(), "internal: x");
211 assert!(Error::NoRepository.to_string().contains("--repo-root"));
212 assert!(Error::NoRepository.source().is_none());
213 }
214}