Crates.io | enum_to_enum_derive |
lib.rs | enum_to_enum_derive |
version | 0.1.0 |
source | src |
created_at | 2021-09-06 15:37:46.248105 |
updated_at | 2021-09-06 15:37:46.248105 |
description | Derive macro implementation for enum_to_enum. enum_to_enum derives possibly effectful conversions between enums. |
homepage | |
repository | https://github.com/ratchetdesigns/enum_to_enum |
max_upload_size | |
id | 447583 |
size | 49,261 |
enum_to_enum exposes a derive macro to easily generate possibly effectful enum-to-enum conversions: #[derive(FromEnum)]
.
Many transformation pipelines are readily expressed as conversions from one enum to another. However, these transformations can be tedious to write, especially if they generate some additional effects in addition to data mapping. enum_to_enum makes it easy to generate these conversions.
[dependencies]
enum_to_enum = "0.1.0"
use enum_to_enum::FromEnum;
#[derive(Debug)]
enum Src {
Case1(),
Case2(SrcStrField),
Case3 { a: SrcStrField, b: u8 },
}
#[derive(FromEnum, Debug, PartialEq, Eq)]
#[from_enum(Src)]
enum Dest {
Case1(),
#[from_case(Case2)]
DestCase2(DestStrField),
#[from_case(Src = Case3)]
DestCase3 { a: DestStrField, b: u8 },
}
#[derive(Debug, PartialEq, Eq)]
struct SrcStrField(String);
#[derive(Debug, PartialEq, Eq)]
struct DestStrField(String);
impl From<SrcStrField> for DestStrField {
fn from(src: SrcStrField) -> DestStrField {
DestStrField(src.0 + " world")
}
}
assert_eq!(
Dest::from(Src::Case1()),
Dest::Case1(),
);
assert_eq!(
Dest::from(Src::Case2(SrcStrField(String::from("hello")))),
Dest::DestCase2(DestStrField(String::from("hello world"))),
);
assert_eq!(
Dest::from(Src::Case3 {
a: SrcStrField(String::from("hello")),
b: 100u8,
}),
Dest::DestCase3 {
a: DestStrField(String::from("hello world")),
b: 100u8,
},
);