A derive macro for converting data types to enum variants

Hi there!

I've created a macro that allows me to track updates to a struct for auditing purposes. That's working well enough, but to allow storing the various data types I have an enum of every sort of data that could be held by any struct. The issue is that I have a lot of repetitive code for From<CustomStruct> for MyEnum and was wondering if there's a common off-the-shelf crate before I go writing my own thing

Here's an example. As a summary here:

enum BaseTypes {
    String(String),
    I32(i32),
    TypeA(MyTypeA),
    TypeB(MyTypeB),
}

impl From<String> for BaseTypes {
    fn from(value: String) -> Self {
        BaseTypes::String(value)
    }
}

// ... The impl above is duplicated for about ten data types so far and growing

I think what I'm looking for is a macro that will create a From<> impl for each member of the enum that can store a value. Any good crates before I go writing my own?

Try the derive_more crate.

2 Likes

That's exactly what I'm looking for! Thanks @quinedot!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.