define_property

Macro define_property 

Source
macro_rules! define_property {
    (
        struct $name:ident ( $visibility:vis Option<$inner_ty:ty> ),
        $entity:ident
        $(, $($extra:tt)+),*
    ) => { ... };
    (
        struct $name:ident ( $($visibility:vis $field_ty:ty),* $(,)? ),
        $entity:ident
        $(, $($extra:tt)+),*
    ) => { ... };
    (
        struct $name:ident { $($visibility:vis $field_name:ident : $field_ty:ty),* $(,)? },
        $entity:ident
        $(, $($extra:tt)+),*
    ) => { ... };
    (
        enum $name:ident {
            $($variant:ident),* $(,)?
        },
        $entity:ident
        $(, $($extra:tt)+),*
    ) => { ... };
}
Expand description

Defines a struct or enum with a standard set of derives and automatically invokes impl_property! for it. This macro provides a concise shorthand for defining simple property types that follow the same derive and implementation pattern.

The macro supports the following forms:

§1. Tuple Structs

define_property!(struct Age(u8), Person);

Expands to:

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize)]
pub struct Age(u8);
impl_property!(Age, Person);

You can define multiple tuple fields:

define_property!(struct Location(City, State), Person);

§2. Named-field Structs

define_property!(struct Coordinates { x: i32, y: i32 }, Person);

Expands to:

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize)]
pub struct Coordinates { x: i32, y: i32 }
impl_property!(Coordinates, Person);

§3. Enums

define_property!(
    enum InfectionStatus {
        Susceptible,
        Infectious,
        Recovered,
    },
    Person
);

Expands to:

#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize)]
pub enum InfectionStatus {
    Susceptible,
    Infectious,
    Recovered,
}
impl_property!(InfectionStatus, Person);

§Notes

  • The generated type always derives the following traits: Debug, PartialEq, Eq, Clone, Copy, and Serialize.
  • Use the optional default_const = <default_value> argument to define a compile-time constant default for the property.
  • If you need a more complex type definition (e.g., generics, attributes, or non-Copy fields), define the type manually and then call impl_property! directly.