Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting struct with enum members #193

Open
janosimas opened this issue Jul 19, 2021 · 1 comment
Open

Converting struct with enum members #193

janosimas opened this issue Jul 19, 2021 · 1 comment

Comments

@janosimas
Copy link

Hi, I just started using this crate and looks quite promising!

I want to check if there is some limitation with the crate or maybe I'm doing something wrong.
Here is what I found in my tests:

  • Enum to enum, works.
  • Struct without enum, works.
  • Struct with enum, do not work.

Is the correct? Is it possible to convert a complex struct with enum fields?

Here is a minimal example.
I have a BaseStruct with an BaseEnum field, all of them derive from LabelledGeneric, and I want to convert to a TargetStruct with a TargetEnum field. All of the them have the same names and member order.

extern crate frunk;
extern crate frunk_core;

use frunk::{labelled::Transmogrifier, LabelledGeneric};

fn main() {
    // works
    let target: TargetEnum = frunk::labelled_convert_from(BaseEnum::default());

    // works
    let target: TargetStruct = frunk::labelled_convert_from(BaseStruct::default());
    let target: TargetStruct = frunk::transform_from(BaseStruct::default());
    let target: TargetStruct = BaseStruct::default().transmogrify();
    // Doesn't work
    let target: TargetStruct = frunk::from_labelled_generic(BaseStruct::default());

    // No conversion works for the "complex" struct
    let target: TargetComplexStruct = frunk::labelled_convert_from(BaseComplexStruct::default());
    let target: TargetComplexStruct = frunk::from_labelled_generic(BaseComplexStruct::default());
    let target: TargetComplexStruct = frunk::transform_from(BaseComplexStruct::default());
    let target: TargetComplexStruct = BaseComplexStruct::default().transmogrify();
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
enum BaseEnum {
    SomeValue,
}

impl Default for BaseEnum {
    fn default() -> Self {
        BaseEnum::SomeValue
    }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
enum TargetEnum {
    SomeValue,
}

#[derive(Default, Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct BaseStruct {
    test: String,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct TargetStruct {
    test: String,
}

#[derive(Default, Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct BaseComplexStruct {
    test: String,
    other: BaseEnum,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, LabelledGeneric)]
struct TargetComplexStruct {
    test: String,
    other: TargetEnum,
}
@azzamsa
Copy link

azzamsa commented Jul 28, 2023

Yes. Looking for the same thing. I tried many approaches but had no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants