use std::path::PathBuf;
use crate::RepoPath;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("not in a Git repository; pass --repo-root")]
NoRepository,
#[error("{path}: {message}")]
Config {
path: RepoPath,
message: String,
},
#[error("{path}: {message}")]
FormatsFile {
path: RepoPath,
message: String,
},
#[error("{0}: no such path in the repository")]
Path(String),
#[error("init: {0}")]
Init(String),
#[error("harness: {0}")]
Harness(String),
#[error("new: {0}")]
New(String),
#[error("info: {0}")]
Info(String),
#[error("graph: {0}")]
Start(String),
#[error("locate: {0}")]
Locate(String),
#[error("query: {0}")]
Query(String),
#[error("I/O error at {path}: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[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),
}
}
}
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 {
pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Error::Io {
path: path.into(),
source,
}
}
pub fn config(path: RepoPath, message: impl Into<String>) -> Self {
Error::Config {
path,
message: message.into(),
}
}
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::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());
}
}