# enum_macro_gen   [![Build Status]][actions] [![Latest Version]][crates.io] [![Docs badge]][Docs link] [![License badge]][License link] [Build Status]: https://github.com/da-x/enum-macro-gen/actions/workflows/test.yml/badge.svg [actions]: https://github.com/da-x/enum-macro-gen/actions [Latest Version]: https://img.shields.io/crates/v/enum_macro_gen.svg [crates.io]: https://crates.io/crates/enum_macro_gen [License badge]: https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg [License link]: https://travis-ci.org/da-x/enum_macro_gen [Docs badge]: https://docs.rs/enum_macro_gen/badge.svg [Docs link]: https://docs.rs/enum_macro_gen This crate provides the `EnumMacroGen` derive proc macro for Rust, which simplifies handling variants of an enum. It generates declartive macros according to a given template. ## Example ```rust use enum_macro_gen::EnumMacroGen; #[derive(EnumMacroGen)] #[enum_macro[handle_test={match: $self.handle_$variant($fields);}]] enum Test { Foo(Item), Double(Item, Box), Bar, } ``` Instead of writing a `match` statement to handle each variant of `Test`, you can use the `handle_test!` a macro generated by `EnumMacroGen`. ```rust // <**GENERATED**> macro_rules! handle_test { ($self:ident, $test:ident) => { match $test { Test::Foo(a_0) => { $self.handle_foo(a_0); } Test::Double(a_0, a_1) => { $self.handle_double(a_0, a_1); } Test::Bar => { $self.handle_bar(); } } }; } // ``` ## Deriving EnumMacroGen To use `EnumMacroGen`, simply add `#[derive(EnumMacroGen)]` above your enum declaration. You can also specify the format of the generated macro with the `enum_macro` attribute. The attribute value should be a token list containing `$variant` and `$fields`, which will be replaced with the variant name and fields, respectively. ## License `enum_macro_gen` is licensed under either of * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. ### Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in `enum_macro_gen` by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.