1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#[macro_export]
macro_rules! impl_from_to_pax_any_for_from_to_pax_value {
    ($Type:ty) => {
        impl ToFromPaxAny for $Type {
            fn to_pax_any(self) -> PaxAny {
                self.to_pax_value().to_pax_any()
            }

            fn from_pax_any(pax_any: PaxAny) -> Result<Self, String> {
                PaxValue::from_pax_any(pax_any)
                    .and_then(|pax_value| Self::from_pax_value(pax_value))
            }

            fn ref_from_pax_any(pax_any: &PaxAny) -> Result<&Self, String> {
                PaxValue::ref_from_pax_any(pax_any)
                    .and_then(|pax_value| Self::ref_from_pax_value(pax_value))
            }

            fn mut_from_pax_any(pax_any: &mut PaxAny) -> Result<&mut Self, String> {
                PaxValue::mut_from_pax_any(pax_any)
                    .and_then(|pax_value| Self::mut_from_pax_value(pax_value))
            }
        }
    };
}

// This macro requires that the $Type can be created by calling into on the $Variant contents
#[macro_export]
macro_rules! impl_default_coercion_rule {
    ($Type:ty, $Variant:path) => {
        impl CoercionRules for $Type {
            fn try_coerce(pax_value: PaxValue) -> Result<Self, String> {
                if let $Variant(val) = pax_value {
                    Ok(val.into())
                } else {
                    Err(format!(
                        "cound't coerce {:?} into {}",
                        pax_value,
                        std::any::type_name::<$Type>()
                    ))
                }
            }
        }
    };
}

// This macro implements from and to
#[macro_export]
macro_rules! impl_to_from_pax_value {
    // For a single variant path
    ($Type:ty, $Variant:path) => {
        impl ToFromPaxValue for $Type {
            fn to_pax_value(self) -> PaxValue {
                $Variant(self)
            }

            fn from_pax_value(pax_value: PaxValue) -> Result<Self, String> {
                if let $Variant(val) = pax_value {
                    Ok(val)
                } else {
                    Err(format!(
                        "pax value {:?} cannot be coerced into {}",
                        pax_value,
                        std::any::type_name::<Self>(),
                    ))
                }
            }

            fn ref_from_pax_value(pax_value: &PaxValue) -> Result<&Self, String> {
                if let $Variant(ref val) = pax_value {
                    Ok(val)
                } else {
                    Err(format!(
                        "pax value cannot be coerced into {}",
                        std::any::type_name::<Self>(),
                    ))
                }
            }

            fn mut_from_pax_value(pax_value: &mut PaxValue) -> Result<&mut Self, String> {
                if let $Variant(ref mut val) = pax_value {
                    Ok(val)
                } else {
                    Err(format!(
                        "pax value cannot be coerced into {}",
                        std::any::type_name::<Self>(),
                    ))
                }
            }
        }

        impl_from_to_pax_any_for_from_to_pax_value!($Type);
    };
    // For nested variant paths like Numeric::U8
    // looks almost exactly the same as above, just with nested variant
    ($Type:ty, $OuterVariant:path, $InnerVariant:path) => {
        impl ToFromPaxValue for $Type {
            fn to_pax_value(self) -> PaxValue {
                $OuterVariant($InnerVariant(self))
            }

            fn from_pax_value(pax_value: PaxValue) -> Result<Self, String> {
                if let $OuterVariant($InnerVariant(val)) = pax_value {
                    Ok(val)
                } else {
                    Err(format!(
                        "pax value cannot be coerced into {}",
                        std::any::type_name::<Self>(),
                    ))
                }
            }

            fn ref_from_pax_value(pax_value: &PaxValue) -> Result<&Self, String> {
                if let $OuterVariant($InnerVariant(ref val)) = pax_value {
                    Ok(val)
                } else {
                    Err(format!(
                        "pax value cannot be coerced into {}",
                        std::any::type_name::<Self>(),
                    ))
                }
            }

            fn mut_from_pax_value(pax_value: &mut PaxValue) -> Result<&mut Self, String> {
                if let $OuterVariant($InnerVariant(ref mut val)) = pax_value {
                    Ok(val)
                } else {
                    Err(format!(
                        "pax value cannot be coerced into {}",
                        std::any::type_name::<Self>(),
                    ))
                }
            }
        }
        impl_from_to_pax_any_for_from_to_pax_value!($Type);
    };
}