From acb67e3d7fef9e83e0be3150d42af3dd02e33fa6 Mon Sep 17 00:00:00 2001 From: "C. Morgan Hamill" Date: Thu, 19 Mar 2015 11:43:51 -0400 Subject: Add `NotmuchEnum` trait and `notmuch_enum!` macro. Implements enum types in pairs, specifying the type and variant names of each, like so: notmuch_enum! { pub enum enum_name => EnumName { variant_one => VariantOne, variant_two => VariantTwo } } Which expands to: pub enum enum_name { variant_one, variant_two } pub enum EnumName { VariantOne, VariantTwo } The `NotmuchEnum` trait also entails two functions to convert between the defined pairs, `from_notmuch_t()` and `to_notmuch_t()`. The macro takes care of their implementation, also. Yes, this is purely aesthetic whimsy: I wanted the types in the `ffi` module to match the types from the `notmuch.h` file, and I wanted the types I used within (and exported by) this crate to match the expected Rust convention. --- src/macros.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/macros.rs (limited to 'src/macros.rs') diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..836be63 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,35 @@ +#[macro_escape] +macro_rules! notmuch_enum { + ( + $(#[$enum_attr:meta])* + pub enum $name:ident => $name_alias:ident { + $($variant:ident => $variant_alias:ident),* + } + ) => { + $(#[$enum_attr])* + pub enum $name { + $($variant),* + } + + $(#[$enum_attr])* + pub enum $name_alias { + $($variant_alias),* + } + + impl NotmuchEnum for $name_alias { + type NotmuchT = $name; + + fn from_notmuch_t(notmuch_t: $name) -> Self { + match notmuch_t { + $($name::$variant => $name_alias::$variant_alias),* + } + } + + fn to_notmuch_t(self) -> $name { + match self { + $($name_alias::$variant_alias => $name::$variant),* + } + } + } + } +} -- cgit v1.2.1