rustler
rustler copied to clipboard
Add attribute keep_case for NifUnitEnum
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
}
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")]
?
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?
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.
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.
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.
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.
I started working on integrating this into rustler over the weekend 👍