Artem-Romanenia/o2o

Question : How to implement From<tuple variant> on unit enums

Closed this issue · 3 comments

Hi, new question !

I would like to do https://docs.rs/o2o/latest/o2o/#enum-variant-type-hint but in reverse :

#[derive(o2o::o2o)]
#[from_owned(Enum)]
enum EnumDto {
    Var1,
    Var2,
    Var3
}

enum Enum {
    Var1,
    Var2(i32, String),
    Var3 {_field: i32, _str_field: String}
}

The generated code is not too far off :

    impl std::convert::From<Enum> for EnumDto {
        fn from(value: &Authentication) -> EnumDto {
            match value {
                Enum::Var1 => EnumDto::Var1,
                Enum::Var2 => EnumDto::Var2,
                Enum::Var3 => EnumDto::Var3,
            }
        }
    }

It would need to be :

    impl std::convert::From<Enum> for EnumDto {
        fn from(value: &Authentication) -> EnumDto {
            match value {
                Enum::Var1 => EnumDto::Var1,
                Enum::Var2(_) => EnumDto::Var2,
                Enum::Var3 { .. } => EnumDto::Var3,
            }
        }
    }

Thanks, your project really need more stars !

Hi, currently the shortest way to do this is

#[derive(Clone, PartialEq, Eq)]
enum Enum {
    Var1,
    Var2(i32, String),
    Var3 { _field: i32, _str_field: String }
}

#[derive(Clone, PartialEq, Eq, o2o::o2o)]
#[from_owned(Enum)]
enum EnumDto {
    Var1,
    #[type_hint(as ())]
    #[ghosts(0: {}, 1: {})]
    Var2,
    #[type_hint(as {})]
    #[ghosts(_field: {}, _str_field: {})]
    Var3
}

which is still an overkill. I missed this aspect of using {..} or (..) in such cases. So thanks for pointing me in this direction. I've already concocted some code that addresses this, it should become part of the next version, which is due somewhere this week. I'll keep this issue open and post an update when it's done!

Hi, with a version v0.4.8, the above example can be shortened a little bit:

#[derive(Clone, PartialEq, Eq)]
enum Enum {
    Var1,
    Var2(i32, String),
    Var3 { _field: i32, _str_field: String }
}

#[derive(Clone, PartialEq, Eq, o2o::o2o)]
#[from_owned(Enum)]
enum EnumDto {
    Var1,
    #[type_hint(as ())] Var2,
    #[type_hint(as {})] Var3
}

Thanks again for submitting this issue, please let me know if you have any other questions!

Awesome !