winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
525 stars 40 forks source link

Can Context support String type. #496

Closed baoyachi closed 5 months ago

baoyachi commented 6 months ago

Please complete the following tasks

winnow version

0.6.5

Describe your use case

Can context support String type. I want to use format! convert String type. How to add custom content.

Describe the solution you'd like

...
preceded(multispace0, take_while(0.., |c| self.sep_char))
            .context(StrContext::Expected(self.sep_char.into()))
+            .context(StrContext::Label(format!("get filed:{}",self.sep_char)))
            .parse_next(input)...

I want to use format! convert String type. How to add custom content.

Alternatives, if applicable

No response

Additional Context

No response

epage commented 6 months ago

There a reason you can't define your own context type for this?

baoyachi commented 6 months ago

There a reason you can't define your own context type for this?

Yes, because self.sep_char is dynamic, I want to name this field.

epage commented 6 months ago

context: C is generic, allowing you to define your own context type, so you can create one that accepts strings, chars, or any type you want.