Skip to content
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

Question: supporting enum parsing without requiring #[peek(...)] for the last variant. #4

Open
s-arash opened this issue Sep 25, 2021 · 2 comments

Comments

@s-arash
Copy link

s-arash commented Sep 25, 2021

Does this make sense? I tried implementing it, I'm not sure what to do with name though. Would letting the variant name fill in for name be acceptable?

@sharnoff
Copy link
Owner

sharnoff commented Sep 27, 2021

Hey, I'm curious to hear more about what you mean here.

Are you referring to something along the lines of "the last variant is parsed if all else fails, therefore no peeking is required"? Or something more like "the last variant is empty, so it cannot fail"

In the first case, if parsing fails we still need a user-supplied value of name to construct an error message (admittedly, the documentation around the actual error messages produced is a bit lacking). I'm not sure what the best solution for the second case might be -- there's a handful of mediocre ones. My current working solution to the second case tends to look like this:

#[derive(Parse)]
enum Foo {
    /* Other variants... */

    #[peek_with(|_| true, name = "<this message should never appear>")]
    NoneOfTheAbove,
}

@s-arash
Copy link
Author

s-arash commented Sep 28, 2021

My use case is the first case (parse the last variant if all the #[peek(..)]s fail). I realized that I could use #[peek_with(|_| true, ...)] to achieve the effect I was looking for for that case too.

If you still think my suggestion is a good idea, I can open a PR.

Otherwise, feel free to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants