Macro enum_properties::enum_properties[][src]

macro_rules! enum_properties {
    (
        $(#[$($m:tt)*])*
        $public:vis enum $Enum:ident : $EnumProperties:ident {
            $(
                $variant:ident {
                    $($field:ident : $value:expr),* $(, .. $default:expr)? $(,)?
                }
                $(
                    $(@$is_struct_variant_marker:tt)?
                    {
                        $($struct_variant_content:tt)*
                    }
                )?
                $((
                    $(
                        $(@$tuple_variant_item_marker:tt)?
                        $tuple_variant_item:ty
                    ),* $(,)?
                ))?
                $(= $discriminant:expr)?
            ),* $(,)?
        }
    ) => { ... };
    (
        $(#[$($m:tt)*])*
        $public:vis enum $Enum:ident : $EnumProperties:ident {
            $(
                $variant:ident {
                    $($field:ident : $value:expr),* $(,)?
                }
                $(
                    $(@$is_struct_variant_marker:tt)?
                    {
                        $($struct_variant_content:tt)*
                    }
                )?
                $((
                    $(
                        $(@$tuple_variant_item_marker:tt)?
                        $tuple_variant_item:ty
                    ),* $(,)?
                ))?
                $(= $discriminant:expr)?
            ),* , .. $default:expr $(,)?
        }
    ) => { ... };
}
Expand description

Defines a new enum and implements Deref for it.

The enum will Deref to a variant-specific static item.

To specify default properties, use the following syntax (inspired by functional update syntax):

Examples

use enum_properties::enum_properties;
 
pub struct EnemyProperties {
    pub health:     i32,
    pub is_solid:   bool,
    pub is_flying:  bool,
}
 
const DEFAULT_ENEMY_PROPERTIES: EnemyProperties = EnemyProperties {
    health:     10,
    is_solid:   true,
    is_flying:  false,
};
 
enum_properties! {
    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    pub enum EnemyKind: EnemyProperties {
        Skeleton {
            health: 15,
        },
        Ghost {
            is_solid: false,
            is_flying: true,
        },
        Bat {
            health: 1,
            is_flying: true,
        },
        ..DEFAULT_ENEMY_PROPERTIES
    }
}

Non-unit variants and custom discriminants are supported too, by inserting the static initializer directly after the variant name:

use enum_properties::enum_properties;

pub struct EnemyProperties {
    pub base_health:     i32,
    pub is_solid:   bool,
    pub is_flying:  bool,
}

const DEFAULT_ENEMY_PROPERTIES: EnemyProperties = EnemyProperties {
    base_health:     10,
    is_solid:   true,
    is_flying:  false,
};

enum_properties! {
    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    pub enum EnemyKind: EnemyProperties {
        Skeleton {
            base_health: 15,
        } {
            current_health: i32,
        },
        Ghost {
            is_solid: false,
            is_flying: true,
        } {
            is_spooky: bool,
        },
        Bats {
            base_health: 1,
            is_flying: true,
        } (
            u128, // Bat count (but please name this field in an actual program)
        ),
        ..DEFAULT_ENEMY_PROPERTIES
    }
}