Skip to main content

gam_model_kernels/
macros.rs

1//! The shared `impl_reason_error_boilerplate!` derive used by every
2//! `{ reason: String }`-shaped error enum across the `gam-model-kernels` and
3//! `gam-models` crates. The macro is pure, dependency-free boilerplate
4//! (`Display` / `Error` / `From<_> for String`), so it lives in this base
5//! kernel crate and is `#[macro_export]`ed back up to `gam-models` (which
6//! brings it into crate-wide textual scope via `#[macro_use] extern crate
7//! gam_model_kernels;`).
8
9#[macro_export]
10macro_rules! impl_reason_error_boilerplate {
11    ($type:ident { $($variant:ident),+ $(,)? }) => {
12        impl ::std::fmt::Display for $type {
13            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
14                match self {
15                    $(Self::$variant { reason })|+ => f.write_str(reason),
16                }
17            }
18        }
19
20        impl ::std::error::Error for $type {}
21
22        impl From<$type> for String {
23            fn from(err: $type) -> String {
24                err.to_string()
25            }
26        }
27    };
28}