derive-syn-parse icon indicating copy to clipboard operation
derive-syn-parse copied to clipboard

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

Open s-arash opened this issue 4 years ago • 2 comments

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?

s-arash avatar Sep 25 '21 19:09 s-arash

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,
}

sharnoff avatar Sep 27 '21 06:09 sharnoff

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.

s-arash avatar Sep 28 '21 19:09 s-arash