-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide means to specify error string when matching variant is not found #13
Comments
I appreciate the feedback, I'm hesitant to make this change. At the moment the error message comes from the impl of You can still change the error message by matching on the result and printing out a custom message.
|
What about allowing different custom error types to be used, rather than fiddling with strum::ParseError? For instance, I'm using failure for a project and wanted to parse to a I can, as you point out, convert the |
That's potentially doable. Right now there aren't any attributes on the enum itself, only variants, but that could change. :)
Let me know if you have any thoughts or other implementation ideas. Otherwise I'll probably see how well idea 1 works! |
I had also imagined it like 1, as that keeps the code fairly clean. Using |
For me, something like would work: #[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum ParseError {
VariantNotFound(String), // variant name
} another related issue #91 will donate 3 usd for fix. |
@Peternator7 I'm the maintainer of
We then allow the user to drop in any custom error type that pub struct VariantNotFoundError<'a>(&'a str);
impl<'a> From<VariantNotFoundError<'a>> for ParseError {
fn from(_: VariantNotFoundError<'a>) -> Self {
ParseError::VariantNotFound
}
} Then the enum-level attribute has a default value of |
How goes this? I'd love something along the lines of: #[strum(error= CustomError(String))]
pub enum Foo {} Where String gets replaced with the invalid variant. |
Right now we implement TryFrom ourselves, which can be annoying when it comes to updating enum fields. Strum handles this for us automatically, lessening the room for human error. We can't use it right now if I understand this issue correctly, since we can't return a custom error type: Peternator7/strum#13 Signed-off-by: Dallas Strouse <dastrouses@gmail.com>
@Peternator7 any updates on this one? |
@Peternator7 would you mind commenting the current state of the issue? Was it closed as WONTFIX as I couldn't find any related updates in the changelog. |
Closed this one accidentally sorry while cleaning up some older issues. Re-opened, but tbh, I don't have any plans around this particular issue. |
Right now --when no matching variant is found-- (parsing a string into an enum using the derived
from_str
) a constant string error is returned saying"Matching variant not found"
. There are two improvements that can be done on this:Better default: A better default would probably be something like
"expected 'string1', 'string2', 'string3' or 'string4'"
. Even though it costs to iterate over the options, it doesn't matter, because it would only happen when no valid string is found.Custom error: The user should be able to specify a custom error message, something like:
** special token: You could even provide a special token for the
onerror
keyword so that the user doesn't have to repeat the options all the time. Something likeonerror="We like you, but can't understand what you wrote, please use {opts}"
.The text was updated successfully, but these errors were encountered: