baseplug icon indicating copy to clipboard operation
baseplug copied to clipboard

Donwfall85/feature/enum impl

Open downfall85 opened this issue 4 years ago • 2 comments

Here is my attempt to implement enums in baseplug model. We can now write models like that:

baseplug::model! {
    #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
    enum MyEnum {
        A,
        B,
    }

    #[derive(Debug, Serialize, Deserialize)]
    struct MyModel {

        #[parameter(name = "param")]
        param: MyEnum
    }
}

Nearly all the job is done in the proc macro.

There is one thing that I would like to improve but I haven't any solution right now. We now need to import baseplug::Translatable and baseplug::Param in the plugin since the proc macro generates the implementation of the Translatable trait for the enum.

Let me know what do you think and how I can improve it.

downfall85 avatar Sep 04 '21 19:09 downfall85

I improved the implementation. It's cleaner and we don't need the imports anymore.

The Copy trait is now needed on the enum.

baseplug::model! {
    #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Copy)]
    enum MyEnum {
        A,
        B,
    }

    #[derive(Debug, Serialize, Deserialize)]
    struct MyModel {

        #[parameter(name = "param")]
        param: MyEnum
    }
}

downfall85 avatar Sep 08 '21 15:09 downfall85

With the work I did on the PR https://github.com/wrl/baseplug/pull/28, I wonder if it wouldn't be wiser to avoid the blanket implementation

impl <P: Plugin, Model, T> Translatable <T, P, Model> for T
     where T: Copy + From <f32> + EnumModel, f32: From <T>

and instead make an implementation for each enum in the macro.. This would also get rid of the EnumModel trait.

downfall85 avatar Sep 30 '21 21:09 downfall85