diff options
| author | C. Morgan Hamill <me@cmhamill.org> | 2015-03-19 16:43:51 +0100 |
|---|---|---|
| committer | C. Morgan Hamill <me@cmhamill.org> | 2015-03-19 18:59:52 +0100 |
| commit | acb67e3d7fef9e83e0be3150d42af3dd02e33fa6 (patch) | |
| tree | d0114011907d5a137f0c118f9ef44dc1cd692b57 /src/macros.rs | |
| parent | a0f345202299b5f5214f1a791e896376e6cb7a04 (diff) | |
| download | mail-acb67e3d7fef9e83e0be3150d42af3dd02e33fa6.tar.gz | |
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.
Diffstat (limited to 'src/macros.rs')
| -rw-r--r-- | src/macros.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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),* + } + } + } + } +} |
