Expand description
§enum-group-macros
Define grouped enums with ergonomic pattern matching.
This crate allows you to define a “wire” enum with variants organized into logical groups, and provides macros for matching on those groups without boilerplate.
§Example
ⓘ
use enum_group_macros::{define_enum_group, match_enum_group, EnumGroup};
use serde::{Deserialize, Serialize};
// Define message types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MsgA { pub value: i32 }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MsgB { pub text: String }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MsgC { pub flag: bool }
// Define grouped enum
define_enum_group! {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "payload")]
pub enum WireMsg {
Protocol {
A(MsgA),
B(MsgB),
},
Business {
C(MsgC),
}
}
}
// This generates:
// - enum Protocol { A(MsgA), B(MsgB) }
// - enum Business { C(MsgC) }
// - enum WireMsg { A(MsgA), B(MsgB), C(MsgC) }
// - enum WireMsgGroup { Protocol(Protocol), Business(Business) }
// - impl EnumGroup for WireMsg
fn handle_message(msg: WireMsg) {
match_enum_group!(msg, WireMsg, {
Protocol(p) => {
println!("Protocol message: {:?}", p);
},
Business(b) => {
println!("Business message: {:?}", b);
},
})
}§Features
- Zero runtime overhead: All grouping is compile-time
- Async-friendly: Works seamlessly with
async/await - Serde compatible: Attributes like
#[serde(...)]are propagated - IDE support: Full autocomplete and type checking
§How It Works
The define_enum_group! macro generates:
- Group enums: One enum per group (e.g.,
Protocol,Business) - Wire enum: A flat enum with all variants for serialization
- Group dispatch enum: An enum wrapping group enums (e.g.,
WireMsgGroup) - EnumGroup impl: Conversion from wire enum to grouped representation
The match_enum_group! macro expands to a match on the grouped representation,
using the EnumGroup trait to access the Group type without explicit imports.
Macros§
- define_
enum_ group - Defines a flat wire enum and multiple specialized categorical enums.
- match_
enum_ group - Matches on a grouped enum using ergonomic syntax.
Traits§
- Enum
Group - Trait for enums with grouped variants.