pax_runtime_api/pax_value/
macros.rs

1// This macro requires that the $Type can be created by calling into on the $Variant contents
2#[macro_export]
3macro_rules! impl_default_coercion_rule {
4    ($Type:ty, $Variant:path) => {
5        impl CoercionRules for $Type {
6            fn try_coerce(pax_value: PaxValue) -> Result<Self, String> {
7                if let $Variant(val) = pax_value {
8                    Ok(val.into())
9                } else {
10                    Err(format!(
11                        "couldn't coerce {:?} into {}",
12                        pax_value,
13                        std::any::type_name::<$Type>()
14                    ))
15                }
16            }
17        }
18    };
19}
20
21// This macro implements from and to
22#[macro_export]
23macro_rules! impl_to_from_pax_value {
24    // For a single variant path
25    ($Type:ty, $Variant:path) => {
26        impl ToPaxValue for $Type {
27            fn to_pax_value(self) -> PaxValue {
28                $Variant(self)
29            }
30        }
31    };
32    // For nested variant paths like Numeric::U8
33    // looks almost exactly the same as above, just with nested variant
34    ($Type:ty, $OuterVariant:path, $InnerVariant:path) => {
35        impl ToPaxValue for $Type {
36            fn to_pax_value(self) -> PaxValue {
37                $OuterVariant($InnerVariant(self))
38            }
39        }
40    };
41}