Crate reword [−] [src]
A macro for generating types that allows for fast lookup of const
values at runtime.
Examples
#[macro_use] extern crate reword; reword! { enum Lang: &'static str { Hi { EN_UK | EN_US = "Hi"; NO = "Hei"; } HowAreYou { EN_UK = "How are you?"; EN_US = "How you doing?"; NO = "Hvordan går det?"; } } } fn main() { let mut lang = Lang::NO; assert_eq!(lang.reword::<Hi>(), "Hei"); lang = Lang::EN_UK; assert_eq!(lang.reword::<HowAreYou>(), "How are you?"); lang = Lang::EN_US; assert_eq!(lang.reword::<HowAreYou>(), "How you doing?"); }
Visibility
The generated types are not exported out of its module by default.
Use pub
before the enum
to export it.
#[macro_use] extern crate reword; mod example { reword! { pub enum Lang: &'static str { Hi { EN_UK | EN_US = "Hi"; NO = "Hei"; } HowAreYou { EN_UK = "How are you?"; EN_US = "How you doing?"; NO = "Hvordan går det?"; } } } } fn main() { let mut lang = example::Lang::NO; assert_eq!(lang.reword::<example::Hi>(), "Hei"); lang = example::Lang::EN_UK; assert_eq!(lang.reword::<example::HowAreYou>(), "How are you?"); lang = example::Lang::EN_US; assert_eq!(lang.reword::<example::HowAreYou>(), "How you doing?"); }
Attributes
Attributes can be attached to both the enum
and the structs
generated.
The Copy
, Clone
, Debug
, Eq
, PartialEq
, Ord
, PartialOrd
, and Hash
traits are
automatically derived for the types using the derive
attribute.
#[macro_use] extern crate reword; #[repr(C)] #[derive(Debug, PartialEq)] enum Code { Hex(u32), Rgb(u8, u8, u8), } reword! { #[repr(C)] enum Color: Code { #[repr(C)] Red { Rgb = Code::Rgb(255, 0, 0); Hex = Code::Hex(0xff0000); } } } fn main() { let mut color = Color::Rgb; assert_eq!(color.reword::<Red>(), Code::Rgb(255, 0, 0)); color = Color::Hex; assert_eq!(color.reword::<Red>(), Code::Hex(0xff0000)); }
Macros
reword |
The macro used to generate the lookup types. |