#enums #attributes #macro #value #annotating #handybars

macro handybars_macros

Attribute macro for annotating structs and enums as Handybars values

1 unstable release

0.2.0 Jan 4, 2025

#363 in #attributes

Download history 106/week @ 2024-12-31 13/week @ 2025-01-07

119 downloads per month
Used in handybars

MIT license

6KB
81 lines

Overview

This is an attribute macro that implements the Into<Value> trait for annotated structs and enums to be used with Handybars. Please refer to the main Handybars crate for information on how to use!

Implementation Notes

Annotating an enum or a struct with #[handybars_value] generates Into<Value> implementations for the item. For example, the #[handybars_value] attribute on the enum:

#[handybars_value]
enum SimpleEnumProp {
    A,
    B,
}

... will result in the following code being generated for the SimpleEnumProp:

impl<'v> Into<handybars::Value<'v>> for SimpleEnumProp {
    fn into(self) -> handybars::Value<'v> {
        match self {
            SimpleEnumProp::A => handybars::Value::String(std::borrow::Cow::from("A")),
            SimpleEnumProp::B => handybars::Value::String(std::borrow::Cow::from("B")),
        }
    }
}

Why use an attribute and not a derive process macro?

Derive Macros do not support implementing traits with generic arguments. In this case we need to implement Into<Value> for the annotated enum or struct. If Value had been a trait and not an enum, a derive macro would have been appropriate.

Dependencies

~210–650KB
~15K SLoC