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
macro_rules! generate_matching_enum_impl {
    (
        $(#[doc = $docs:literal])*
        #[repr($repr_type:ty)]
        $(#[$enum_meta:meta])*
        $enum_vis:vis enum $enum_name:ident {
            $(
                $(#[$variant_meta:meta])*
                $variant_name:ident = $variant_value:expr,
            )*
        }
    ) => {
         paste::paste! {
            $(#[doc = $docs])*
            $(#[$enum_meta])*
            #[repr($repr_type)]
            $enum_vis enum $enum_name {
                $(
                    $(#[$variant_meta])*
                    $variant_name = $variant_value,
                )*
            }

            impl core::fmt::Display for $enum_name {
                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    write!(f, "{:?}", self)
                }
            }

            impl core::convert::TryFrom<$repr_type> for $enum_name {
                type Error = [<NoRecognized $enum_name Error>];

                #[inline]
                fn try_from(value: $repr_type) -> core::result::Result<Self, [<NoRecognized $enum_name Error>]> {
                    match value {
                        $($variant_value => core::result::Result::Ok($enum_name::$variant_name),)*
                        _ => core::result::Result::Err([<NoRecognized $enum_name Error>] {
                            [<$enum_name:snake>]: value
                        }),
                    }
                }
            }

            #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
            $enum_vis struct [<NoRecognized $enum_name Error>] {
                 pub [<$enum_name:snake>]: $repr_type
            }

            impl core::fmt::Display for [<NoRecognized $enum_name Error>] {
                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    write!(f, "Not a valid header value , was: {:?}", self.[<$enum_name:snake>])
                }
            }

            #[cfg(all(feature = "error_trait", not(feature = "std")))]
            impl core::error::Error for [<NoRecognized $enum_name Error>] {}

            #[cfg(feature = "std")]
            impl std::error::Error for [<NoRecognized $enum_name Error>] {}

            // Kani verification to ensure the code does not panic on any input.
            #[cfg(kani)]
            mod [<$enum_name:lower _verification>] {
                use super::*;

                #[kani::proof]
                fn [<$enum_name:snake _proof>]() {
                    let try_value = kani::any::<$repr_type>();
                    match $enum_name::try_from(try_value) {
                        Ok(_) => {}
                        Err(err) => {
                            assert_eq!([<NoRecognized $enum_name Error>]{[<$enum_name:snake>]:try_value}, err);
                        }
                    }
                }
            }
        }
    }
}
pub(crate) use generate_matching_enum_impl;