rust-derivative
rust-derivative copied to clipboard
Enumeration derives
On enumerations with only simple variants, I've often wanted to have some functions to:
- [ ] get all variants (make
constarray? some kind of iterator?) - [ ] given a variant, get the next variant (should this return an
Optionfor end-cases or go round?) - [ ] get a human readable name for a variant
- [ ] by default should just be based on the case of the variant, eg.
PermissionDeniedwould give “permission denied” - [ ] have a
human="some value"attribute on variants to change that
- [ ] by default should just be based on the case of the variant, eg.
- [ ]
{enum}↔{integer}conversion
It would also be nice to be able to derive Display (ToString?) and FromStr implementation on C-style enumerations for "quick & easy" (de)serialisation. Any chance of getting this included?
I'm not sure about FromStr, I feel like this can only be abused.
As for Display, I though displaying stuff like “permission denied”, not “PermissionDenied”. Not very useful for serialization then (you should use serde anyway :smile:).
But this could be configured through some attributes. We also are not limited to just implement existing traits, nothing prevents us to add inherent methods (eg. like we already do with new + Default) (for now, Macro 1.1 can't export new traits though).
In fact, I jus found out tha the enum_derive crate already does both these things, albeit using traditional macros.
I already use Serde, and indeed I plan to use this with serde, since Serde doesn't support my particular use case -- but fair point in general.
I don't know what you mean by new + Default... looking forward to Macros 2.0 in any case!
Traditional macros can't make PermissionDenied into “permission denied”, so that's one more reason to go that way.
I don't know what you mean by
new+Default... looking forward to Macros 2.0 in any case!
Contrary to the built-in derive, derivative is not limited to trait implementations. We can already derive a new inherent method. So some of those functions could be provided by such methods instead of trait implementations. Display is really not meant for serialization.
Serde doesn't support my particular use case
I'm curious, what is that? serde supports #[derive(Serialize, Deserialize)] on enum, with all sorts of attributes.
Ah, that's intriguing.
Anyway, my use case is (de)serialising commas-eparated lists of enum values, with serde_urlencoded.