#enums #macro-derive #conversion #generate #effect #possibly #enum-to-enum

macro enum_to_enum_derive

Derive macro implementation for enum_to_enum. enum_to_enum derives possibly effectful conversions between enums.

1 unstable release

0.1.0 Sep 6, 2021

#7 in #possibly

Download history 2053/week @ 2024-05-27 2069/week @ 2024-06-03 2048/week @ 2024-06-10 1956/week @ 2024-06-17 2744/week @ 2024-06-24 2542/week @ 2024-07-01 2882/week @ 2024-07-08 2056/week @ 2024-07-15 2448/week @ 2024-07-22 2882/week @ 2024-07-29 2436/week @ 2024-08-05 2057/week @ 2024-08-12 1753/week @ 2024-08-19 2719/week @ 2024-08-26 4611/week @ 2024-09-02 3089/week @ 2024-09-09

12,174 downloads per month
Used in 3 crates (via enum_to_enum)

MIT/Apache

38KB
723 lines

enum_to_enum   CI

enum_to_enum exposes a derive macro to easily generate possibly effectful enum-to-enum conversions: #[derive(FromEnum)].


When should you use enum_to_enum?

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.

enum_to_enum in action

Show cargo.toml
[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,
    },
);

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in enum_to_enum by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.5MB
~35K SLoC