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
//! Provides a macro for generating static structures used for value lookup.
//!
//! ```
//! # use reword::reword;
//! reword! {
//!     enum Lang: &'static str {
//!         Hi {
//!             NO = "Hei";
//!             EN_UK | EN_US = "Hi";
//!         }
//!     }
//! }
//!
//! let mut lang = Lang::NO;
//! assert_eq!(lang.reword::<Hi>(), "Hei");
//! lang = Lang::EN_UK;
//! assert_eq!(lang.reword::<Hi>(), "Hi");
//! lang = Lang::EN_US;
//! assert_eq!(lang.reword::<Hi>(), "Hi");
//! ```
//!
//! The structures generated are not exported out of its module by default.
//! Use `pub` before the`enum` keyword to export it.
//! Attributes can be attached to both the `enum` and the structures generated.
//! The `Copy`, `Clone`, `Debug`, `Eq`, `PartialEq`, `Ord`, `PartialOrd`, and `Hash` traits are
//! automatically derived for the types using the derive attribute. At the moment, the reword macro
//! can only be used once per module, so if you need to define multiple structures you should
//! put them in separate submodules.

#![no_std]
#![doc(html_root_url = "https://docs.rs/reword/latest")]
#![deny(
    bad_style,
    bare_trait_objects,
    missing_debug_implementations,
    missing_docs,
    unused_import_braces,
    unused_qualifications,
    unsafe_code,
    unstable_features
)]

/// The macro used to generate the lookup structures.
///
/// See the [crate level docs](index.html) for more information.
#[macro_export]
macro_rules! reword {
    (
        $(#[$outer:meta])*
        enum $reword:ident : $T:ty {
            $(#[$inner:meta])*
            $key:ident { $($($name:ident)|+ = $val:expr;)+ }
            $(
                $(#[$inner2:meta])*
                $key2:ident { $($($name2:ident)|+ = $val2:expr;)+ }
            )*
        }
    ) => {
        /// Trait used for lookup.
        trait Word {
            $($(#[allow(non_upper_case_globals)] const $name: $T;)+)+
        }

        $(#[$outer])*
        #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
        enum $reword {
            $($(#[allow(non_camel_case_types)] $name,)+)+
        }

        impl $reword {
            /// Get the value corresponding to the value of `self`.
            #[inline]
            fn reword<W: Word + ?Sized>(self) -> $T {
                match self {
                    $($($reword::$name => W::$name,)+)+
                }
            }
        }

        $(#[$inner])*
        #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
        struct $key;

        impl Word for $key {
            $($(#[allow(non_upper_case_globals)] const $name: $T = $val;)+)+
        }

        $(
            $(#[$inner2])*
            #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
            struct $key2;

            impl Word for $key2 {
                $($(#[allow(non_upper_case_globals)] const $name2: $T = $val2;)+)+
            }
        )*
    };
    (
        $(#[$outer:meta])*
        pub enum $reword:ident : $T:ty {
            $(#[$inner:meta])*
            $key:ident { $($($name:ident)|+ = $val:expr;)+ }
            $(
                $(#[$inner2:meta])*
                $key2:ident { $($($name2:ident)|+ = $val2:expr;)+ }
            )*
        }
    ) => {
        /// Trait used for lookup.
        pub trait Word {
            $($(#[allow(non_upper_case_globals)] const $name: $T;)+)+
        }

        $(#[$outer])*
        #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
        pub enum $reword {
            $($(#[allow(non_camel_case_types)] $name,)+)+
        }

        impl $reword {
            /// Get the value corresponding to the value of `self`.
            #[inline]
            pub fn reword<W: Word + ?Sized>(self) -> $T {
                match self {
                    $($($reword::$name => W::$name,)+)+
                }
            }
        }

        $(#[$inner])*
        #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
        pub struct $key;

        impl Word for $key {
            $($(#[allow(non_upper_case_globals)] const $name: $T = $val;)+)+
        }

        $(
            $(#[$inner2])*
            #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
            pub struct $key2;

            impl Word for $key2 {
                $($(#[allow(non_upper_case_globals)] const $name2: $T = $val2;)+)+
            }
        )*
    };
}