rustler icon indicating copy to clipboard operation
rustler copied to clipboard

Add attribute keep_case for NifUnitEnum

Open evnu opened this issue 5 years ago • 7 comments

NifUnitEnum currently expects atoms corresponding to struct fields to be in snake case. This works fine when the user can define the atoms to be in snake case. If that is not possible, e.g. because they are defined by a third party, then decoding them with NifUnitEnum is not possible.

I suggest we add an attribute keep_case which defines that the fields are to be used exactly as-is as atoms.

#[derive(NifUnitEnum)]
#[rustler(keep_case)]
pub struct KeepCase {
  UPPERCASE,
  lowercase
}

evnu avatar Oct 23 '19 10:10 evnu

What about something more similar to serde's rename?

#[derive(NifUnitEnum)]
pub struct KeepCase {
    #[rustler(rename = "UPPERCASE")]
    uppercase,
    lowercase
}

I imagine that if we end up adding serde support to rustler, we will end up just using #[serde(rename = "UPPERCASE")] ?

scrogson avatar Oct 24 '19 03:10 scrogson

What about something more similar to serde's rename?

Yes, that would work. Does serde by default rename variants at all, or does it keep them as-is except when rename is specified?

evnu avatar Oct 24 '19 07:10 evnu

Wouldn't it make sense to instead work on getting serde_rustler in? I don't see the advantage of a custom codegen right now.

filmor avatar Oct 25 '19 15:10 filmor

As this is a small change, I would do this. I am unsure how much work serde_rustler will be. Of course, rustler_serde would be the preferred way.

evnu avatar Oct 25 '19 16:10 evnu

Of course, rustler_serde would be the preferred way.

Agreed in the context of this crate. I would assume that it wouldn't really be useful outside of rustler anyways.

scrogson avatar Oct 25 '19 19:10 scrogson

Mentioned here:

Depending on how willing y'all are to change up rustler and depend directly on serde, a lot of ser/de + encoder/decoder + macro logic could be simplified and/or gotten rid of. The simplest non-breaking change however would be to just copy my Serializer and Deserializer logic and implement them directly on Env and Term respectively behind a feature flag, no real need for a separate crate.

sunny-g avatar Mar 05 '20 17:03 sunny-g

I started working on integrating this into rustler over the weekend 👍

scrogson avatar Mar 09 '20 12:03 scrogson