2个版本
0.1.5 | 2024年4月20日 |
---|---|
0.1.4 | 2024年3月30日 |
0.1.3 |
|
0.1.1 |
|
0.1.0 |
|
#262 in 过程宏
每月43次下载
24KB
449 行
ENUM_DESC & ENUM_TRS
提供一系列派生宏,用于向枚举添加描述性信息并简化枚举代码的翻译。
具体来说,以下宏用于增强枚举的额外功能:"EnumDesc", "EnumDescI8", "EnumDescU8", "EnumDescI16", "EnumDescU16", "EnumDescI32", "EnumDescU32", "EnumDescI64", "EnumDescU64", "EnumDescISize", "EnumDescUSize"
另一方面,enum_trs属性宏使值的翻译成为可能,支持“等效翻译”和“标志翻译”。
示例 - EnumDesc
向枚举添加描述性信息
#[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));
}
运行上述代码,您将看到以下信息
枚举:Some(PHONE)
代码:2
描述:主机计算机
描述:平板电脑
示例 - enum_trs
翻译枚举代码
/// 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);
}
运行上述代码,您将看到以下信息
UserDto {
name: "hui",
gender: 1,
allow_device_type: Some(
7,
),
gender_desc: "female",
allow_device_type_desc: "移动电话 | 主机计算机 | 平板电脑",
}
依赖
~265–710KB
~17K SLoC