use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("[IO error] {context} ({source})")]
Io {
context: String,
#[source]
source: std::io::Error,
},
#[error("[Image error] {context} ({source})")]
Image {
context: String,
#[source]
source: image::ImageError,
},
#[error("[YAML error] {context} ({source})")]
Yaml {
context: String,
#[source]
source: serde_yaml_ng::Error,
},
}
#[macro_export]
macro_rules! impl_error_constructors {
($($method_name:ident => $variant:ident, $error_type:ty);* $(;)?) => {
$(
#[doc = concat!("Wrap a `", stringify!($error_type), "` with additional context message.")]
pub fn $method_name(context: impl ToString, source: $error_type) -> Self {
Self::$variant {
context: context.to_string(),
source,
}
}
)*
};
}
impl Error {
impl_error_constructors! {
io => Io, std::io::Error;
image => Image, image::ImageError;
yaml => Yaml, serde_yaml_ng::Error;
}
}