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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/// A helper hacro to define a type that can be used in field value.
///
/// It can define either an enum or a newtype struct. It also implement `into` and `try_from` for
/// the given interger types. For an example of the generated types, see the
/// [example module](crate::example::field_type).
///
/// # Usage
///
/// ```
/// use core::convert::{TryFrom, TryInto};
///
/// // Define an enum
/// peripherals::field_type! {
///     enum Mode [u8, u16] {
///         A = 0,
///         B = 1,
///         C = 2,
///         D = 3,
///     }
/// }
///
/// // It implements into and try_from for given interger types (here u8 and u16)
/// assert_eq!(Into::<u8>::into(Mode::A), 0);
/// assert_eq!(u16::from(Mode::B), 1);
/// assert_eq!(TryInto::<Mode>::try_into(2_u8), Ok(Mode::C));
/// assert_eq!(Mode::try_from(3_u16), Ok(Mode::D));
///
/// // Define a newtype struct
/// peripherals::field_type! {
///     struct Data [u16] (u8);
/// }
///
/// // It implements into and try_from for given interger types (here u16)
/// assert_eq!(u16::from(Data(10)), 10);
/// assert_eq!(Data::try_from(20), Ok(Data(20)));
/// ```
///
/// It also implements `Not` for enum with two fields and newtypes over bool. Fields with these
/// types can be toggled.
///
/// ```
/// use core::convert::{TryFrom, TryInto};
///
/// // Define an enum with two variants
/// peripherals::field_type! {
///     enum State [] {
///         Low = 0,
///         High = 1,
///     }
/// }
///
/// // It implements Not
/// assert_eq!(!State::Low, State::High);
/// assert_eq!(!State::High, State::Low);
///
/// // Define a newtype struct over a bool
/// peripherals::field_type! {
///     struct Status [] (bool);
/// }
///
/// // It implements Not
/// assert_eq!(!Status(true), Status(false));
/// assert_eq!(!Status(false), Status(true));
/// ```

#[macro_export]
macro_rules! field_type {
    ($(#[$($attr:tt)*])* enum $name:ident $int:tt {
        $(#[$variant1_attr:meta])*
        $variant1:ident = $value1:literal,
        $(#[$variant2_attr:meta])*
        $variant2:ident = $value2:literal $(,)?
    }) => {
        $crate::periph_attr_inner! { @type { $([$($attr)*])* } {} {
        periph_attr_inner: @expand
            #[derive(Copy, Clone, Debug, PartialEq, Eq)]
            pub enum $name {
                $(#[$variant1_attr])*
                $variant1 = $value1,
                $(#[$variant2_attr])*
                $variant2 = $value2,
            }
        }}

        $crate::periph_attr_inner! { @impl { $([$($attr)*])* } {} {
        periph_attr_inner: @expand
            impl ::core::ops::Not for $name {
                type Output = $name;

                #[inline]
                fn not(self) -> $name {
                    match self {
                        $name::$variant1 => $name::$variant2,
                        $name::$variant2 => $name::$variant1,
                    }
                }
            }
        }}

        $crate::periph_attr_inner! { @impl { $([$($attr)*])* } {} {
        field_type_inner: @outer $name value $int:
            value as _;
            match value {
                $value1 => ::core::result::Result::Ok($name::$variant1),
                $value2 => ::core::result::Result::Ok($name::$variant2),
                _ => ::core::result::Result::Err($crate::InvalidValue),
            };
        }}
    };
    ($(#[$($attr:tt)*])* enum $name:ident $int:tt {$(
        $(#[$variant_attr:meta])*
        $variant:ident = $value:literal
    ),*$(,)?}) => {
        $crate::periph_attr_inner! { @type { $([$($attr)*])* } {} {
        periph_attr_inner: @expand
            #[derive(Copy, Clone, Debug, PartialEq, Eq)]
            pub enum $name {$(
                $(#[$variant_attr])*
                $variant = $value
            ),*}
        }}

        $crate::periph_attr_inner! { @impl { $([$($attr)*])* } {} {
        field_type_inner: @outer $name value $int:
            value as _;
            match value {
                $($value => ::core::result::Result::Ok($name::$variant),)*
                _ => ::core::result::Result::Err($crate::InvalidValue),
            };
        }}
    };
    ($(#[$($attr:tt)*])* struct $name:ident $int:tt (bool);) => {
        $crate::periph_attr_inner! { @type { $([$($attr)*])* } {} {
        periph_attr_inner: @expand
            #[derive(Copy, Clone, Debug, PartialEq, Eq)]
            pub struct $name(pub bool);
        }}

        $crate::periph_attr_inner! { @impl { $([$($attr)*])* } {} {
        periph_attr_inner: @expand
            impl ::core::ops::Not for $name {
                type Output = $name;

                #[inline]
                fn not(self) -> $name {
                    $name(!self.0)
                }
            }
        }}

        $crate::periph_attr_inner! { @impl { $([$($attr)*])* } {} {
        field_type_inner: @outer $name value $int:
            ::core::convert::Into::into(value.0);
            ::core::result::Result::Ok($name(value != 0));
        }}
    };
    ($(#[$($attr:tt)*])* struct $name:ident $int:tt ($inner:ty);) => {
        $crate::periph_attr_inner! { @type { $([$($attr)*])* } {} {
        periph_attr_inner: @expand
            #[derive(Copy, Clone, Debug, PartialEq, Eq)]
            pub struct $name(pub $inner);
        }}

        $crate::periph_attr_inner! { @impl { $([$($attr)*])* } {} {
        field_type_inner: @outer $name value $int:
            ::core::convert::Into::into(value.0);
            ::core::convert::TryInto::try_into(value).map($name).map_err(|_| $crate::InvalidValue);
        }}
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! field_type_inner {
    (@outer $(#[$attr:meta])* $name:ident $value:ident [$($int:ty),*]: $to_type:expr; $from_type:expr;) => {
        $crate::field_type_inner!(@inner {$(#[$attr])*} $name $value [$($int),*]: $to_type; $from_type;);
    };
    (@inner $attr:tt $name:ident $value:ident [$($int:ty),*]: $to_type:expr; $from_type:expr;) => {
        $( $crate::field_type_inner!(@impl $attr $name $value $int: $to_type; $from_type;); )*
    };
    (@impl { $(#[$attr:meta])* } $name:ident $value:ident $int:ty: $to_type:expr; $from_type:expr; ) => {
        $(#[$attr])*
        impl ::core::convert::From<$name> for $int {
            #[inline]
            fn from($value: $name) -> $int {
                $to_type
            }
        }

        $(#[$attr])*
        impl ::core::convert::TryFrom<$int> for $name {
            type Error = $crate::InvalidValue;

            #[inline]
            fn try_from($value: $int) -> ::core::result::Result<$name, $crate::InvalidValue> {
                $from_type
            }
        }
    };
}