1 | /// A macro which defines an enum type. |
2 | macro_rules! enum_builder { |
3 | ( |
4 | $(#[doc = $comment:literal])* |
5 | #[repr($uint:ty)] |
6 | $enum_vis:vis enum $enum_name:ident |
7 | { |
8 | $( $enum_var:ident => $enum_val:literal),* $(,)? |
9 | $( !Debug: |
10 | $( $enum_var_nd:ident => $enum_val_nd:literal),* $(,)? |
11 | )? |
12 | } |
13 | ) => { |
14 | $(#[doc = $comment])* |
15 | #[non_exhaustive] |
16 | #[derive(PartialEq, Eq, Clone, Copy)] |
17 | $enum_vis enum $enum_name { |
18 | $( $enum_var),* |
19 | $(, $($enum_var_nd),* )? |
20 | ,Unknown($uint) |
21 | } |
22 | |
23 | impl $enum_name { |
24 | // NOTE(allow) generated irrespective if there are callers |
25 | #[allow(dead_code)] |
26 | $enum_vis fn to_array(self) -> [u8; core::mem::size_of::<$uint>()] { |
27 | <$uint>::from(self).to_be_bytes() |
28 | } |
29 | |
30 | // NOTE(allow) generated irrespective if there are callers |
31 | #[allow(dead_code)] |
32 | $enum_vis fn as_str(&self) -> Option<&'static str> { |
33 | match self { |
34 | $( $enum_name::$enum_var => Some(stringify!($enum_var))),* |
35 | $(, $( $enum_name::$enum_var_nd => Some(stringify!($enum_var_nd))),* )? |
36 | ,$enum_name::Unknown(_) => None, |
37 | } |
38 | } |
39 | } |
40 | |
41 | impl Codec<'_> for $enum_name { |
42 | // NOTE(allow) fully qualified Vec is only needed in no-std mode |
43 | #[allow(unused_qualifications)] |
44 | fn encode(&self, bytes: &mut alloc::vec::Vec<u8>) { |
45 | <$uint>::from(*self).encode(bytes); |
46 | } |
47 | |
48 | fn read(r: &mut Reader<'_>) -> Result<Self, crate::error::InvalidMessage> { |
49 | match <$uint>::read(r) { |
50 | Ok(x) => Ok($enum_name::from(x)), |
51 | Err(_) => Err(crate::error::InvalidMessage::MissingData(stringify!($enum_name))), |
52 | } |
53 | } |
54 | } |
55 | |
56 | impl From<$uint> for $enum_name { |
57 | fn from(x: $uint) -> Self { |
58 | match x { |
59 | $($enum_val => $enum_name::$enum_var),* |
60 | $(, $($enum_val_nd => $enum_name::$enum_var_nd),* )? |
61 | , x => $enum_name::Unknown(x), |
62 | } |
63 | } |
64 | } |
65 | |
66 | impl From<$enum_name> for $uint { |
67 | fn from(value: $enum_name) -> Self { |
68 | match value { |
69 | $( $enum_name::$enum_var => $enum_val),* |
70 | $(, $( $enum_name::$enum_var_nd => $enum_val_nd),* )? |
71 | ,$enum_name::Unknown(x) => x |
72 | } |
73 | } |
74 | } |
75 | |
76 | impl core::fmt::Debug for $enum_name { |
77 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
78 | match self { |
79 | $( $enum_name::$enum_var => f.write_str(stringify!($enum_var)), )* |
80 | _ => write!(f, "{}(0x{:x?})" , stringify!($enum_name), <$uint>::from(*self)), |
81 | } |
82 | } |
83 | } |
84 | }; |
85 | } |
86 | |