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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! A declarative macro for type-safe enum-to-numbers conversion. `no-std` supported!
//!
//! ```
//! use numeric_enum_macro::{numeric_enum, numeric_enum_ident};
//!
//! numeric_enum! {
//!     #[repr(i64)] // repr must go first.
//!     /// Some docs.
//!     ///
//!     /// Multiline docs works too.
//!     #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] // all the attributes are forwarded!
//!     pub enum Lol {
//!         // All the constants must have explicit values assigned!
//!         Kek = 14,
//!         Wow = 87,
//!     }
//! }
//!
//! const KEK: u32 = 0;
//! const WOW: u32 = 1;
//!
//! // Unfortunately, it is not currently possible to use one macros to parse both literals and
//! // identifiers, so if you want to set up a macro using constants, use `numeric_enum_ident!`
//! // macro instead.
//! numeric_enum_ident! {
//!     #[repr(u32)] // repr must go first.
//!     /// Some docs.
//!     ///
//!     /// Multiline docs works too.
//!     #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] // all the attributes are forwarded!
//!     pub enum Lol2 {
//!         // All the constants must have explicit values assigned!
//!         Kek = KEK,
//!         Wow = WOW,
//!     }
//! }
//!
//! # use ::core::convert::TryFrom;
//! // Conversion to raw number:
//! assert_eq!(14i64, Lol::Kek.into());
//! // Conversion from raw number:
//! assert_eq!(Ok(Lol::Wow), Lol::try_from(87));
//! // Unknown number:
//! assert_eq!(Err(88), Lol::try_from(88));
//!
//! assert_eq!(Ok(Lol2::Wow), Lol2::try_from(WOW));
//! ```

#![no_std]

/// Declares an enum with a given numeric representation defined by literals.
///
/// Only explicetly enumerated enum constants are supported.
///
/// Automatically derives `TryFrom<$repr>` and `From<$name>`.
///
/// For examples look at the crate root documentation.
#[macro_export]
macro_rules! numeric_enum {
    (#[repr($repr:ident)]
     $(#$attrs:tt)* $vis:vis enum $name:ident {
        $($enum:ident = $constant:expr),* $(,)?
    } ) => {
        #[repr($repr)]
        $(#$attrs)*
        $vis enum $name {
            $($enum = $constant),*
        }

        impl ::core::convert::TryFrom<$repr> for $name {
            type Error = $repr;

            fn try_from(value: $repr) -> ::core::result::Result<Self, $repr> {
                match value {
                    $($constant => Ok($name :: $enum),)*
                    other => Err(other),
                }
            }
        }

        impl ::core::convert::From<$name> for $repr {
            fn from(value: $name) -> $repr {
                match value {
                    $($name :: $enum => $constant,)*
                }
            }
        }
    }
}

/// Declares an enum with a given numeric representation defined by identifiers.
///
/// Only explicetly enumerated enum constants are supported.
///
/// Automatically derives `TryFrom<$repr>` and `From<$name>`.
///
/// For examples look at the crate root documentation.
#[macro_export]
macro_rules! numeric_enum_ident {
    (#[repr($repr:ident)]
     $(#$attrs:tt)* $vis:vis enum $name:ident {
        $($enum:ident = $constant:ident),* $(,)?
    } ) => {
        #[repr($repr)]
        $(#$attrs)*
        $vis enum $name {
            $($enum = $constant),*
        }

        impl ::core::convert::TryFrom<$repr> for $name {
            type Error = $repr;

            fn try_from(value: $repr) -> ::core::result::Result<Self, $repr> {
                match value {
                    $($constant => Ok($name :: $enum),)*
                    other => Err(other),
                }
            }
        }

        impl ::core::convert::From<$name> for $repr {
            fn from(value: $name) -> $repr {
                match value {
                    $($name :: $enum => $constant,)*
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    numeric_enum! {
        #[repr(i16)]
        /// Documentation.
        ///
        /// Multiline.
        #[derive(Debug, PartialEq, Eq)]
        pub enum PublicEnum { Zero = 0, Lol = -1 }
    }

    numeric_enum! {
        #[repr(u8)]
        enum TrailingComa { A = 0, B = 1, }
    }

    numeric_enum! {
        #[repr(u8)]
        enum NoTrailingComa { A = 0, B = 1 }
    }

    const ZERO: u8 = 0;
    const LOL: u8 = 1;

    numeric_enum_ident! {
        #[repr(u8)]
        enum PrivateEnum {
            Zero = ZERO,
            Lol = LOL,
        }
    }

    #[test]
    fn it_works() {
        use core::convert::TryFrom;

        assert_eq!(-1i16, PublicEnum::Lol.into());
        assert_eq!(PublicEnum::try_from(0), Ok(PublicEnum::Zero));
        assert_eq!(PublicEnum::try_from(-1), Ok(PublicEnum::Lol));
        assert_eq!(PublicEnum::try_from(2), Err(2));
    }
}