macro_rules! impl_field_display {
($enum_name:ident, $( $variant:ident => $name:literal ),* ) => { ... };
}
Expand description
Implements std::fmt::Display
for a field enum with complex (nested) variants.
This macro handles field enums that contain both simple variants (e.g., Title
)
and complex variants that hold a Vec
of sub-fields (e.g., User(Vec<UserField>)
).
- For simple variants, it relies on
strum
’sAsRefStr
trait to produce thecamelCase
string. - For complex variants, it formats them as
fieldName(subField1,subField2,...)
.
§Arguments
$enum_name:ident
- The name of the enum to implementDisplay
for.$( $variant:ident => $name:literal ),*
- A comma-separated list of the complex variants. Each entry specifies theVariantName
and the exact string literal to use for its name.
§Example
// An enum with simple and complex variants
use wp_mini::impl_field_display;
#[derive(strum_macros::AsRefStr)]
#[strum(serialize_all = "camelCase")]
enum StoryField {
Title,
VoteCount,
// A complex variant with sub-fields
User(Vec<UserField>),
}
#[derive(strum_macros::AsRefStr)]
#[strum(serialize_all = "camelCase")]
enum UserField {
Username,
Avatar,
}
// Generate the Display implementation using the macro
impl_field_display!(StoryField, User => "user");
// --- Verification ---
// Simple field
let simple_field = StoryField::VoteCount;
assert_eq!(simple_field.to_string(), "voteCount");
// Complex field
let complex_field = StoryField::User(vec![UserField::Username, UserField::Avatar]);
assert_eq!(complex_field.to_string(), "user(username,avatar)");