chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

feat: impl From<ExpectedNumber> for RichPattern

Open seftontycho opened this issue 5 months ago • 4 comments

Currently it is not possible to use the number::Number parsers with the Rich error type as RichPattern does not implement From<ExpectedNumber>.

This PR adds the missing impl in the number module so it is appropriatley feature gated.

Not previously possible:

fn parse_u8<'a>() -> impl Parser<'a, &'a str, u8, extra::Err<Rich<'a, char>>> {
    let u8_num: Number<STANDARD, &'a str, u8, ErrRich<'a>> = number();
    u8_num
}

seftontycho avatar Aug 15 '25 22:08 seftontycho

I'm not exactly sure what variant of RichPattern to use here, Label seemed the most fitting but then I'm not sure on the label name.

Open to suggestions better than "number".

seftontycho avatar Aug 15 '25 22:08 seftontycho

Thanks! I think adding a new variant to RichPattern is fine also, and might be worth it for the ability to disambiguate at a type level (we don't want end users pattern-matching on strings, if we can help it). If you could, also added #[non_exhaustive] to the enum would also be great. It's a breaking change, but there are already several of those lined up anyway!

zesterer avatar Aug 18 '25 17:08 zesterer

Thanks! I think adding a new variant to RichPattern is fine also...

How would you like that to work feature wise? If we added RichPattern::Number it wouldn't really make sense if a user wasn't using the lexical-numbers feature.

seftontycho avatar Aug 18 '25 17:08 seftontycho

Apologies, GitHub swallowed the notification for this.

I think the only change needed is to add #[non_exhaustive] to the RichPattern enum.

zesterer avatar Sep 20 '25 10:09 zesterer