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
#[macro_export]
/// Generate a serializable enum.
/// 
/// An Unknown field is always generated, which contains a variable, which type is the enum representation.
/// ```
/// use macro_bits::serializable_enum;
/// 
/// serializable_enum! {
///     #[derive(Debug, PartialEq)]
///     pub enum ABC: u8 {
///         /// This is a doc comment too.
///         A => 0,
///         B => 1,
///         C => 2
///     }
/// }
/// assert_eq!(ABC::from_representation(2), ABC::C);
/// assert_eq!(ABC::from_representation(3), ABC::Unknown(3));
/// assert_eq!((ABC::C).to_representation(), 2);
/// ```
macro_rules! serializable_enum {
    (
        $(#[$enum_attr:meta])*
        $enum_visibility:vis enum $enum_name:ident : $representation:ty {
            $(
                $(#[$field_attr:meta])*
                $field_name:ident => $field_value:expr
            ),*
        }
    ) => {
        ::macro_bits::defile::defile!{
            $(#[$enum_attr])*
            $enum_visibility enum $enum_name {
                $(
                    $(#[$field_attr])*
                    $field_name,
                )*
                Unknown($representation)
            }
            impl $enum_name {
                pub fn from_representation(value: $representation) -> Self {
                    use $enum_name::*;
                    match value {
                        $(
                            $field_value => $field_name,
                        )*
                        x => Self::Unknown(x)
                    }
                }
                pub fn to_representation(self) -> $representation {
                    use $enum_name::*;
                    match self {
                        $(
                            $field_name => $field_value,
                        )*
                        Self::Unknown(x) => x
                    }
                }
            }
            ::macro_bits::generate_conversions!($representation, $representation, $enum_name);
        }
    };
}