Closed qwandor closed 1 year ago
I really like this idea, and in particular I think it will also help solve https://github.com/illicitonion/num_enum/issues/82 (which ideally would be solved by use of something like https://crates.io/crates/transitive rather than with num_enum-specific support)...
I'll have a play with what the API could look like for this - the place I'm not 100% sure of is on how to take the error_constructor
, but in general I like the proposed API :)
I put together a draft in https://github.com/illicitonion/num_enum/pull/123 - can you give it a go and see how it works for you?
You can see an example of the syntax in the newly added tests in num_enum/tests/try_from_primitive.rs:
#[derive(TryFromPrimitive)]
#[num_enum(error_type(name = CustomError, constructor = CustomError::new))]
#[repr(u8)]
enum FirstNumber {
Zero,
One,
Two,
}
I just had a go with this in my code and it works well, thanks! Looking forward to seeing a release with this in it.
Fabulous! I've just published 0.7.0 with this feature :) Thanks again for the feature request, and testing it out!
It would be useful for the
TryFromPrimitive
derive macro to have an option to specify a custom error type to use when the conversion fails, rather than the suppliedTryFromPrimitiveError
. I'd like to useTryFromPrimitive
on some types which are part of my library's public API, and exposingTryFromPrimitiveError
as part of this isn't great. It could look something like:Where
MyError
is the error type to use for theTryFrom
implementation, anderror_constructor
is a function of type(u8) -> MyError
which the generatedTryFrom
implementation would call with the invalid input value to make an instance of the error type.