Skip to main content

gam_problem/
macros.rs

1macro_rules! impl_reason_error_boilerplate {
2    ($type:ident { $($variant:ident),+ $(,)? }) => {
3        impl ::std::fmt::Display for $type {
4            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
5                match self {
6                    $(Self::$variant { reason })|+ => f.write_str(reason),
7                }
8            }
9        }
10
11        impl ::std::error::Error for $type {}
12
13        impl From<$type> for String {
14            fn from(err: $type) -> String {
15                err.to_string()
16            }
17        }
18    };
19}
20
21#[macro_export]
22macro_rules! bail_invalid_estim {
23    ($fmt:literal $(, $($arg:tt)*)?) => {
24        return Err($crate::EstimationError::InvalidInput(format!($fmt $(, $($arg)*)?)))
25    };
26    ($msg:expr $(,)?) => {
27        return Err($crate::EstimationError::InvalidInput($msg))
28    };
29}
30
31#[macro_export]
32macro_rules! bail_invalid_basis {
33    ($fmt:literal $(, $($arg:tt)*)?) => {
34        return Err($crate::BasisError::InvalidInput(format!($fmt $(, $($arg)*)?)))
35    };
36    ($msg:expr $(,)?) => {
37        return Err($crate::BasisError::InvalidInput($msg))
38    };
39}
40
41#[macro_export]
42macro_rules! bail_dim_basis {
43    ($fmt:literal $(, $($arg:tt)*)?) => {
44        return Err($crate::BasisError::DimensionMismatch(format!($fmt $(, $($arg)*)?)))
45    };
46    ($msg:expr $(,)?) => {
47        return Err($crate::BasisError::DimensionMismatch($msg))
48    };
49}
50
51#[macro_export]
52macro_rules! bail_dim_custom {
53    ($fmt:literal $(, $($arg:tt)*)?) => {
54        return Err($crate::CustomFamilyError::DimensionMismatch { reason: format!($fmt $(, $($arg)*)?) })
55    };
56    ($msg:expr $(,)?) => {
57        return Err($crate::CustomFamilyError::DimensionMismatch { reason: $msg })
58    };
59}