Crates.io | enum_desc |
lib.rs | enum_desc |
version | 0.1.5 |
source | src |
created_at | 2024-03-25 12:28:41.50229 |
updated_at | 2024-04-20 02:41:21.893828 |
description | A set of column-derived macros is provided to add descriptive information to enumerations and translate enumeration codes |
homepage | |
repository | https://github.com/hwpok/enum_desc |
max_upload_size | |
id | 1185261 |
size | 27,224 |
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."
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"
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",
}