use std::path::PathBuf;
use thiserror::Error;
use difflore_core::CoreError;
#[derive(Error, Debug)]
pub enum CliError {
#[error("{0}")]
Core(#[from] CoreError),
#[error("{0}")]
Message(String),
#[error("config at {path}: {message}")]
Config { path: PathBuf, message: String },
}
impl CliError {
pub fn msg<S: Into<String>>(s: S) -> Self {
Self::Message(s.into())
}
}
impl From<String> for CliError {
fn from(s: String) -> Self {
Self::Message(s)
}
}
impl From<&str> for CliError {
fn from(s: &str) -> Self {
Self::Message(s.to_owned())
}
}
pub type CliResult<T> = Result<T, CliError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn underlying_io_error_funnels_through_core_with_context() {
fn fails() -> CliResult<()> {
Err(CoreError::Io(std::io::Error::other("disk full")))?;
Ok(())
}
let err = fails().expect_err("expected error");
assert!(matches!(err, CliError::Core(CoreError::Io(_))));
assert_eq!(err.to_string(), "IO error: disk full");
}
}