1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Error, Debug)]
10pub enum Error {
11 #[error("[IO error] {context} ({source})")]
13 Io {
14 context: String,
15 #[source]
16 source: std::io::Error,
17 },
18
19 #[error("[Image error] {context} ({source})")]
21 Image {
22 context: String,
23 #[source]
24 source: image::ImageError,
25 },
26
27 #[error("[YAML error] {context} ({source})")]
29 Yaml {
30 context: String,
31 #[source]
32 source: serde_yaml_ng::Error,
33 },
34}
35
36#[macro_export]
38macro_rules! impl_error_constructors {
39 ($($method_name:ident => $variant:ident, $error_type:ty);* $(;)?) => {
40 $(
41 #[doc = concat!("Wrap a `", stringify!($error_type), "` with additional context message.")]
42 pub fn $method_name(context: impl ToString, source: $error_type) -> Self {
43 Self::$variant {
44 context: context.to_string(),
45 source,
46 }
47 }
48 )*
49 };
50}
51
52impl Error {
53 impl_error_constructors! {
55 io => Io, std::io::Error;
56 image => Image, image::ImageError;
57 yaml => Yaml, serde_yaml_ng::Error;
58 }
59}