Thanks, everyone. At the end, I went to sleep and came back to this problem today, and solved with the strum
and strum_macros
crates. I marked my enum with #[derive(EnumDiscriminants, EnumString, AsRefStr)]
and #[strum_discriminants(derive(EnumString, AsRefStr))]
, which gives me a new enum called ErrorDiscriminant
that contains all the same variants but without extra data, as well as a method ErrorDiscriminant::from(&Error)
that I can use to map each Error
variant to an ErrorDiscriminant
variant. From there, I can just do <error-discriminant> as i32
and the problem is solved.
As I'm new to Rust and this is the solution I just found, I cannot guarantee it is correct, but it's working for me so far and it makes sense.