Skip to main content

maps_io_ros/
error.rs

1//! Error handling for the `maps_io_ros` crate.
2
3use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// Error types for the `maps_io_ros` crate.
8/// Allows to wrap external errors in a unified way.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// An I/O error with additional context.
12    #[error("[IO error] {context} ({source})")]
13    Io {
14        context: String,
15        #[source]
16        source: std::io::Error,
17    },
18
19    /// An image loading error with additional context.
20    #[error("[Image error] {context} ({source})")]
21    Image {
22        context: String,
23        #[source]
24        source: image::ImageError,
25    },
26
27    /// YAML serialization or deserialization error with additional context.
28    #[error("[YAML error] {context} ({source})")]
29    Yaml {
30        context: String,
31        #[source]
32        source: serde_yaml_ng::Error,
33    },
34}
35
36/// Macro for generating wrapping error constructors with doc comments.
37#[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    // Generate the wrapping error constructors.
54    impl_error_constructors! {
55        io => Io, std::io::Error;
56        image => Image, image::ImageError;
57        yaml => Yaml, serde_yaml_ng::Error;
58    }
59}