enum_macro_gen

Crates.ioenum_macro_gen
lib.rsenum_macro_gen
version0.2.0
sourcesrc
created_at2023-01-03 21:11:02.531183
updated_at2024-09-20 12:05:46.791167
descriptionMacro generator for handling enums
homepagehttps://github.com/da-x/enum_macro_gen
repositoryhttps://github.com/da-x/enum_macro_gen
max_upload_size
id750412
size16,667
Dan Aloni (da-x)

documentation

README

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

use enum_macro_gen::EnumMacroGen;

#[derive(EnumMacroGen)]
#[enum_macro[handle_test={match: $self.handle_$variant($fields);}]]
enum Test {
    Foo(Item),
    Double(Item, Box<Test>),
    Bar,
}

Instead of writing a match statement to handle each variant of Test, you can use the handle_test! a macro generated by EnumMacroGen.

// <**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();
            }
        }
    };
}
// </**GENERATED**>

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.

Commit count: 0

cargo fmt