use thiserror::Error;
#[cfg(feature = "http")]
use docspec_http::server::ServerError;
pub type Result<T> = core::result::Result<T, CliError>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CliError {
#[error(transparent)]
Conversion(#[from] docspec_core::Error),
#[error("{message}")]
FormatDetection {
message: String,
},
#[cfg(feature = "http")]
#[error("HTTP server error: {0}")]
Http(#[from] ServerError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("input and output paths refer to the same file")]
SameInputOutput,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_conversion_error() {
let inner = docspec_core::Error::Other {
message: "pipeline failed".to_string(),
};
let err = CliError::Conversion(inner);
assert!(err.to_string().contains("pipeline failed"));
}
#[test]
fn display_format_detection_error() {
let err = CliError::FormatDetection {
message: "cannot detect format".to_string(),
};
assert_eq!(err.to_string(), "cannot detect format");
}
#[test]
fn display_io_error() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let err = CliError::Io(io_err);
assert!(err.to_string().contains("file not found"));
}
#[test]
fn display_same_input_output() {
let err = CliError::SameInputOutput;
assert!(err.to_string().contains("same file"));
}
#[test]
fn from_docspec_error() {
let inner = docspec_core::Error::Other {
message: "test".to_string(),
};
let err = CliError::from(inner);
assert!(matches!(err, CliError::Conversion(_)));
}
#[test]
fn from_io_error() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
let err = CliError::from(io_err);
assert!(matches!(err, CliError::Io(_)));
}
}