#enums #translation #macro #macro-derive #enumeration #information #flags

macro enum_desc

A set of column-derived macros is provided to add descriptive information to enumerations and translate enumeration codes

2 releases

0.1.5 Apr 20, 2024
0.1.4 Mar 30, 2024
0.1.3 Mar 27, 2024
0.1.1 Mar 25, 2024
0.1.0 Mar 25, 2024

#417 in Procedural macros

Download history 43/week @ 2024-07-30 5/week @ 2024-09-24

312 downloads per month

MIT license

24KB
449 lines

ENUM_DESC & ENUM_TRS

A set of column-derived macros is provided to add descriptive information to enumerations and facilitate the translation of enumeration codes.
Specifically, the following macros serve to augment enums with additional functionality: "EnumDesc", "EnumDescI8", "EnumDescU8", "EnumDescI16", "EnumDescU16", "EnumDescI32", "EnumDescU32", "EnumDescI64", "EnumDescU64", "EnumDescISize", "EnumDescUSize"
The enum_trs attribute macro, on the other hand, enables the translation of values, supporting both "equivalent translation" and "flag translation."

EXAMPLE - EnumDesc

Add descriptive information to enumerations

#[derive(EnumDesc, Debug)]
pub enum DeviceTypeEnum {
    #[info(desc = "mobile phone")]
    PHONE = 1,
    #[info(desc = "host computer")]
    PC = 2,
    #[info(desc = "tablet")]
    PAD = 3,
}

fn main() {
    println!("enum: {:#?}", DeviceTypeEnum::from_code(1));
    println!("code: {:#?}", DeviceTypeEnum::PC.to_code());
    println!("desc: {:#?}", DeviceTypeEnum::PC.get_desc());
    println!("desc: {:#?}", DeviceTypeEnum::got_desc(3));
}

Run the code above, and you will see the following information:
     enum: Some(PHONE)
     code: 2
     desc: "host computer"
     desc: "tablet"

EXAMPLE - enum_trs

Translate enumeration codes

/// Ordinary Enumeration
#[derive(Debug, EnumDesc)]
pub enum GenderEnum {
    #[info(desc = "male")]
    MALE = 0,
    #[info(desc = "female")]
    FEMALE = 1,
}

/// Flagged Bit Enumeration, 
#[derive(Debug, EnumDescU8)]
pub enum AllowDeviceTypeEnum {
    #[info(desc = "mobile phone")]
    PHONE = 1,

    #[info(desc = "host computer")]
    PC = 2,

    #[info(desc = "tablet")]
    PAD = 4,
}

/// "=" is used for equivalent translation
/// "&"  is used for flag bit translation.
#[enum_trs(
gender = GenderEnum,
allow_device_type & AllowDeviceTypeEnum,
)]
#[derive(Debug)]
pub struct UserDto {
    pub name: String,
    pub gender: i16,
    pub allow_device_type: Option<u8>,
}

fn main() {
    let mut user_dto = UserDto {
        name: "hui".to_string(),
        gender: 1i16,
        allow_device_type: Some(7u8),
        gender_desc: "".to_string(),
        allow_device_type_desc: "".to_string(),
    };
    user_dto.translate_enums();
    println!("{:#?}", user_dto);
}

Run the code above, and you will see the following information:
     UserDto {
         name: "hui",
         gender: 1,
         allow_device_type: Some(
             7,
         ),
         gender_desc: "female",
         allow_device_type_desc: "mobile phone | host computer | tablet",
     }



Dependencies

~225–670KB
~16K SLoC