use std::{fmt, io};
#[derive(Debug)]
pub enum Error {
Core(agent_harness_adapter_core::Error),
Io(io::Error),
Usage(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Core(e) => e.fmt(f),
Error::Io(e) => e.fmt(f),
Error::Usage(m) => f.write_str(m),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Core(e) => Some(e),
Error::Io(e) => Some(e),
Error::Usage(_) => None,
}
}
}
impl From<agent_harness_adapter_core::Error> for Error {
fn from(e: agent_harness_adapter_core::Error) -> Self {
Error::Core(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
#[cfg(test)]
mod tests {
use std::error::Error as _;
use super::*;
#[test]
fn each_kind_displays_and_chains() {
let core = Error::from(agent_harness_adapter_core::Error::Refused("no".into()));
assert_eq!(core.to_string(), "no");
assert!(core.source().is_some());
let io = Error::from(io::Error::new(io::ErrorKind::BrokenPipe, "pipe"));
assert_eq!(io.to_string(), "pipe");
assert!(agent_harness_adapter_core::cli::is_broken_pipe(&io));
let usage = Error::Usage("set HOME".into());
assert_eq!(usage.to_string(), "set HOME");
assert!(usage.source().is_none());
}
}