Skip to content

Commit

Permalink
docs(tut): Show more of what error context can do
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 6, 2024
1 parent f8dc918 commit 8aeba70
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions src/_tutorial/chapter_7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
//! # use winnow::combinator::fail;
//! # use winnow::Parser;
//! use winnow::error::StrContext;
//! use winnow::error::StrContextValue;
//!
//! #
//! # #[derive(Debug, PartialEq, Eq)]
Expand All @@ -118,10 +119,18 @@
//! #
//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> {
//! alt((
//! ("0b", parse_bin_digits).context(StrContext::Label("binary")),
//! ("0o", parse_oct_digits).context(StrContext::Label("octal")),
//! ("0d", parse_dec_digits).context(StrContext::Label("decimal")),
//! ("0x", parse_hex_digits).context(StrContext::Label("hexadecimal")),
//! ("0b", parse_bin_digits)
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("binary"))),
//! ("0o", parse_oct_digits)
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("octal"))),
//! ("0d", parse_dec_digits)
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("decimal"))),
//! ("0x", parse_hex_digits)
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("hexadecimal"))),
//! )).parse_next(input)
//! }
//!
Expand Down Expand Up @@ -158,7 +167,8 @@
//! let error = "\
//! 0xZZ
//! ^
//! invalid hexadecimal";
//! invalid digit
//! expected hexadecimal";
//! assert_eq!(input.parse::<Hex>().unwrap_err(), error);
//! }
//! ```
Expand All @@ -172,6 +182,7 @@
//! # use winnow::combinator::fail;
//! # use winnow::Parser;
//! # use winnow::error::StrContext;
//! # use winnow::error::StrContextValue;
//! #
//! #
//! # #[derive(Debug, PartialEq, Eq)]
Expand All @@ -197,10 +208,18 @@
//! #
//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> {
//! # alt((
//! # ("0b", parse_bin_digits).context(StrContext::Label("binary")),
//! # ("0o", parse_oct_digits).context(StrContext::Label("octal")),
//! # ("0d", parse_dec_digits).context(StrContext::Label("decimal")),
//! # ("0x", parse_hex_digits).context(StrContext::Label("hexadecimal")),
//! # ("0b", parse_bin_digits)
//! # .context(StrContext::Label("digit"))
//! # .context(StrContext::Expected(StrContextValue::Description("binary"))),
//! # ("0o", parse_oct_digits)
//! # .context(StrContext::Label("digit"))
//! # .context(StrContext::Expected(StrContextValue::Description("octal"))),
//! # ("0d", parse_dec_digits)
//! # .context(StrContext::Label("digit"))
//! # .context(StrContext::Expected(StrContextValue::Description("decimal"))),
//! # ("0x", parse_hex_digits)
//! # .context(StrContext::Label("digit"))
//! # .context(StrContext::Expected(StrContextValue::Description("hexadecimal"))),
//! # )).parse_next(input)
//! # }
//! #
Expand Down Expand Up @@ -234,7 +253,8 @@
//! let error = "\
//! 0b5
//! ^
//! invalid hexadecimal";
//! invalid digit
//! expected hexadecimal";
//! assert_eq!(input.parse::<Hex>().unwrap_err(), error);
//! }
//! ```
Expand Down Expand Up @@ -262,11 +282,12 @@
//! # use winnow::prelude::*;
//! # use winnow::token::take_while;
//! # use winnow::combinator::alt;
//! # use winnow::combinator::cut_err;
//! # use winnow::token::take;
//! # use winnow::combinator::fail;
//! # use winnow::Parser;
//! use winnow::error::StrContext;
//! # use winnow::error::StrContext;
//! # use winnow::error::StrContextValue;
//! use winnow::combinator::cut_err;
//!
//! #
//! # #[derive(Debug, PartialEq, Eq)]
Expand All @@ -292,10 +313,18 @@
//! #
//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> {
//! alt((
//! ("0b", cut_err(parse_bin_digits)).context(StrContext::Label("binary")),
//! ("0o", cut_err(parse_oct_digits)).context(StrContext::Label("octal")),
//! ("0d", cut_err(parse_dec_digits)).context(StrContext::Label("decimal")),
//! ("0x", cut_err(parse_hex_digits)).context(StrContext::Label("hexadecimal")),
//! ("0b", cut_err(parse_bin_digits))
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("binary"))),
//! ("0o", cut_err(parse_oct_digits))
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("octal"))),
//! ("0d", cut_err(parse_dec_digits))
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("decimal"))),
//! ("0x", cut_err(parse_hex_digits))
//! .context(StrContext::Label("digit"))
//! .context(StrContext::Expected(StrContextValue::Description("hexadecimal"))),
//! )).parse_next(input)
//! }
//!
Expand Down Expand Up @@ -332,7 +361,8 @@
//! let error = "\
//! 0b5
//! ^
//! invalid binary";
//! invalid digit
//! expected binary";
//! assert_eq!(input.parse::<Hex>().unwrap_err(), error);
//! }
//! ```
Expand Down

0 comments on commit 8aeba70

Please sign in to comment.