strum icon indicating copy to clipboard operation
strum copied to clipboard

I way to provide my own implementation.

Open andoriyu opened this issue 6 years ago • 2 comments

Maybe I'm just dumb, but is there a way to do something like:

enum Compression {
Lz4,
Gzip(u8),
}

Compression::Gzip(9).to_string() # returns gzip-9
Compression::from_str("gzip-2") # returns Compression::Gzip(2)

andoriyu avatar Aug 25 '19 22:08 andoriyu

In this particular case, you could do something like this SO Answer. This doesn't work if there's more than one variant with additional data though. You need to manually implement Display and From<&str>.

In general, this is probably outside the scope of what strum is for. I'm not sure what the obvious, generalizable way of serializing enums with additional data on the variants that preserves the data, and that makes me think it's not a great candidate for this plugin.

Peternator7 avatar Aug 26 '19 12:08 Peternator7

The failure crate as a way to solve this somewhat nicely

#[derive(failure)]
pub enum MyError {
  #[fail(display = "This is an error with props: {}, {}", _0, _1)]
  Error(String, usize)
}

See https://boats.gitlab.io/failure/custom-fail.html for the docs on this, so it would be great if strum could implement the following

pub enum Compression {
  #[strum(serialize = "gzip-{}", _0)]
  Gzip(u8),
  Lz4,
}

dignifiedquire avatar Sep 13 '19 09:09 dignifiedquire

Closing old issues

Peternator7 avatar Oct 02 '22 19:10 Peternator7