From 3ba915fe45e40a7da1fafc143a7852f65c75bb3e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 7 Jan 2025 16:28:16 -0600 Subject: [PATCH 01/17] fix(error): Rename PResult to ModalResult I'm hoping being more explicitly will help people better understand how the role this plays. If we make `ErrMode` optional, this will make it even more important to provide clarity. --- src/error.rs | 8 ++++++-- src/lib.rs | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index 4727c89f..09efb2f9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,7 +30,7 @@ use crate::stream::Stream; #[allow(unused_imports)] // Here for intra-doc links use crate::Parser; -/// For use with [`Parser::parse_next`] +/// [Modal error reporting][ErrMode] for [`Parser::parse_next`] /// /// - `Ok(O)` is the parsed value /// - [`Err(ErrMode)`][ErrMode] is the error along with how to respond to it @@ -40,7 +40,11 @@ use crate::Parser; /// When integrating into the result of the application, see /// - [`Parser::parse`] /// - [`ErrMode::into_inner`] -pub type PResult = Result>; +pub type ModalResult = Result>; + +/// Deprecated, replaced with [`ModalResult`] +#[deprecated(since = "0.6.23", note = "Replaced with ModalResult")] +pub type PResult = ModalResult; /// Deprecated, replaced with [`PResult`] #[deprecated(since = "0.6.25", note = "Replaced with `PResult`")] diff --git a/src/lib.rs b/src/lib.rs index 4268a616..70e098fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,6 +149,7 @@ pub mod prelude { pub use crate::stream::ContainsToken as _; pub use crate::stream::Stream as _; pub use crate::stream::StreamIsPartial as _; + pub use crate::ModalResult; pub use crate::PResult; pub use crate::Parser; #[cfg(feature = "unstable-recover")] @@ -159,6 +160,7 @@ pub mod prelude { pub(crate) use crate::TestResult; } +pub use error::ModalResult; pub use error::PResult; pub use parser::*; pub use stream::BStr; From 4d2e4d07714755c2beeab05bcfb3cccd3e3fe661 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 22 Jan 2025 09:00:34 -0600 Subject: [PATCH 02/17] refactor: Resolve PResult deprecations --- benches/contains_token.rs | 10 +- benches/find_slice.rs | 4 +- benches/iter.rs | 10 +- benches/next_slice.rs | 20 +- benches/number.rs | 4 +- examples/arithmetic/parser.rs | 8 +- examples/arithmetic/parser_ast.rs | 8 +- examples/arithmetic/parser_lexer.rs | 14 +- examples/css/parser.rs | 4 +- examples/custom_error.rs | 2 +- examples/http/parser.rs | 10 +- examples/http/parser_streaming.rs | 10 +- examples/ini/bench.rs | 2 +- examples/ini/parser.rs | 6 +- examples/ini/parser_str.rs | 14 +- examples/iterator.rs | 4 +- examples/json/parser.rs | 28 +-- examples/json/parser_dispatch.rs | 30 +-- examples/json/parser_partial.rs | 30 +-- examples/json_iterator.rs | 16 +- examples/ndjson/parser.rs | 24 +-- examples/s_expression/parser.rs | 22 +- examples/string/parser.rs | 12 +- fuzz/fuzz_targets/fuzz_arithmetic.rs | 10 +- src/_topic/language.rs | 20 +- src/_topic/nom.rs | 4 +- src/_topic/stream.rs | 4 +- src/_topic/why.rs | 2 +- src/_tutorial/chapter_1.rs | 12 +- src/_tutorial/chapter_2.rs | 42 ++-- src/_tutorial/chapter_3.rs | 52 ++--- src/_tutorial/chapter_4.rs | 20 +- src/_tutorial/chapter_5.rs | 50 ++--- src/_tutorial/chapter_6.rs | 20 +- src/_tutorial/chapter_7.rs | 82 ++++---- src/_tutorial/chapter_8.rs | 4 +- src/ascii/mod.rs | 170 ++++++++-------- src/ascii/tests.rs | 2 +- src/binary/bits/mod.rs | 16 +- src/binary/bits/tests.rs | 26 +-- src/binary/mod.rs | 290 +++++++++++++-------------- src/combinator/branch.rs | 20 +- src/combinator/core.rs | 38 ++-- src/combinator/debug/internals.rs | 2 +- src/combinator/debug/mod.rs | 2 +- src/combinator/impls.rs | 46 ++--- src/combinator/multi.rs | 73 +++---- src/combinator/sequence.rs | 8 +- src/combinator/tests.rs | 18 +- src/error.rs | 10 +- src/lib.rs | 4 +- src/macros/dispatch.rs | 4 +- src/macros/mod.rs | 2 +- src/macros/seq.rs | 4 +- src/parser.rs | 98 ++++----- src/stream/mod.rs | 16 +- src/stream/tests.rs | 4 +- src/token/mod.rs | 76 +++---- src/token/tests.rs | 2 +- tests/testsuite/custom_errors.rs | 8 +- tests/testsuite/issues.rs | 4 +- tests/testsuite/main.rs | 4 +- tests/testsuite/multiline.rs | 6 +- 63 files changed, 791 insertions(+), 776 deletions(-) diff --git a/benches/contains_token.rs b/benches/contains_token.rs index 675b08e5..a44c10aa 100644 --- a/benches/contains_token.rs +++ b/benches/contains_token.rs @@ -50,7 +50,7 @@ fn contains_token(c: &mut criterion::Criterion) { group.finish(); } -fn parser_slice(input: &mut &str) -> PResult { +fn parser_slice(input: &mut &str) -> ModalResult { let contains = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..]; repeat( 0.., @@ -59,7 +59,7 @@ fn parser_slice(input: &mut &str) -> PResult { .parse_next(input) } -fn parser_array(input: &mut &str) -> PResult { +fn parser_array(input: &mut &str) -> ModalResult { let contains = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; repeat( 0.., @@ -68,7 +68,7 @@ fn parser_array(input: &mut &str) -> PResult { .parse_next(input) } -fn parser_tuple(input: &mut &str) -> PResult { +fn parser_tuple(input: &mut &str) -> ModalResult { let contains = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); repeat( 0.., @@ -77,7 +77,7 @@ fn parser_tuple(input: &mut &str) -> PResult { .parse_next(input) } -fn parser_closure_or(input: &mut &str) -> PResult { +fn parser_closure_or(input: &mut &str) -> ModalResult { let contains = |c: char| { c == '0' || c == '1' @@ -97,7 +97,7 @@ fn parser_closure_or(input: &mut &str) -> PResult { .parse_next(input) } -fn parser_closure_matches(input: &mut &str) -> PResult { +fn parser_closure_matches(input: &mut &str) -> ModalResult { let contains = |c: char| matches!(c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'); repeat( 0.., diff --git a/benches/find_slice.rs b/benches/find_slice.rs index 656c9899..c5350ef3 100644 --- a/benches/find_slice.rs +++ b/benches/find_slice.rs @@ -38,11 +38,11 @@ fn find_slice(c: &mut criterion::Criterion) { group.finish(); } -fn parser_byte(input: &mut &str) -> PResult { +fn parser_byte(input: &mut &str) -> ModalResult { repeat(0.., (take_until(0.., "\r"), "\r")).parse_next(input) } -fn parser_slice(input: &mut &str) -> PResult { +fn parser_slice(input: &mut &str) -> ModalResult { repeat(0.., (take_until(0.., "\r\n"), "\r\n")).parse_next(input) } diff --git a/benches/iter.rs b/benches/iter.rs index bc75d821..0b5dd01b 100644 --- a/benches/iter.rs +++ b/benches/iter.rs @@ -51,7 +51,7 @@ fn iter(c: &mut criterion::Criterion) { group.finish(); } -fn iterate(input: &mut &[u8]) -> PResult { +fn iterate(input: &mut &[u8]) -> ModalResult { let mut count = 0; for byte in input.iter() { if byte.is_dec_digit() { @@ -62,7 +62,7 @@ fn iterate(input: &mut &[u8]) -> PResult { Ok(count) } -fn next_token(input: &mut &[u8]) -> PResult { +fn next_token(input: &mut &[u8]) -> ModalResult { let mut count = 0; while let Some(byte) = input.next_token() { if byte.is_dec_digit() { @@ -72,7 +72,7 @@ fn next_token(input: &mut &[u8]) -> PResult { Ok(count) } -fn opt_one_of(input: &mut &[u8]) -> PResult { +fn opt_one_of(input: &mut &[u8]) -> ModalResult { let mut count = 0; while !input.is_empty() { while opt(one_of(AsChar::is_dec_digit)) @@ -89,7 +89,7 @@ fn opt_one_of(input: &mut &[u8]) -> PResult { Ok(count) } -fn take_while(input: &mut &[u8]) -> PResult { +fn take_while(input: &mut &[u8]) -> ModalResult { let mut count = 0; while !input.is_empty() { count += winnow::token::take_while(0.., AsChar::is_dec_digit) @@ -100,7 +100,7 @@ fn take_while(input: &mut &[u8]) -> PResult { Ok(count) } -fn repeat(input: &mut &[u8]) -> PResult { +fn repeat(input: &mut &[u8]) -> ModalResult { let mut count = 0; while !input.is_empty() { count += winnow::combinator::repeat(0.., one_of(AsChar::is_dec_digit)) diff --git a/benches/next_slice.rs b/benches/next_slice.rs index b8f2e0b7..28378b41 100644 --- a/benches/next_slice.rs +++ b/benches/next_slice.rs @@ -89,43 +89,43 @@ fn next_slice(c: &mut criterion::Criterion) { group.finish(); } -fn parser_ascii_char(input: &mut &str) -> PResult { +fn parser_ascii_char(input: &mut &str) -> ModalResult { repeat(0.., 'h').parse_next(input) } -fn parser_ascii_str(input: &mut &str) -> PResult { +fn parser_ascii_str(input: &mut &str) -> ModalResult { repeat(0.., "h").parse_next(input) } -fn parser_ascii_one_of(input: &mut &str) -> PResult { +fn parser_ascii_one_of(input: &mut &str) -> ModalResult { repeat(0.., one_of('h')).parse_next(input) } -fn parser_ascii_tag_char(input: &mut &str) -> PResult { +fn parser_ascii_tag_char(input: &mut &str) -> ModalResult { repeat(0.., literal('h')).parse_next(input) } -fn parser_ascii_tag_str(input: &mut &str) -> PResult { +fn parser_ascii_tag_str(input: &mut &str) -> ModalResult { repeat(0.., literal("h")).parse_next(input) } -fn parser_utf8_char(input: &mut &str) -> PResult { +fn parser_utf8_char(input: &mut &str) -> ModalResult { repeat(0.., '🧑').parse_next(input) } -fn parser_utf8_str(input: &mut &str) -> PResult { +fn parser_utf8_str(input: &mut &str) -> ModalResult { repeat(0.., "🧑").parse_next(input) } -fn parser_utf8_one_of(input: &mut &str) -> PResult { +fn parser_utf8_one_of(input: &mut &str) -> ModalResult { repeat(0.., one_of('🧑')).parse_next(input) } -fn parser_utf8_tag_char(input: &mut &str) -> PResult { +fn parser_utf8_tag_char(input: &mut &str) -> ModalResult { repeat(0.., literal('🧑')).parse_next(input) } -fn parser_utf8_tag_str(input: &mut &str) -> PResult { +fn parser_utf8_tag_str(input: &mut &str) -> ModalResult { repeat(0.., literal("🧑")).parse_next(input) } diff --git a/benches/number.rs b/benches/number.rs index d35d65c2..cb70ec2d 100644 --- a/benches/number.rs +++ b/benches/number.rs @@ -14,7 +14,7 @@ use winnow::stream::ParseSlice; type Stream<'i> = &'i [u8]; -fn parser(i: &mut Stream<'_>) -> PResult { +fn parser(i: &mut Stream<'_>) -> ModalResult { be_u64.parse_next(i) } @@ -49,7 +49,7 @@ fn float_str(c: &mut Criterion) { }); } -fn std_float(input: &mut &[u8]) -> PResult { +fn std_float(input: &mut &[u8]) -> ModalResult { match input.parse_slice() { Some(n) => Ok(n), None => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)), diff --git a/examples/arithmetic/parser.rs b/examples/arithmetic/parser.rs index 2e174d80..2a7a5f1e 100644 --- a/examples/arithmetic/parser.rs +++ b/examples/arithmetic/parser.rs @@ -11,7 +11,7 @@ use winnow::{ // Parser definition -pub(crate) fn expr(i: &mut &str) -> PResult { +pub(crate) fn expr(i: &mut &str) -> ModalResult { let init = term.parse_next(i)?; repeat(0.., (one_of(['+', '-']), term)) @@ -31,7 +31,7 @@ pub(crate) fn expr(i: &mut &str) -> PResult { // We read an initial factor and for each time we find // a * or / operator followed by another factor, we do // the math by folding everything -fn term(i: &mut &str) -> PResult { +fn term(i: &mut &str) -> ModalResult { let init = factor.parse_next(i)?; repeat(0.., (one_of(['*', '/']), factor)) @@ -52,7 +52,7 @@ fn term(i: &mut &str) -> PResult { // We look for a digit suite, and try to convert it. // If either str::from_utf8 or FromStr::from_str fail, // we fallback to the parens parser defined above -fn factor(i: &mut &str) -> PResult { +fn factor(i: &mut &str) -> ModalResult { delimited( multispaces, alt((digits.try_map(FromStr::from_str), parens)), @@ -62,7 +62,7 @@ fn factor(i: &mut &str) -> PResult { } // We parse any expr surrounded by parens, ignoring all whitespace around those -fn parens(i: &mut &str) -> PResult { +fn parens(i: &mut &str) -> ModalResult { delimited('(', expr, ')').parse_next(i) } diff --git a/examples/arithmetic/parser_ast.rs b/examples/arithmetic/parser_ast.rs index 251d7304..e7bc6e11 100644 --- a/examples/arithmetic/parser_ast.rs +++ b/examples/arithmetic/parser_ast.rs @@ -49,7 +49,7 @@ impl Display for Expr { } } -pub(crate) fn expr(i: &mut &str) -> PResult { +pub(crate) fn expr(i: &mut &str) -> ModalResult { let init = term.parse_next(i)?; repeat(0.., (one_of(['+', '-']), term)) @@ -66,7 +66,7 @@ pub(crate) fn expr(i: &mut &str) -> PResult { .parse_next(i) } -fn term(i: &mut &str) -> PResult { +fn term(i: &mut &str) -> ModalResult { let init = factor.parse_next(i)?; repeat(0.., (one_of(['*', '/']), factor)) @@ -83,7 +83,7 @@ fn term(i: &mut &str) -> PResult { .parse_next(i) } -fn factor(i: &mut &str) -> PResult { +fn factor(i: &mut &str) -> ModalResult { delimited( multispaces, alt((digits.try_map(FromStr::from_str).map(Expr::Value), parens)), @@ -92,7 +92,7 @@ fn factor(i: &mut &str) -> PResult { .parse_next(i) } -fn parens(i: &mut &str) -> PResult { +fn parens(i: &mut &str) -> ModalResult { delimited("(", expr, ")") .map(|e| Expr::Paren(Box::new(e))) .parse_next(i) diff --git a/examples/arithmetic/parser_lexer.rs b/examples/arithmetic/parser_lexer.rs index 632efd90..5a5eaea1 100644 --- a/examples/arithmetic/parser_lexer.rs +++ b/examples/arithmetic/parser_lexer.rs @@ -98,16 +98,16 @@ impl winnow::stream::ContainsToken for [Token; LEN] { } #[allow(dead_code)] -pub(crate) fn expr2(i: &mut &str) -> PResult { +pub(crate) fn expr2(i: &mut &str) -> ModalResult { let tokens = lex.parse_next(i)?; expr.parse_next(&mut tokens.as_slice()) } -pub(crate) fn lex(i: &mut &str) -> PResult> { +pub(crate) fn lex(i: &mut &str) -> ModalResult> { preceded(multispaces, repeat(1.., terminated(token, multispaces))).parse_next(i) } -fn token(i: &mut &str) -> PResult { +fn token(i: &mut &str) -> ModalResult { dispatch! {peek(any); '0'..='9' => digits.try_map(FromStr::from_str).map(Token::Value), '(' => '('.value(Token::OpenParen), @@ -121,7 +121,7 @@ fn token(i: &mut &str) -> PResult { .parse_next(i) } -pub(crate) fn expr(i: &mut &[Token]) -> PResult { +pub(crate) fn expr(i: &mut &[Token]) -> ModalResult { let init = term.parse_next(i)?; repeat( @@ -144,7 +144,7 @@ pub(crate) fn expr(i: &mut &[Token]) -> PResult { .parse_next(i) } -fn term(i: &mut &[Token]) -> PResult { +fn term(i: &mut &[Token]) -> ModalResult { let init = factor.parse_next(i)?; repeat( @@ -167,7 +167,7 @@ fn term(i: &mut &[Token]) -> PResult { .parse_next(i) } -fn factor(i: &mut &[Token]) -> PResult { +fn factor(i: &mut &[Token]) -> ModalResult { alt(( one_of(|t| matches!(t, Token::Value(_))).map(|t| match t { Token::Value(v) => Expr::Value(v), @@ -178,7 +178,7 @@ fn factor(i: &mut &[Token]) -> PResult { .parse_next(i) } -fn parens(i: &mut &[Token]) -> PResult { +fn parens(i: &mut &[Token]) -> ModalResult { delimited(one_of(Token::OpenParen), expr, one_of(Token::CloseParen)) .map(|e| Expr::Paren(Box::new(e))) .parse_next(i) diff --git a/examples/css/parser.rs b/examples/css/parser.rs index 8088de52..e2f23b88 100644 --- a/examples/css/parser.rs +++ b/examples/css/parser.rs @@ -18,7 +18,7 @@ impl std::str::FromStr for Color { } } -pub(crate) fn hex_color(input: &mut &str) -> PResult { +pub(crate) fn hex_color(input: &mut &str) -> ModalResult { seq!(Color { _: '#', red: hex_primary, @@ -28,7 +28,7 @@ pub(crate) fn hex_color(input: &mut &str) -> PResult { .parse_next(input) } -fn hex_primary(input: &mut &str) -> PResult { +fn hex_primary(input: &mut &str) -> ModalResult { take_while(2, |c: char| c.is_ascii_hexdigit()) .try_map(|input| u8::from_str_radix(input, 16)) .parse_next(input) diff --git a/examples/custom_error.rs b/examples/custom_error.rs index 0f184fd5..5300a692 100644 --- a/examples/custom_error.rs +++ b/examples/custom_error.rs @@ -48,7 +48,7 @@ impl FromExtern } } -pub fn parse<'s>(_input: &mut &'s str) -> PResult<&'s str, CustomError<&'s str>> { +pub fn parse<'s>(_input: &mut &'s str) -> ModalResult<&'s str, CustomError<&'s str>> { Err(ErrMode::Backtrack(CustomError::MyError)) } diff --git a/examples/http/parser.rs b/examples/http/parser.rs index 4c790f4c..227e4fd8 100644 --- a/examples/http/parser.rs +++ b/examples/http/parser.rs @@ -43,7 +43,7 @@ pub(crate) fn parse(data: &[u8]) -> Option, Vec>)>> Some(v) } -fn request<'s>(input: &mut Stream<'s>) -> PResult<(Request<'s>, Vec>)> { +fn request<'s>(input: &mut Stream<'s>) -> ModalResult<(Request<'s>, Vec>)> { let req = request_line(input)?; let h = repeat(1.., message_header).parse_next(input)?; let _ = line_ending.parse_next(input)?; @@ -51,7 +51,7 @@ fn request<'s>(input: &mut Stream<'s>) -> PResult<(Request<'s>, Vec>) Ok((req, h)) } -fn request_line<'s>(input: &mut Stream<'s>) -> PResult> { +fn request_line<'s>(input: &mut Stream<'s>) -> ModalResult> { seq!( Request { method: take_while(1.., is_token), _: take_while(1.., is_space), @@ -63,14 +63,14 @@ fn request_line<'s>(input: &mut Stream<'s>) -> PResult> { .parse_next(input) } -fn http_version<'s>(input: &mut Stream<'s>) -> PResult<&'s [u8]> { +fn http_version<'s>(input: &mut Stream<'s>) -> ModalResult<&'s [u8]> { let _ = "HTTP/".parse_next(input)?; let version = take_while(1.., is_version).parse_next(input)?; Ok(version) } -fn message_header_value<'s>(input: &mut Stream<'s>) -> PResult<&'s [u8]> { +fn message_header_value<'s>(input: &mut Stream<'s>) -> ModalResult<&'s [u8]> { let _ = take_while(1.., is_horizontal_space).parse_next(input)?; let data = take_while(1.., till_line_ending).parse_next(input)?; let _ = line_ending.parse_next(input)?; @@ -78,7 +78,7 @@ fn message_header_value<'s>(input: &mut Stream<'s>) -> PResult<&'s [u8]> { Ok(data) } -fn message_header<'s>(input: &mut Stream<'s>) -> PResult> { +fn message_header<'s>(input: &mut Stream<'s>) -> ModalResult> { seq!(Header { name: take_while(1.., is_token), _: ':', diff --git a/examples/http/parser_streaming.rs b/examples/http/parser_streaming.rs index 361730e6..e5181a13 100644 --- a/examples/http/parser_streaming.rs +++ b/examples/http/parser_streaming.rs @@ -44,7 +44,7 @@ pub(crate) fn parse(data: &[u8]) -> Option, Vec>)>> Some(v) } -fn request<'s>(input: &mut Stream<'s>) -> PResult<(Request<'s>, Vec>)> { +fn request<'s>(input: &mut Stream<'s>) -> ModalResult<(Request<'s>, Vec>)> { let req = request_line(input)?; let h = repeat(1.., message_header).parse_next(input)?; let _ = line_ending.parse_next(input)?; @@ -52,7 +52,7 @@ fn request<'s>(input: &mut Stream<'s>) -> PResult<(Request<'s>, Vec>) Ok((req, h)) } -fn request_line<'s>(input: &mut Stream<'s>) -> PResult> { +fn request_line<'s>(input: &mut Stream<'s>) -> ModalResult> { seq!( Request { method: take_while(1.., is_token), _: take_while(1.., is_space), @@ -64,14 +64,14 @@ fn request_line<'s>(input: &mut Stream<'s>) -> PResult> { .parse_next(input) } -fn http_version<'s>(input: &mut Stream<'s>) -> PResult<&'s [u8]> { +fn http_version<'s>(input: &mut Stream<'s>) -> ModalResult<&'s [u8]> { let _ = "HTTP/".parse_next(input)?; let version = take_while(1.., is_version).parse_next(input)?; Ok(version) } -fn message_header_value<'s>(input: &mut Stream<'s>) -> PResult<&'s [u8]> { +fn message_header_value<'s>(input: &mut Stream<'s>) -> ModalResult<&'s [u8]> { let _ = take_while(1.., is_horizontal_space).parse_next(input)?; let data = take_while(1.., till_line_ending).parse_next(input)?; let _ = line_ending.parse_next(input)?; @@ -79,7 +79,7 @@ fn message_header_value<'s>(input: &mut Stream<'s>) -> PResult<&'s [u8]> { Ok(data) } -fn message_header<'s>(input: &mut Stream<'s>) -> PResult> { +fn message_header<'s>(input: &mut Stream<'s>) -> ModalResult> { seq!(Header { name: take_while(1.., is_token), _: ':', diff --git a/examples/ini/bench.rs b/examples/ini/bench.rs index b6ccf288..677c2754 100644 --- a/examples/ini/bench.rs +++ b/examples/ini/bench.rs @@ -31,7 +31,7 @@ port=143 file=payroll.dat \0"; - fn acc<'s>(i: &mut parser::Stream<'s>) -> PResult> { + fn acc<'s>(i: &mut parser::Stream<'s>) -> ModalResult> { repeat(0.., parser::key_value).parse_next(i) } diff --git a/examples/ini/parser.rs b/examples/ini/parser.rs index 13b552e0..1dbfc3df 100644 --- a/examples/ini/parser.rs +++ b/examples/ini/parser.rs @@ -14,7 +14,7 @@ pub(crate) type Stream<'i> = &'i [u8]; pub(crate) fn categories<'s>( i: &mut Stream<'s>, -) -> PResult>> { +) -> ModalResult>> { repeat( 0.., separated_pair( @@ -26,13 +26,13 @@ pub(crate) fn categories<'s>( .parse_next(i) } -fn category<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { +fn category<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { delimited('[', take_while(0.., |c| c != b']'), ']') .try_map(str::from_utf8) .parse_next(i) } -pub(crate) fn key_value<'s>(i: &mut Stream<'s>) -> PResult<(&'s str, &'s str)> { +pub(crate) fn key_value<'s>(i: &mut Stream<'s>) -> ModalResult<(&'s str, &'s str)> { let key = alphanumeric.try_map(str::from_utf8).parse_next(i)?; let _ = (opt(space), '=', opt(space)).parse_next(i)?; let val = take_while(0.., |c| c != b'\n' && c != b';') diff --git a/examples/ini/parser_str.rs b/examples/ini/parser_str.rs index 8af75a55..df2a9ed9 100644 --- a/examples/ini/parser_str.rs +++ b/examples/ini/parser_str.rs @@ -13,15 +13,15 @@ pub(crate) type Stream<'i> = &'i str; pub(crate) fn categories<'s>( input: &mut Stream<'s>, -) -> PResult>> { +) -> ModalResult>> { repeat(0.., category_and_keys).parse_next(input) } -fn category_and_keys<'s>(i: &mut Stream<'s>) -> PResult<(&'s str, HashMap<&'s str, &'s str>)> { +fn category_and_keys<'s>(i: &mut Stream<'s>) -> ModalResult<(&'s str, HashMap<&'s str, &'s str>)> { (category, keys_and_values).parse_next(i) } -fn category<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { +fn category<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { terminated( delimited('[', take_while(0.., |c| c != ']'), ']'), opt(take_while(1.., [' ', '\r', '\n'])), @@ -29,11 +29,11 @@ fn category<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { .parse_next(i) } -fn keys_and_values<'s>(input: &mut Stream<'s>) -> PResult> { +fn keys_and_values<'s>(input: &mut Stream<'s>) -> ModalResult> { repeat(0.., key_value).parse_next(input) } -fn key_value<'s>(i: &mut Stream<'s>) -> PResult<(&'s str, &'s str)> { +fn key_value<'s>(i: &mut Stream<'s>) -> ModalResult<(&'s str, &'s str)> { let key = alphanumeric.parse_next(i)?; let _ = (opt(space), "=", opt(space)).parse_next(i)?; let val = take_till(0.., is_line_ending_or_comment).parse_next(i)?; @@ -48,11 +48,11 @@ fn is_line_ending_or_comment(chr: char) -> bool { chr == ';' || chr == '\n' } -fn till_line_ending<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { +fn till_line_ending<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { take_while(0.., |c| c != '\r' && c != '\n').parse_next(i) } -fn space_or_line_ending<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { +fn space_or_line_ending<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { take_while(1.., [' ', '\r', '\n']).parse_next(i) } diff --git a/examples/iterator.rs b/examples/iterator.rs index a394f716..b3ccc89f 100644 --- a/examples/iterator.rs +++ b/examples/iterator.rs @@ -9,7 +9,7 @@ use winnow::prelude::*; fn main() { let mut data = "abcabcabcabc"; - fn parser<'s>(i: &mut &'s str) -> PResult<&'s str> { + fn parser<'s>(i: &mut &'s str) -> ModalResult<&'s str> { "abc".parse_next(i) } @@ -66,7 +66,7 @@ fn main() { .map(|(k, v)| (k.to_uppercase(), v)) .collect::>(); - let parser_result: PResult<(_, _), ()> = winnow_it.finish(); + let parser_result: ModalResult<(_, _), ()> = winnow_it.finish(); let (remaining_input, ()) = parser_result.unwrap(); // will print "iterator returned {"key1": "value1", "key3": "value3", "key2": "value2"}, remaining input is ';'" diff --git a/examples/json/parser.rs b/examples/json/parser.rs index a9597f2c..c2f8c6ac 100644 --- a/examples/json/parser.rs +++ b/examples/json/parser.rs @@ -19,8 +19,8 @@ pub(crate) type Stream<'i> = &'i str; /// The root element of a JSON parser is any value /// /// A parser has the following signature: -/// `&mut Stream -> PResult`, with `PResult` defined as: -/// `type PResult = Result>;` +/// `&mut Stream -> ModalResult`, with `ModalResult` defined as: +/// `type ModalResult = Result>;` /// /// most of the times you can ignore the error type and use the default (but this /// examples shows custom error types later on!) @@ -30,7 +30,7 @@ pub(crate) type Stream<'i> = &'i str; /// implements the required traits. pub(crate) fn json<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { delimited(ws, json_value, ws).parse_next(input) } @@ -38,7 +38,7 @@ pub(crate) fn json<'i, E: ParserError> + AddContext, StrCo /// one of them succeeds fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { // `alt` combines the each value parser. It returns the result of the first // successful parser, or an error alt(( @@ -55,7 +55,7 @@ fn json_value<'i, E: ParserError> + AddContext, StrContext /// `literal(string)` generates a parser that takes the argument string. /// /// This also shows returning a sub-slice of the original input -fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // This is a parser that returns `"null"` if it sees the string "null", and // an error otherwise "null".parse_next(input) @@ -63,7 +63,7 @@ fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i s /// We can combine `tag` with other functions, like `value` which returns a given constant value on /// success. -fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { // This is a parser that returns `true` if it sees the string "true", and // an error otherwise let parse_true = "true".value(true); @@ -79,7 +79,7 @@ fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { preceded( '\"', // `cut_err` transforms an `ErrMode::Backtrack(e)` to `ErrMode::Cut(e)`, signaling to @@ -102,7 +102,7 @@ fn string<'i, E: ParserError> + AddContext, StrContext>>( /// You can mix the above declarative parsing with an imperative style to handle more unique cases, /// like escaping -fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { let c = none_of('\"').parse_next(input)?; if c == '\\' { alt(( @@ -125,7 +125,7 @@ fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult< } } -fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { alt(( // Not a surrogate u16_hex @@ -147,7 +147,7 @@ fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PRe .parse_next(input) } -fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { take(4usize) .verify_map(|s| u16::from_str_radix(s, 16).ok()) .parse_next(input) @@ -159,7 +159,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('[', ws), cut_err(terminated( @@ -173,7 +173,7 @@ fn array<'i, E: ParserError> + AddContext, StrContext>>( fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('{', ws), cut_err(terminated( @@ -187,14 +187,14 @@ fn object<'i, E: ParserError> + AddContext, StrContext>>( fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult<(String, JsonValue), E> { +) -> ModalResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) } /// Parser combinators are constructed from the bottom up: /// first we write parsers for the smallest elements (here a space character), /// then we'll combine them in larger parsers -fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // Combinators like `take_while` return a function. That function is the // parser,to which we can pass the input take_while(0.., WS).parse_next(input) diff --git a/examples/json/parser_dispatch.rs b/examples/json/parser_dispatch.rs index 06b391b4..501d9e06 100644 --- a/examples/json/parser_dispatch.rs +++ b/examples/json/parser_dispatch.rs @@ -22,8 +22,8 @@ pub(crate) type Stream<'i> = &'i str; /// The root element of a JSON parser is any value /// /// A parser has the following signature: -/// `&mut Stream -> PResult`, with `PResult` defined as: -/// `type PResult = Result>;` +/// `&mut Stream -> ModalResult`, with `ModalResult` defined as: +/// `type ModalResult = Result>;` /// /// most of the times you can ignore the error type and use the default (but this /// examples shows custom error types later on!) @@ -33,7 +33,7 @@ pub(crate) type Stream<'i> = &'i str; /// implements the required traits. pub(crate) fn json<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { delimited(ws, json_value, ws).parse_next(input) } @@ -41,7 +41,7 @@ pub(crate) fn json<'i, E: ParserError> + AddContext, StrCo /// one of them succeeds fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { // `dispatch` gives you `match`-like behavior compared to `alt` successively trying different // implementations. dispatch!(peek(any); @@ -62,7 +62,7 @@ fn json_value<'i, E: ParserError> + AddContext, StrContext /// `literal(string)` generates a parser that takes the argument string. /// /// This also shows returning a sub-slice of the original input -fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // This is a parser that returns `"null"` if it sees the string "null", and // an error otherwise "null".parse_next(input) @@ -70,7 +70,7 @@ fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i s /// We can combine `tag` with other functions, like `value` which returns a given constant value on /// success. -fn true_<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn true_<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { // This is a parser that returns `true` if it sees the string "true", and // an error otherwise "true".value(true).parse_next(input) @@ -78,7 +78,7 @@ fn true_<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult>>(input: &mut Stream<'i>) -> PResult { +fn false_<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { // This is a parser that returns `false` if it sees the string "false", and // an error otherwise "false".value(false).parse_next(input) @@ -88,7 +88,7 @@ fn false_<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { preceded( '\"', // `cut_err` transforms an `ErrMode::Backtrack(e)` to `ErrMode::Cut(e)`, signaling to @@ -111,7 +111,7 @@ fn string<'i, E: ParserError> + AddContext, StrContext>>( /// You can mix the above declarative parsing with an imperative style to handle more unique cases, /// like escaping -fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { let c = none_of('\"').parse_next(input)?; if c == '\\' { dispatch!(any; @@ -132,7 +132,7 @@ fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult< } } -fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { alt(( // Not a surrogate u16_hex @@ -154,7 +154,7 @@ fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PRe .parse_next(input) } -fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { take(4usize) .verify_map(|s| u16::from_str_radix(s, 16).ok()) .parse_next(input) @@ -166,7 +166,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('[', ws), cut_err(terminated( @@ -180,7 +180,7 @@ fn array<'i, E: ParserError> + AddContext, StrContext>>( fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('{', ws), cut_err(terminated( @@ -194,14 +194,14 @@ fn object<'i, E: ParserError> + AddContext, StrContext>>( fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult<(String, JsonValue), E> { +) -> ModalResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) } /// Parser combinators are constructed from the bottom up: /// first we write parsers for the smallest elements (here a space character), /// then we'll combine them in larger parsers -fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // Combinators like `take_while` return a function. That function is the // parser,to which we can pass the input take_while(0.., WS).parse_next(input) diff --git a/examples/json/parser_partial.rs b/examples/json/parser_partial.rs index 864f7b2b..9c95beb8 100644 --- a/examples/json/parser_partial.rs +++ b/examples/json/parser_partial.rs @@ -20,8 +20,8 @@ pub(crate) type Stream<'i> = Partial<&'i str>; /// The root element of a JSON parser is any value /// /// A parser has the following signature: -/// `&mut Stream -> PResult`, with `PResult` defined as: -/// `type PResult = Result>;` +/// `&mut Stream -> ModalResult`, with `ModalResult` defined as: +/// `type ModalResult = Result>;` /// /// most of the times you can ignore the error type and use the default (but this /// examples shows custom error types later on!) @@ -31,7 +31,7 @@ pub(crate) type Stream<'i> = Partial<&'i str>; /// implements the required traits. pub(crate) fn json<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { delimited(ws, json_value, ws_or_eof).parse_next(input) } @@ -39,7 +39,7 @@ pub(crate) fn json<'i, E: ParserError> + AddContext, StrCo /// one of them succeeds fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { // `alt` combines the each value parser. It returns the result of the first // successful parser, or an error alt(( @@ -56,7 +56,7 @@ fn json_value<'i, E: ParserError> + AddContext, StrContext /// `literal(string)` generates a parser that takes the argument string. /// /// This also shows returning a sub-slice of the original input -fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // This is a parser that returns `"null"` if it sees the string "null", and // an error otherwise "null".parse_next(input) @@ -64,7 +64,7 @@ fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i s /// We can combine `tag` with other functions, like `value` which returns a given constant value on /// success. -fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { // This is a parser that returns `true` if it sees the string "true", and // an error otherwise let parse_true = "true".value(true); @@ -80,7 +80,7 @@ fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { preceded( '\"', // `cut_err` transforms an `ErrMode::Backtrack(e)` to `ErrMode::Cut(e)`, signaling to @@ -103,7 +103,7 @@ fn string<'i, E: ParserError> + AddContext, StrContext>>( /// You can mix the above declarative parsing with an imperative style to handle more unique cases, /// like escaping -fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { let c = none_of('\"').parse_next(input)?; if c == '\\' { alt(( @@ -126,7 +126,7 @@ fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult< } } -fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { alt(( // Not a surrogate u16_hex @@ -148,7 +148,7 @@ fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PRe .parse_next(input) } -fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { take(4usize) .verify_map(|s| u16::from_str_radix(s, 16).ok()) .parse_next(input) @@ -160,7 +160,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('[', ws), cut_err(terminated( @@ -174,7 +174,7 @@ fn array<'i, E: ParserError> + AddContext, StrContext>>( fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('{', ws), cut_err(terminated( @@ -188,20 +188,20 @@ fn object<'i, E: ParserError> + AddContext, StrContext>>( fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult<(String, JsonValue), E> { +) -> ModalResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) } /// Parser combinators are constructed from the bottom up: /// first we write parsers for the smallest elements (here a space character), /// then we'll combine them in larger parsers -fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // Combinators like `take_while` return a function. That function is the // parser,to which we can pass the input take_while(0.., WS).parse_next(input) } -fn ws_or_eof<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn ws_or_eof<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { rest.verify(|s: &str| s.chars().all(|c| WS.contains(&c))) .parse_next(input) } diff --git a/examples/json_iterator.rs b/examples/json_iterator.rs index 3ca86773..6232653f 100644 --- a/examples/json_iterator.rs +++ b/examples/json_iterator.rs @@ -209,27 +209,27 @@ impl<'a, 'b: 'a> JsonValue<'a, 'b> { } } -fn sp<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<&'a str, E> { +fn sp<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> ModalResult<&'a str, E> { let chars = " \t\r\n"; take_while(0.., move |c| chars.contains(c)).parse_next(i) } -fn parse_str<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<&'a str, E> { +fn parse_str<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> ModalResult<&'a str, E> { take_escaped(alphanumeric, '\\', one_of(['"', 'n', '\\'])).parse_next(i) } -fn string<'s>(i: &mut &'s str) -> PResult<&'s str> { +fn string<'s>(i: &mut &'s str) -> ModalResult<&'s str> { preceded('\"', cut_err(terminated(parse_str, '\"'))) .context(StrContext::Label("string")) .parse_next(i) } -fn boolean(input: &mut &str) -> PResult { +fn boolean(input: &mut &str) -> ModalResult { alt(("false".map(|_| false), "true".map(|_| true))).parse_next(input) } -fn array(i: &mut &str) -> PResult<()> { +fn array(i: &mut &str) -> ModalResult<()> { preceded( '[', cut_err(terminated( @@ -241,11 +241,11 @@ fn array(i: &mut &str) -> PResult<()> { .parse_next(i) } -fn key_value<'s>(i: &mut &'s str) -> PResult<(&'s str, ())> { +fn key_value<'s>(i: &mut &'s str) -> ModalResult<(&'s str, ())> { separated_pair(preceded(sp, string), cut_err(preceded(sp, ':')), value).parse_next(i) } -fn hash(i: &mut &str) -> PResult<()> { +fn hash(i: &mut &str) -> ModalResult<()> { preceded( '{', cut_err(terminated( @@ -257,7 +257,7 @@ fn hash(i: &mut &str) -> PResult<()> { .parse_next(i) } -fn value(i: &mut &str) -> PResult<()> { +fn value(i: &mut &str) -> ModalResult<()> { preceded( sp, alt(( diff --git a/examples/ndjson/parser.rs b/examples/ndjson/parser.rs index 4d5f495f..ba5201dd 100644 --- a/examples/ndjson/parser.rs +++ b/examples/ndjson/parser.rs @@ -29,7 +29,7 @@ pub(crate) type Stream<'i> = Partial<&'i str>; pub(crate) fn ndjson<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { alt(( terminated(delimited(ws, json_value, ws), line_ending).map(Some), line_ending.value(None), @@ -43,7 +43,7 @@ pub(crate) fn ndjson<'i, E: ParserError> + AddContext, Str /// one of them succeeds fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { // `alt` combines the each value parser. It returns the result of the first // successful parser, or an error alt(( @@ -60,7 +60,7 @@ fn json_value<'i, E: ParserError> + AddContext, StrContext /// `literal(string)` generates a parser that takes the argument string. /// /// This also shows returning a sub-slice of the original input -fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // This is a parser that returns `"null"` if it sees the string "null", and // an error otherwise "null".parse_next(input) @@ -68,7 +68,7 @@ fn null<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i s /// We can combine `tag` with other functions, like `value` which returns a given constant value on /// success. -fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { // This is a parser that returns `true` if it sees the string "true", and // an error otherwise let parse_true = "true".value(true); @@ -84,7 +84,7 @@ fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult { +) -> ModalResult { preceded( '\"', // `cut_err` transforms an `ErrMode::Backtrack(e)` to `ErrMode::Cut(e)`, signaling to @@ -107,7 +107,7 @@ fn string<'i, E: ParserError> + AddContext, StrContext>>( /// You can mix the above declarative parsing with an imperative style to handle more unique cases, /// like escaping -fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { let c = none_of('"').parse_next(input)?; if c == '\\' { alt(( @@ -130,7 +130,7 @@ fn character<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult< } } -fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { alt(( // Not a surrogate u16_hex @@ -152,7 +152,7 @@ fn unicode_escape<'i, E: ParserError>>(input: &mut Stream<'i>) -> PRe .parse_next(input) } -fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult { +fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult { take(4usize) .verify_map(|s| u16::from_str_radix(s, 16).ok()) .parse_next(input) @@ -164,7 +164,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('[', ws), cut_err(terminated( @@ -178,7 +178,7 @@ fn array<'i, E: ParserError> + AddContext, StrContext>>( fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult, E> { +) -> ModalResult, E> { preceded( ('{', ws), cut_err(terminated( @@ -192,14 +192,14 @@ fn object<'i, E: ParserError> + AddContext, StrContext>>( fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, -) -> PResult<(String, JsonValue), E> { +) -> ModalResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) } /// Parser combinators are constructed from the bottom up: /// first we write parsers for the smallest elements (here a space character), /// then we'll combine them in larger parsers -fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult<&'i str, E> { +fn ws<'i, E: ParserError>>(input: &mut Stream<'i>) -> ModalResult<&'i str, E> { // Combinators like `take_while` return a function. That function is the // parser,to which we can pass the input take_while(0.., WS).parse_next(input) diff --git a/examples/s_expression/parser.rs b/examples/s_expression/parser.rs index abcf5a4a..dfedf59c 100644 --- a/examples/s_expression/parser.rs +++ b/examples/s_expression/parser.rs @@ -70,7 +70,7 @@ pub(crate) enum BuiltIn { } /// With types defined, we move onto the top-level expression parser! -fn parse_expr(i: &mut &'_ str) -> PResult { +fn parse_expr(i: &mut &'_ str) -> ModalResult { preceded( multispace0, alt((parse_constant, parse_application, parse_if, parse_quote)), @@ -79,13 +79,13 @@ fn parse_expr(i: &mut &'_ str) -> PResult { } /// We then add the Expr layer on top -fn parse_constant(i: &mut &'_ str) -> PResult { +fn parse_constant(i: &mut &'_ str) -> ModalResult { parse_atom.map(Expr::Constant).parse_next(i) } /// Now we take all these simple parsers and connect them. /// We can now parse half of our language! -fn parse_atom(i: &mut &'_ str) -> PResult { +fn parse_atom(i: &mut &'_ str) -> ModalResult { alt(( parse_num, parse_bool, @@ -97,7 +97,7 @@ fn parse_atom(i: &mut &'_ str) -> PResult { /// Next up is number parsing. We're keeping it simple here by accepting any number (> 1) /// of digits but ending the program if it doesn't fit into an i32. -fn parse_num(i: &mut &'_ str) -> PResult { +fn parse_num(i: &mut &'_ str) -> ModalResult { alt(( digit1.try_map(|digit_str: &str| digit_str.parse::().map(Atom::Num)), preceded("-", digit1).map(|digit_str: &str| Atom::Num(-digit_str.parse::().unwrap())), @@ -106,7 +106,7 @@ fn parse_num(i: &mut &'_ str) -> PResult { } /// Our boolean values are also constant, so we can do it the same way -fn parse_bool(i: &mut &'_ str) -> PResult { +fn parse_bool(i: &mut &'_ str) -> ModalResult { alt(( "#t".map(|_| Atom::Boolean(true)), "#f".map(|_| Atom::Boolean(false)), @@ -114,7 +114,7 @@ fn parse_bool(i: &mut &'_ str) -> PResult { .parse_next(i) } -fn parse_builtin(i: &mut &'_ str) -> PResult { +fn parse_builtin(i: &mut &'_ str) -> ModalResult { // alt gives us the result of first parser that succeeds, of the series of // parsers we give it alt(( @@ -128,7 +128,7 @@ fn parse_builtin(i: &mut &'_ str) -> PResult { /// Continuing the trend of starting from the simplest piece and building up, /// we start by creating a parser for the built-in operator functions. -fn parse_builtin_op(i: &mut &'_ str) -> PResult { +fn parse_builtin_op(i: &mut &'_ str) -> ModalResult { // one_of matches one of the characters we give it let t = one_of(['+', '-', '*', '/', '=']).parse_next(i)?; @@ -150,7 +150,7 @@ fn parse_builtin_op(i: &mut &'_ str) -> PResult { /// /// Put plainly: `preceded(":", cut_err(alpha1))` means that once we see the `:` /// character, we have to see one or more alphabetic characters or the input is invalid. -fn parse_keyword(i: &mut &'_ str) -> PResult { +fn parse_keyword(i: &mut &'_ str) -> ModalResult { preceded(":", cut_err(alpha1)) .context(StrContext::Label("keyword")) .map(|sym_str: &str| Atom::Keyword(sym_str.to_owned())) @@ -166,7 +166,7 @@ fn parse_keyword(i: &mut &'_ str) -> PResult { /// /// tuples are themselves a parser, used to sequence parsers together, so we can translate this /// directly and then map over it to transform the output into an `Expr::Application` -fn parse_application(i: &mut &'_ str) -> PResult { +fn parse_application(i: &mut &'_ str) -> ModalResult { let application_inner = (parse_expr, repeat(0.., parse_expr)) .map(|(head, tail)| Expr::Application(Box::new(head), tail)); // finally, we wrap it in an s-expression @@ -179,7 +179,7 @@ fn parse_application(i: &mut &'_ str) -> PResult { /// /// In fact, we define our parser as if `Expr::If` was defined with an Option in it, /// we have the `opt` combinator which fits very nicely here. -fn parse_if(i: &mut &'_ str) -> PResult { +fn parse_if(i: &mut &'_ str) -> ModalResult { let if_inner = preceded( // here to avoid ambiguity with other names starting with `if`, if we added // variables to our language, we say that if must be terminated by at least @@ -207,7 +207,7 @@ fn parse_if(i: &mut &'_ str) -> PResult { /// This example doesn't have the symbol atom, but by adding variables and changing /// the definition of quote to not always be around an S-expression, we'd get them /// naturally. -fn parse_quote(i: &mut &'_ str) -> PResult { +fn parse_quote(i: &mut &'_ str) -> ModalResult { // this should look very straight-forward after all we've done: // we find the `'` (quote) character, use cut_err to say that we're unambiguously // looking for an s-expression of 0 or more expressions, and then parse them diff --git a/examples/string/parser.rs b/examples/string/parser.rs index a84df74e..4940b83f 100644 --- a/examples/string/parser.rs +++ b/examples/string/parser.rs @@ -19,7 +19,7 @@ use winnow::token::{take_till, take_while}; /// Parse a string. Use a loop of `parse_fragment` and push all of the fragments /// into an output string. -pub(crate) fn parse_string<'a, E>(input: &mut &'a str) -> PResult +pub(crate) fn parse_string<'a, E>(input: &mut &'a str) -> ModalResult where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -64,7 +64,7 @@ enum StringFragment<'a> { /// Combine `parse_literal`, `parse_escaped_whitespace`, and `parse_escaped_char` /// into a `StringFragment`. -fn parse_fragment<'a, E>(input: &mut &'a str) -> PResult, E> +fn parse_fragment<'a, E>(input: &mut &'a str) -> ModalResult, E> where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -79,7 +79,7 @@ where } /// Parse a non-empty block of text that doesn't include \ or " -fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<&'a str, E> { +fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> ModalResult<&'a str, E> { // `take_till` parses a string of 0 or more characters that aren't one of the // given characters. let not_quote_slash = take_till(1.., ['"', '\\']); @@ -98,7 +98,7 @@ fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PResult<&' // then combine them into larger parsers. /// Parse an escaped character: \n, \t, \r, \u{00AC}, etc. -fn parse_escaped_char<'a, E>(input: &mut &'a str) -> PResult +fn parse_escaped_char<'a, E>(input: &mut &'a str) -> ModalResult where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -128,7 +128,7 @@ where /// Parse a unicode sequence, of the form u{XXXX}, where XXXX is 1 to 6 /// hexadecimal numerals. We will combine this later with `parse_escaped_char` /// to parse sequences like \u{00AC}. -fn parse_unicode<'a, E>(input: &mut &'a str) -> PResult +fn parse_unicode<'a, E>(input: &mut &'a str) -> ModalResult where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -162,6 +162,6 @@ where /// to discard any escaped whitespace. fn parse_escaped_whitespace<'a, E: ParserError<&'a str>>( input: &mut &'a str, -) -> PResult<&'a str, E> { +) -> ModalResult<&'a str, E> { preceded('\\', multispace1).parse_next(input) } diff --git a/fuzz/fuzz_targets/fuzz_arithmetic.rs b/fuzz/fuzz_targets/fuzz_arithmetic.rs index f6521e28..2c15628b 100644 --- a/fuzz/fuzz_targets/fuzz_arithmetic.rs +++ b/fuzz/fuzz_targets/fuzz_arithmetic.rs @@ -23,7 +23,7 @@ fn reset() { }); } -fn incr(i: &mut &str) -> PResult<()> { +fn incr(i: &mut &str) -> ModalResult<()> { LEVEL.with(|l| { *l.borrow_mut() += 1; @@ -45,7 +45,7 @@ fn decr() { }); } -fn parens(i: &mut &str) -> PResult { +fn parens(i: &mut &str) -> ModalResult { delimited( space, delimited(terminated("(", incr), expr, ")".map(|_| decr())), @@ -54,11 +54,11 @@ fn parens(i: &mut &str) -> PResult { .parse_next(i) } -fn factor(i: &mut &str) -> PResult { +fn factor(i: &mut &str) -> ModalResult { alt((delimited(space, digit, space).parse_to(), parens)).parse_next(i) } -fn term(i: &mut &str) -> PResult { +fn term(i: &mut &str) -> ModalResult { incr(i)?; let init = factor(i).inspect_err(|_e| { decr(); @@ -86,7 +86,7 @@ fn term(i: &mut &str) -> PResult { res } -fn expr(i: &mut &str) -> PResult { +fn expr(i: &mut &str) -> ModalResult { incr(i)?; let init = term(i).inspect_err(|_e| { decr(); diff --git a/src/_topic/language.rs b/src/_topic/language.rs index 740fbe7e..f512d5bf 100644 --- a/src/_topic/language.rs +++ b/src/_topic/language.rs @@ -65,7 +65,7 @@ //! token::take_till, //! }; //! -//! pub fn peol_comment<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<(), E> +//! pub fn peol_comment<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> ModalResult<(), E> //! { //! ('%', take_till(1.., ['\n', '\r'])) //! .void() // Output is thrown away. @@ -85,7 +85,7 @@ //! token::take_until, //! }; //! -//! pub fn pinline_comment<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> PResult<(), E> { +//! pub fn pinline_comment<'a, E: ParserError<&'a str>>(i: &mut &'a str) -> ModalResult<(), E> { //! ( //! "(*", //! take_until(0.., "*)"), @@ -111,7 +111,7 @@ //! token::one_of, //! }; //! -//! pub fn identifier<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! pub fn identifier<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! ( //! one_of(|c: char| c.is_alpha() || c == '_'), //! take_while(0.., |c: char| c.is_alphanum() || c == '_') @@ -161,7 +161,7 @@ //! token::one_of, //! }; //! -//! fn hexadecimal<'s>(input: &mut &'s str) -> PResult<&'s str> { // <'a, E: ParserError<&'a str>> +//! fn hexadecimal<'s>(input: &mut &'s str) -> ModalResult<&'s str> { // <'a, E: ParserError<&'a str>> //! preceded( //! alt(("0x", "0X")), //! repeat(1.., @@ -182,7 +182,7 @@ //! token::one_of, //! }; //! -//! fn hexadecimal_value(input: &mut &str) -> PResult { +//! fn hexadecimal_value(input: &mut &str) -> ModalResult { //! preceded( //! alt(("0x", "0X")), //! repeat(1.., @@ -207,7 +207,7 @@ //! token::one_of, //! }; //! -//! fn octal<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn octal<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! preceded( //! alt(("0o", "0O")), //! repeat(1.., @@ -228,7 +228,7 @@ //! token::one_of, //! }; //! -//! fn binary<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn binary<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! preceded( //! alt(("0b", "0B")), //! repeat(1.., @@ -248,7 +248,7 @@ //! token::one_of, //! }; //! -//! fn decimal<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn decimal<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! repeat(1.., //! terminated(one_of('0'..='9'), repeat(0.., '_').map(|()| ())) //! ).map(|()| ()) @@ -273,7 +273,7 @@ //! token::one_of, //! }; //! -//! fn float<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn float<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! alt(( //! // Case one: .42 //! ( @@ -305,7 +305,7 @@ //! )).parse_next(input) //! } //! -//! fn decimal<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn decimal<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! repeat(1.., //! terminated(one_of('0'..='9'), repeat(0.., '_').map(|()| ())) //! ). diff --git a/src/_topic/nom.rs b/src/_topic/nom.rs index 7da29a6d..2245fb41 100644 --- a/src/_topic/nom.rs +++ b/src/_topic/nom.rs @@ -82,7 +82,7 @@ //! When the Output of a parser is a slice, you have to add a lifetime: //! ```rust //! # use winnow::prelude::*; -//! fn foo<'i>(i: &mut &'i str) -> PResult<&'i str> { +//! fn foo<'i>(i: &mut &'i str) -> ModalResult<&'i str> { //! // ... //! # winnow::token::rest.parse_next(i) //! } @@ -92,7 +92,7 @@ //! ```rust //! # use winnow::prelude::*; //! # use winnow::combinator::trace; -//! fn foo(i: &mut &str) -> PResult { +//! fn foo(i: &mut &str) -> ModalResult { //! trace("foo", |i: &mut _| { //! // ... //! # Ok(0) diff --git a/src/_topic/stream.rs b/src/_topic/stream.rs index eb9573ee..5b6a197b 100644 --- a/src/_topic/stream.rs +++ b/src/_topic/stream.rs @@ -15,12 +15,12 @@ //! Let's assume we have an input type we'll call `MyStream`. //! `MyStream` is a sequence of `MyItem` type. //! -//! The goal is to define parsers with this signature: `&mut MyStream -> PResult`. +//! The goal is to define parsers with this signature: `&mut MyStream -> ModalResult`. //! ```rust //! # use winnow::prelude::*; //! # type MyStream<'i> = &'i str; //! # type Output<'i> = &'i str; -//! fn parser<'s>(i: &mut MyStream<'s>) -> PResult> { +//! fn parser<'s>(i: &mut MyStream<'s>) -> ModalResult> { //! "test".parse_next(i) //! } //! ``` diff --git a/src/_topic/why.rs b/src/_topic/why.rs index 625c5b5a..cdaa93f0 100644 --- a/src/_topic/why.rs +++ b/src/_topic/why.rs @@ -90,7 +90,7 @@ //! See also [#72](https://github.com/winnow-rs/winnow/issues/72). //! //! Downsides: -//! - When returning a slice, you have to add a lifetime (`fn foo<'i>(i: &mut &i str) -> PResult<&i str>`) +//! - When returning a slice, you have to add a lifetime (`fn foo<'i>(i: &mut &i str) -> ModalResult<&i str>`) //! - When writing a closure, you need to annotate the type (`|i: &mut _|`, at least the full type isn't needed) //! //! ## `chumsky` diff --git a/src/_tutorial/chapter_1.rs b/src/_tutorial/chapter_1.rs index 41220bc1..acbcc6b3 100644 --- a/src/_tutorial/chapter_1.rs +++ b/src/_tutorial/chapter_1.rs @@ -23,14 +23,14 @@ //! ``` //! //! -//! To represent this model of the world, winnow uses the [`PResult`] type. +//! To represent this model of the world, winnow uses the [`ModalResult`] type. //! The `Ok` variant has `output: O`; //! whereas the `Err` variant stores an error. //! //! You can import that from: //! //! ```rust -//! use winnow::PResult; +//! use winnow::ModalResult; //! ``` //! //! To combine parsers, we need a common way to refer to them which is where the [`Parser`] @@ -49,7 +49,7 @@ //! The simplest parser we can write is one which successfully does nothing. //! //! To make it easier to implement a [`Parser`], the trait is implemented for -//! functions of the form `Fn(&mut I) -> PResult`. +//! functions of the form `Fn(&mut I) -> ModalResult`. //! //! This parser function should take in a `&str`: //! @@ -58,10 +58,10 @@ //! - Since it doesn't parse anything, it also should just return an empty string. //! //! ```rust -//! use winnow::PResult; +//! use winnow::ModalResult; //! use winnow::Parser; //! -//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! Ok("") //! } //! @@ -80,7 +80,7 @@ #![allow(unused_imports)] use super::chapter_6; use super::chapter_7; -use crate::PResult; +use crate::ModalResult; use crate::Parser; pub use super::chapter_0 as previous; diff --git a/src/_tutorial/chapter_2.rs b/src/_tutorial/chapter_2.rs index a4211cb6..d3461e75 100644 --- a/src/_tutorial/chapter_2.rs +++ b/src/_tutorial/chapter_2.rs @@ -8,13 +8,13 @@ //! single token, you can do: //! ```rust //! # use winnow::Parser; -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::stream::Stream; //! use winnow::error::ParserError; //! use winnow::error::ErrorKind; //! use winnow::error::ErrMode; //! -//! fn parse_prefix(input: &mut &str) -> PResult { +//! fn parse_prefix(input: &mut &str) -> ModalResult { //! let c = input.next_token().ok_or_else(|| { //! ErrMode::from_error_kind(input, ErrorKind::Token) //! })?; @@ -38,14 +38,14 @@ //! //! This extraction of a token is encapsulated in the [`any`] parser: //! ```rust -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! # use winnow::error::ParserError; //! # use winnow::error::ErrorKind; //! # use winnow::error::ErrMode; //! use winnow::Parser; //! use winnow::token::any; //! -//! fn parse_prefix(input: &mut &str) -> PResult { +//! fn parse_prefix(input: &mut &str) -> ModalResult { //! let c = any //! .parse_next(input)?; //! if c != '0' { @@ -69,11 +69,11 @@ //! Using the higher level [`any`] parser opens `parse_prefix` to the helpers on the [`Parser`] trait, //! like [`Parser::verify`] which fails a parse if a condition isn't met, like our check above: //! ```rust -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::Parser; //! use winnow::token::any; //! -//! fn parse_prefix(input: &mut &str) -> PResult { +//! fn parse_prefix(input: &mut &str) -> ModalResult { //! let c = any //! .verify(|c| *c == '0') //! .parse_next(input)?; @@ -95,10 +95,10 @@ //! Matching a single token literal is common enough that [`Parser`] is implemented for //! the `char` type, encapsulating both [`any`] and [`Parser::verify`]: //! ```rust -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::Parser; //! -//! fn parse_prefix(input: &mut &str) -> PResult { +//! fn parse_prefix(input: &mut &str) -> ModalResult { //! let c = '0'.parse_next(input)?; //! Ok(c) //! } @@ -120,13 +120,13 @@ //! [`Stream`] also supports processing slices of tokens: //! ```rust //! # use winnow::Parser; -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::stream::Stream; //! use winnow::error::ParserError; //! use winnow::error::ErrorKind; //! use winnow::error::ErrMode; //! -//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! let expected = "0x"; //! if input.len() < expected.len() { //! return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); @@ -151,11 +151,11 @@ //! //! Matching the input position against a string literal is encapsulated in the [`literal`] parser: //! ```rust -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! # use winnow::Parser; //! use winnow::token::literal; //! -//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! let expected = "0x"; //! let actual = literal(expected).parse_next(input)?; //! Ok(actual) @@ -174,10 +174,10 @@ //! //! Like for a single token, matching a string literal is common enough that [`Parser`] is implemented for the `&str` type: //! ```rust -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::Parser; //! -//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! let actual = "0x".parse_next(input)?; //! Ok(actual) //! } @@ -202,10 +202,10 @@ //! //! ```rust //! # use winnow::Parser; -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::token::one_of; //! -//! fn parse_digits(input: &mut &str) -> PResult { +//! fn parse_digits(input: &mut &str) -> ModalResult { //! one_of(('0'..='9', 'a'..='f', 'A'..='F')).parse_next(input) //! } //! @@ -235,7 +235,7 @@ //! > If you have not programmed in a language where functions are values, the type signature of the //! > [`one_of`] function might be a surprise. //! > The function [`one_of`] *returns a function*. The function it returns is a -//! > `Parser`, taking a `&str` and returning an `PResult`. This is a common pattern in winnow for +//! > `Parser`, taking a `&str` and returning an `ModalResult`. This is a common pattern in winnow for //! > configurable or stateful parsers. //! //! Some of character classes are common enough that a named parser is provided, like with: @@ -246,10 +246,10 @@ //! You can then capture sequences of these characters with parsers like [`take_while`]. //! ```rust //! # use winnow::Parser; -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::token::take_while; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! take_while(1.., ('0'..='9', 'a'..='f', 'A'..='F')).parse_next(input) //! } //! @@ -267,10 +267,10 @@ //! We could simplify this further by using one of the built-in character classes, [`hex_digit1`]: //! ```rust //! # use winnow::Parser; -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! use winnow::ascii::hex_digit1; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! hex_digit1.parse_next(input) //! } //! diff --git a/src/_tutorial/chapter_3.rs b/src/_tutorial/chapter_3.rs index a24f409f..8d445448 100644 --- a/src/_tutorial/chapter_3.rs +++ b/src/_tutorial/chapter_3.rs @@ -13,11 +13,11 @@ //! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # -//! fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! "0x".parse_next(input) //! } //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! take_while(1.., ( //! ('0'..='9'), //! ('A'..='F'), @@ -42,11 +42,11 @@ //! # use winnow::prelude::*; //! # use winnow::token::take_while; //! # -//! # fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # "0x".parse_next(input) //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -77,11 +77,11 @@ //! # use winnow::token::take_while; //! use winnow::combinator::preceded; //! -//! # fn parse_prefix<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # "0x".parse_next(input) //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -118,7 +118,7 @@ //! # use winnow::token::take_while; //! use winnow::stream::Stream; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! let start = input.checkpoint(); //! if let Ok(output) = ("0b", parse_bin_digits).parse_next(input) { //! return Ok(output); @@ -139,25 +139,25 @@ //! } //! //! // ... -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -192,7 +192,7 @@ //! # use winnow::token::take_while; //! use winnow::combinator::opt; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! if let Some(output) = opt(("0b", parse_bin_digits)).parse_next(input)? { //! Ok(output) //! } else if let Some(output) = opt(("0o", parse_oct_digits)).parse_next(input)? { @@ -204,25 +204,25 @@ //! } //! } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -249,7 +249,7 @@ //! # use winnow::token::take_while; //! use winnow::combinator::alt; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! alt(( //! ("0b", parse_bin_digits), //! ("0o", parse_oct_digits), @@ -258,25 +258,25 @@ //! )).parse_next(input) //! } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -314,7 +314,7 @@ //! use winnow::token::take; //! use winnow::combinator::fail; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! dispatch!(take(2usize); //! "0b" => parse_bin_digits, //! "0o" => parse_oct_digits, @@ -324,25 +324,25 @@ //! ).parse_next(input) //! } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), diff --git a/src/_tutorial/chapter_4.rs b/src/_tutorial/chapter_4.rs index 1f4d352a..061896d1 100644 --- a/src/_tutorial/chapter_4.rs +++ b/src/_tutorial/chapter_4.rs @@ -1,15 +1,15 @@ //! # Chapter 4: Parsers With Custom Return Types //! //! So far, we have seen mostly functions that take an `&str`, and return a -//! `PResult<&str>`. Splitting strings into smaller strings and characters is certainly +//! `ModalResult<&str>`. Splitting strings into smaller strings and characters is certainly //! useful, but it's not the only thing winnow is capable of! //! //! A useful operation when parsing is to convert between types; for example //! parsing from `&str` to another primitive, like [`usize`]. //! //! All we need to do for our parser to return a different type is to change -//! the type parameter of [`PResult`] to the desired return type. -//! For example, to return a `usize`, return a `PResult`. +//! the type parameter of [`ModalResult`] to the desired return type. +//! For example, to return a `usize`, return a `ModalResult`. //! //! One winnow-native way of doing a type conversion is to use the //! [`Parser::parse_to`] combinator @@ -20,7 +20,7 @@ //! # use winnow::prelude::*; //! # use winnow::ascii::digit1; //! # -//! fn parse_digits(input: &mut &str) -> PResult { +//! fn parse_digits(input: &mut &str) -> ModalResult { //! digit1 //! .parse_to() //! .parse_next(input) @@ -46,7 +46,7 @@ //! use winnow::token::take; //! use winnow::combinator::fail; //! -//! fn parse_digits(input: &mut &str) -> PResult { +//! fn parse_digits(input: &mut &str) -> ModalResult { //! dispatch!(take(2usize); //! "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -57,25 +57,25 @@ //! } //! //! // ... -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -98,7 +98,7 @@ //! See also [`Parser`] for more output-modifying parsers. #![allow(unused_imports)] -use crate::PResult; +use crate::ModalResult; use crate::Parser; use std::str::FromStr; diff --git a/src/_tutorial/chapter_5.rs b/src/_tutorial/chapter_5.rs index f095e821..eaa3c18e 100644 --- a/src/_tutorial/chapter_5.rs +++ b/src/_tutorial/chapter_5.rs @@ -14,7 +14,7 @@ //! use winnow::combinator::repeat; //! use winnow::combinator::terminated; //! -//! fn parse_list(input: &mut &str) -> PResult> { +//! fn parse_list(input: &mut &str) -> ModalResult> { //! let mut list = Vec::new(); //! while let Some(output) = opt(terminated(parse_digits, opt(','))).parse_next(input)? { //! list.push(output); @@ -23,7 +23,7 @@ //! } //! //! // ... -//! # fn parse_digits(input: &mut &str) -> PResult { +//! # fn parse_digits(input: &mut &str) -> ModalResult { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -33,25 +33,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -82,13 +82,13 @@ //! use winnow::combinator::repeat; //! use winnow::combinator::terminated; //! -//! fn parse_list(input: &mut &str) -> PResult> { +//! fn parse_list(input: &mut &str) -> ModalResult> { //! repeat(0.., //! terminated(parse_digits, opt(',')) //! ).parse_next(input) //! } //! # -//! # fn parse_digits(input: &mut &str) -> PResult { +//! # fn parse_digits(input: &mut &str) -> ModalResult { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -98,25 +98,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -146,12 +146,12 @@ //! # use winnow::combinator::fail; //! use winnow::combinator::separated; //! -//! fn parse_list(input: &mut &str) -> PResult> { +//! fn parse_list(input: &mut &str) -> ModalResult> { //! separated(0.., parse_digits, ",").parse_next(input) //! } //! //! // ... -//! # fn parse_digits(input: &mut &str) -> PResult { +//! # fn parse_digits(input: &mut &str) -> ModalResult { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -161,25 +161,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -223,15 +223,15 @@ //! # use winnow::combinator::fail; //! # use winnow::combinator::separated; //! # -//! fn take_list<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! fn take_list<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! parse_list.take().parse_next(input) //! } //! -//! fn parse_list(input: &mut &str) -> PResult<()> { +//! fn parse_list(input: &mut &str) -> ModalResult<()> { //! separated(0.., parse_digits, ",").parse_next(input) //! } //! -//! # fn parse_digits(input: &mut &str) -> PResult { +//! # fn parse_digits(input: &mut &str) -> ModalResult { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -241,25 +241,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), diff --git a/src/_tutorial/chapter_6.rs b/src/_tutorial/chapter_6.rs index 6ab1bf58..4fa8de24 100644 --- a/src/_tutorial/chapter_6.rs +++ b/src/_tutorial/chapter_6.rs @@ -8,15 +8,15 @@ //! # use winnow::error::ContextError; //! # use winnow::error::ErrMode; //! # use winnow::Parser; -//! use winnow::PResult; +//! use winnow::ModalResult; //! -//! pub fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! pub fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! // ... //! # Ok("") //! } //! ``` //! 1. We have to decide what to do about the "remainder" of the `input`. -//! 2. The [`PResult`] is not compatible with the rest of the Rust ecosystem. +//! 2. The [`ModalResult`] is not compatible with the rest of the Rust ecosystem. //! Normally, Rust applications want errors that are `std::error::Error + Send + Sync + 'static` //! meaning: //! - They implement the [`std::error::Error`] trait @@ -26,7 +26,7 @@ //! //! winnow provides [`Parser::parse`] to help with this: //! - Ensures we hit [`eof`] -//! - Converts from [`PResult`] to [`Result`] +//! - Converts from [`ModalResult`] to [`Result`] //! - Wraps the error in [`ParseError`] //! - For simple cases, [`ParseError`] provides a [`std::fmt::Display`] impl to render the error. //! - For more involved cases, [`ParseError`] provides the original [`input`][ParseError::input] and the @@ -59,7 +59,7 @@ //! } //! //! // ... -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -69,25 +69,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -112,7 +112,7 @@ use super::chapter_7; use crate::combinator::eof; use crate::error::ErrMode; use crate::error::ParseError; -use crate::PResult; +use crate::ModalResult; use crate::Parser; pub use super::chapter_5 as previous; diff --git a/src/_tutorial/chapter_7.rs b/src/_tutorial/chapter_7.rs index 58ccc4d2..8f16f854 100644 --- a/src/_tutorial/chapter_7.rs +++ b/src/_tutorial/chapter_7.rs @@ -35,7 +35,7 @@ //! # //! // ... //! -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! # alt(( //! # ("0b", parse_bin_digits), //! # ("0o", parse_oct_digits), @@ -44,25 +44,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -79,8 +79,8 @@ //! } //! ``` //! -//! Back in [`chapter_1`], we glossed over the `Err` variant of [`PResult`]. `PResult` is -//! actually short for `PResult` where [`ContextError`] is a relatively cheap +//! Back in [`chapter_1`], we glossed over the `Err` variant of [`ModalResult`]. `ModalResult` is +//! actually short for `ModalResult` where [`ContextError`] is a relatively cheap //! way of building up reasonable errors for humans. //! //! You can use [`Parser::context`] to annotate the error with custom types @@ -117,7 +117,7 @@ //! # } //! # } //! # -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! alt(( //! ("0b", parse_bin_digits) //! .context(StrContext::Label("digit")) @@ -137,25 +137,25 @@ //! // ... //! //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -208,7 +208,7 @@ //! # } //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! # alt(( //! # ("0b", parse_bin_digits) //! # .context(StrContext::Label("digit")) @@ -225,25 +225,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -293,7 +293,7 @@ //! # } //! # } //! # -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! alt(( //! ("0b", parse_bin_digits) //! .context(StrContext::Label("digit")) @@ -319,25 +319,25 @@ //! // ... //! //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -391,7 +391,7 @@ //! # } //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! # alt(( //! # ("0b", parse_bin_digits) //! # .context(StrContext::Label("digit")) @@ -414,25 +414,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -450,13 +450,13 @@ //! } //! ``` //! -//! Let's break down `PResult` one step further: +//! Let's break down `ModalResult` one step further: //! ```rust //! # use winnow::error::ErrorKind; //! # use winnow::error::ErrMode; -//! pub type PResult = Result>; +//! pub type ModalResult = Result>; //! ``` -//! [`PResult`] is just a fancy wrapper around `Result` that wraps our error in an [`ErrMode`] +//! [`ModalResult`] is just a fancy wrapper around `Result` that wraps our error in an [`ErrMode`] //! type. //! //! [`ErrMode`] is an enum with [`Backtrack`] and [`Cut`] variants (ignore [`Incomplete`] as its only @@ -498,7 +498,7 @@ //! # } //! # } //! # -//! fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! alt(( //! ("0b", cut_err(parse_bin_digits)) //! .context(StrContext::Label("digit")) @@ -524,25 +524,25 @@ //! // ... //! //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -648,7 +648,7 @@ //! //! impl std::error::Error for HexError {} //! -//! # fn parse_digits<'s>(input: &mut &'s str) -> PResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { //! # alt(( //! # ("0b", cut_err(parse_bin_digits)) //! # .context(StrContext::Label("digit")) @@ -671,25 +671,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -719,7 +719,7 @@ use crate::error::ContextError; use crate::error::ErrMode; use crate::error::ErrMode::*; use crate::error::ErrorKind; -use crate::PResult; +use crate::ModalResult; use crate::Parser; use crate::_topic; diff --git a/src/_tutorial/chapter_8.rs b/src/_tutorial/chapter_8.rs index 719e0d5b..28daf820 100644 --- a/src/_tutorial/chapter_8.rs +++ b/src/_tutorial/chapter_8.rs @@ -9,11 +9,11 @@ //! You can extend your own parsers to show up by wrapping their body with //! [`trace`][crate::combinator::trace]. Going back to [`do_nothing_parser`][super::chapter_1]. //! ```rust -//! # use winnow::PResult; +//! # use winnow::ModalResult; //! # use winnow::Parser; //! use winnow::combinator::trace; //! -//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! trace( //! "do_nothing_parser", //! |i: &mut _| Ok("") diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index 330bc580..dc67581b 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -23,7 +23,7 @@ use crate::token::any; use crate::token::one_of; use crate::token::take_until; use crate::token::take_while; -use crate::PResult; +use crate::ModalResult; use crate::Parser; /// Mark a value as case-insensitive for ASCII characters @@ -34,7 +34,7 @@ use crate::Parser; /// # use winnow::{error::ErrMode, error::{ErrorKind}}; /// # use winnow::ascii::Caseless; /// -/// fn parser<'s>(s: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(s: &mut &'s str) -> ModalResult<&'s str> { /// Caseless("hello").parse_next(s) /// } /// @@ -66,7 +66,7 @@ impl Caseless<&str> { /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn crlf<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn crlf<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::crlf.parse_next(input) /// # } @@ -77,7 +77,7 @@ impl Caseless<&str> { /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::crlf; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// crlf.parse_next(input) /// } /// @@ -96,7 +96,7 @@ impl Caseless<&str> { /// assert_eq!(crlf::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn crlf(input: &mut Input) -> PResult<::Slice, Error> +pub fn crlf(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream + Compare<&'static str>, Error: ParserError, @@ -115,7 +115,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn till_line_ending<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn till_line_ending<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::till_line_ending.parse_next(input) /// # } @@ -126,7 +126,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::till_line_ending; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// till_line_ending.parse_next(input) /// } /// @@ -150,7 +150,9 @@ where /// assert!(till_line_ending::<_, ContextError>.parse_peek(Partial::new("a\rbc")).is_err()); /// ``` #[inline(always)] -pub fn till_line_ending(input: &mut Input) -> PResult<::Slice, Error> +pub fn till_line_ending( + input: &mut Input, +) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream + Compare<&'static str> + FindSlice<(char, char)>, ::Token: AsChar + Clone, @@ -168,7 +170,7 @@ where fn till_line_ending_, const PARTIAL: bool>( input: &mut I, -) -> PResult<::Slice, E> +) -> ModalResult<::Slice, E> where I: StreamIsPartial, I: Stream, @@ -210,7 +212,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn line_ending<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn line_ending<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::line_ending.parse_next(input) /// # } @@ -221,7 +223,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::line_ending; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// line_ending.parse_next(input) /// } /// @@ -240,7 +242,7 @@ where /// assert_eq!(line_ending::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn line_ending(input: &mut Input) -> PResult<::Slice, Error> +pub fn line_ending(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream + Compare<&'static str>, Error: ParserError, @@ -259,7 +261,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn newline(input: &mut &str) -> PResult +/// pub fn newline(input: &mut &str) -> ModalResult /// # { /// # winnow::ascii::newline.parse_next(input) /// # } @@ -270,7 +272,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::newline; -/// fn parser<'s>(input: &mut &'s str) -> PResult { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult { /// newline.parse_next(input) /// } /// @@ -289,7 +291,7 @@ where /// assert_eq!(newline::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn newline>(input: &mut I) -> PResult +pub fn newline>(input: &mut I) -> ModalResult where I: StreamIsPartial, I: Stream, @@ -309,7 +311,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn tab(input: &mut &str) -> PResult +/// pub fn tab(input: &mut &str) -> ModalResult /// # { /// # winnow::ascii::tab.parse_next(input) /// # } @@ -320,7 +322,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::tab; -/// fn parser<'s>(input: &mut &'s str) -> PResult { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult { /// tab.parse_next(input) /// } /// @@ -339,7 +341,7 @@ where /// assert_eq!(tab::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn tab(input: &mut Input) -> PResult +pub fn tab(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream + Compare, Error: ParserError, @@ -360,7 +362,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn alpha0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn alpha0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::alpha0.parse_next(input) /// # } @@ -371,7 +373,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::alpha0; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// alpha0.parse_next(input) /// } /// @@ -390,7 +392,7 @@ where /// assert_eq!(alpha0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alpha0(input: &mut Input) -> PResult<::Slice, Error> +pub fn alpha0(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -412,7 +414,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn alpha1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn alpha1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::alpha1.parse_next(input) /// # } @@ -423,7 +425,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::alpha1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// alpha1.parse_next(input) /// } /// @@ -442,7 +444,7 @@ where /// assert_eq!(alpha1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alpha1(input: &mut Input) -> PResult<::Slice, Error> +pub fn alpha1(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -464,7 +466,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn digit0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn digit0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::digit0.parse_next(input) /// # } @@ -475,7 +477,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::digit0; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// digit0.parse_next(input) /// } /// @@ -495,7 +497,7 @@ where /// assert_eq!(digit0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn digit0(input: &mut Input) -> PResult<::Slice, Error> +pub fn digit0(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -517,7 +519,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn digit1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn digit1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::digit1.parse_next(input) /// # } @@ -528,7 +530,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::digit1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// digit1.parse_next(input) /// } /// @@ -554,7 +556,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::digit1; -/// fn parser<'s>(input: &mut &'s str) -> PResult { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult { /// digit1.try_map(str::parse).parse_next(input) /// } /// @@ -563,7 +565,7 @@ where /// assert!(parser.parse_peek("b").is_err()); /// ``` #[inline(always)] -pub fn digit1(input: &mut Input) -> PResult<::Slice, Error> +pub fn digit1(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -585,7 +587,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn hex_digit0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn hex_digit0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::hex_digit0.parse_next(input) /// # } @@ -596,7 +598,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::hex_digit0; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// hex_digit0.parse_next(input) /// } /// @@ -615,7 +617,7 @@ where /// assert_eq!(hex_digit0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn hex_digit0(input: &mut Input) -> PResult<::Slice, Error> +pub fn hex_digit0(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -638,7 +640,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn hex_digit1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn hex_digit1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::hex_digit1.parse_next(input) /// # } @@ -649,7 +651,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::hex_digit1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// hex_digit1.parse_next(input) /// } /// @@ -668,7 +670,7 @@ where /// assert_eq!(hex_digit1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn hex_digit1(input: &mut Input) -> PResult<::Slice, Error> +pub fn hex_digit1(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -690,7 +692,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn oct_digit0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn oct_digit0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::oct_digit0.parse_next(input) /// # } @@ -701,7 +703,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::oct_digit0; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// oct_digit0.parse_next(input) /// } /// @@ -720,7 +722,7 @@ where /// assert_eq!(oct_digit0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn oct_digit0(input: &mut Input) -> PResult<::Slice, Error> +pub fn oct_digit0(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial, Input: Stream, @@ -743,7 +745,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn oct_digit1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn oct_digit1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::oct_digit1.parse_next(input) /// # } @@ -754,7 +756,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::oct_digit1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// oct_digit1.parse_next(input) /// } /// @@ -773,7 +775,7 @@ where /// assert_eq!(oct_digit1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn oct_digit1(input: &mut Input) -> PResult<::Slice, Error> +pub fn oct_digit1(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -795,7 +797,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn alphanumeric0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn alphanumeric0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::alphanumeric0.parse_next(input) /// # } @@ -806,7 +808,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::alphanumeric0; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// alphanumeric0.parse_next(input) /// } /// @@ -825,7 +827,9 @@ where /// assert_eq!(alphanumeric0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alphanumeric0(input: &mut Input) -> PResult<::Slice, Error> +pub fn alphanumeric0( + input: &mut Input, +) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -847,7 +851,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn alphanumeric1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn alphanumeric1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::alphanumeric1.parse_next(input) /// # } @@ -858,7 +862,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::alphanumeric1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// alphanumeric1.parse_next(input) /// } /// @@ -877,7 +881,9 @@ where /// assert_eq!(alphanumeric1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alphanumeric1(input: &mut Input) -> PResult<::Slice, Error> +pub fn alphanumeric1( + input: &mut Input, +) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -899,7 +905,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn space0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn space0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::space0.parse_next(input) /// # } @@ -917,7 +923,7 @@ where /// assert_eq!(space0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn space0(input: &mut Input) -> PResult<::Slice, Error> +pub fn space0(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -939,7 +945,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn space1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn space1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::space1.parse_next(input) /// # } @@ -950,7 +956,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::space1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// space1.parse_next(input) /// } /// @@ -969,7 +975,7 @@ where /// assert_eq!(space1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn space1(input: &mut Input) -> PResult<::Slice, Error> +pub fn space1(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -991,7 +997,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn multispace0<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn multispace0<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::multispace0.parse_next(input) /// # } @@ -1002,7 +1008,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::multispace0; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// multispace0.parse_next(input) /// } /// @@ -1021,7 +1027,7 @@ where /// assert_eq!(multispace0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn multispace0(input: &mut Input) -> PResult<::Slice, Error> +pub fn multispace0(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar + Clone, @@ -1043,7 +1049,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn multispace1<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn multispace1<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::ascii::multispace1.parse_next(input) /// # } @@ -1054,7 +1060,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::ascii::multispace1; -/// fn parser<'s>(input: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { /// multispace1.parse_next(input) /// } /// @@ -1073,7 +1079,7 @@ where /// assert_eq!(multispace1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn multispace1(input: &mut Input) -> PResult<::Slice, Error> +pub fn multispace1(input: &mut Input) -> ModalResult<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar + Clone, @@ -1093,7 +1099,7 @@ where /// Assuming you are parsing a `&str` [Stream] into a `u32`: /// ```rust /// # use winnow::prelude::*;; -/// pub fn dec_uint(input: &mut &str) -> PResult +/// pub fn dec_uint(input: &mut &str) -> ModalResult /// # { /// # winnow::ascii::dec_uint.parse_next(input) /// # } @@ -1103,7 +1109,7 @@ where #[doc(alias = "u32")] #[doc(alias = "u64")] #[doc(alias = "u128")] -pub fn dec_uint(input: &mut Input) -> PResult +pub fn dec_uint(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, ::Slice: AsBStr, @@ -1178,7 +1184,7 @@ impl Uint for usize { /// Assuming you are parsing a `&str` [Stream] into an `i32`: /// ```rust /// # use winnow::prelude::*;; -/// pub fn dec_int(input: &mut &str) -> PResult +/// pub fn dec_int(input: &mut &str) -> ModalResult /// # { /// # winnow::ascii::dec_int.parse_next(input) /// # } @@ -1188,7 +1194,7 @@ impl Uint for usize { #[doc(alias = "i32")] #[doc(alias = "i64")] #[doc(alias = "i128")] -pub fn dec_int(input: &mut Input) -> PResult +pub fn dec_int(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, ::Slice: AsBStr, @@ -1270,7 +1276,7 @@ impl Int for isize { /// Assuming you are parsing a `&str` [Stream] into a `u32`: /// ```rust /// # use winnow::prelude::*;; -/// pub fn hex_uint(input: &mut &str) -> PResult +/// pub fn hex_uint(input: &mut &str) -> ModalResult /// # { /// # winnow::ascii::hex_uint.parse_next(input) /// # } @@ -1282,7 +1288,7 @@ impl Int for isize { /// # use winnow::prelude::*; /// use winnow::ascii::hex_uint; /// -/// fn parser<'s>(s: &mut &'s [u8]) -> PResult { +/// fn parser<'s>(s: &mut &'s [u8]) -> ModalResult { /// hex_uint(s) /// } /// @@ -1297,7 +1303,7 @@ impl Int for isize { /// # use winnow::Partial; /// use winnow::ascii::hex_uint; /// -/// fn parser<'s>(s: &mut Partial<&'s [u8]>) -> PResult { +/// fn parser<'s>(s: &mut Partial<&'s [u8]>) -> ModalResult { /// hex_uint(s) /// } /// @@ -1306,7 +1312,7 @@ impl Int for isize { /// assert!(parser.parse_peek(Partial::new(&b"ggg"[..])).is_err()); /// ``` #[inline] -pub fn hex_uint(input: &mut Input) -> PResult +pub fn hex_uint(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -1417,7 +1423,7 @@ impl HexUint for u128 { /// Assuming you are parsing a `&str` [Stream] into an `f64`: /// ```rust /// # use winnow::prelude::*;; -/// pub fn float(input: &mut &str) -> PResult +/// pub fn float(input: &mut &str) -> ModalResult /// # { /// # winnow::ascii::float.parse_next(input) /// # } @@ -1430,7 +1436,7 @@ impl HexUint for u128 { /// # use winnow::error::Needed::Size; /// use winnow::ascii::float; /// -/// fn parser<'s>(s: &mut &'s str) -> PResult { +/// fn parser<'s>(s: &mut &'s str) -> ModalResult { /// float(s) /// } /// @@ -1447,7 +1453,7 @@ impl HexUint for u128 { /// # use winnow::Partial; /// use winnow::ascii::float; /// -/// fn parser<'s>(s: &mut Partial<&'s str>) -> PResult { +/// fn parser<'s>(s: &mut Partial<&'s str>) -> ModalResult { /// float(s) /// } /// @@ -1461,7 +1467,7 @@ impl HexUint for u128 { #[doc(alias = "f32")] #[doc(alias = "double")] #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -pub fn float(input: &mut Input) -> PResult +pub fn float(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream + Compare> + Compare + AsBStr, ::Slice: ParseSlice, @@ -1478,7 +1484,9 @@ where } #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -fn take_float_or_exceptions>(input: &mut I) -> PResult<::Slice, E> +fn take_float_or_exceptions>( + input: &mut I, +) -> ModalResult<::Slice, E> where I: StreamIsPartial, I: Stream, @@ -1498,7 +1506,7 @@ where } #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -fn take_unsigned_float_or_exceptions>(input: &mut I) -> PResult<(), E> +fn take_unsigned_float_or_exceptions>(input: &mut I) -> ModalResult<(), E> where I: StreamIsPartial, I: Stream, @@ -1517,7 +1525,7 @@ where } #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -fn take_exp>(input: &mut I) -> PResult<(), E> +fn take_exp>(input: &mut I) -> ModalResult<(), E> where I: StreamIsPartial, I: Stream, @@ -1566,7 +1574,7 @@ where /// use winnow::ascii::take_escaped; /// use winnow::token::one_of; /// -/// fn esc<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn esc<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// take_escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_next(input) /// } /// @@ -1583,7 +1591,7 @@ where /// use winnow::ascii::take_escaped; /// use winnow::token::one_of; /// -/// fn esc<'i>(input: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn esc<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// take_escaped(digit1, '\\', one_of(['"', 'n', '\\'])).parse_next(input) /// } /// @@ -1643,7 +1651,7 @@ fn escaped_internal( normal: &mut F, control_char: char, escapable: &mut G, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -1719,7 +1727,7 @@ where /// use winnow::ascii::alpha1; /// use winnow::combinator::alt; /// -/// fn parser<'s>(input: &mut &'s str) -> PResult { +/// fn parser<'s>(input: &mut &'s str) -> ModalResult { /// escaped_transform( /// alpha1, /// '\\', @@ -1747,7 +1755,7 @@ where /// use winnow::ascii::alpha1; /// use winnow::combinator::alt; /// -/// fn parser<'s>(input: &mut Partial<&'s str>) -> PResult { +/// fn parser<'s>(input: &mut Partial<&'s str>) -> ModalResult { /// escaped_transform( /// alpha1, /// '\\', @@ -1799,7 +1807,7 @@ fn escaped_transform_internal( normal: &mut F, control_char: char, transform: &mut G, -) -> PResult +) -> ModalResult where I: StreamIsPartial, I: Stream, diff --git a/src/ascii/tests.rs b/src/ascii/tests.rs index 8529bd20..9a1d9002 100644 --- a/src/ascii/tests.rs +++ b/src/ascii/tests.rs @@ -2082,7 +2082,7 @@ Err( } #[cfg(feature = "std")] - fn parse_f64(i: &mut &str) -> PResult { + fn parse_f64(i: &mut &str) -> ModalResult { match take_float_or_exceptions.parse_next(i) { Err(e) => Err(e), Ok(s) => { diff --git a/src/binary/bits/mod.rs b/src/binary/bits/mod.rs index 8d0d6d7b..7965d7ce 100644 --- a/src/binary/bits/mod.rs +++ b/src/binary/bits/mod.rs @@ -8,7 +8,7 @@ use crate::combinator::trace; use crate::error::{ErrMode, ErrorConvert, ErrorKind, Needed, ParserError}; use crate::lib::std::ops::{AddAssign, Div, Shl, Shr}; use crate::stream::{Stream, StreamIsPartial, ToUsize}; -use crate::{PResult, Parser}; +use crate::{ModalResult, Parser}; /// Number of bits in a byte const BYTE: usize = u8::BITS as usize; @@ -29,7 +29,7 @@ const BYTE: usize = u8::BITS as usize; /// Bytes::new(b) /// } /// -/// fn parse(input: &mut Stream<'_>) -> PResult<(u8, u8)> { +/// fn parse(input: &mut Stream<'_>) -> ModalResult<(u8, u8)> { /// bits::<_, _, ContextError, _, _>((take(4usize), take(8usize))).parse_next(input) /// } /// @@ -98,7 +98,7 @@ where /// Bytes::new(b) /// } /// -/// fn parse<'i>(input: &mut Stream<'i>) -> PResult<(u8, u8, &'i [u8])> { +/// fn parse<'i>(input: &mut Stream<'i>) -> ModalResult<(u8, u8, &'i [u8])> { /// bits::<_, _, ContextError, _, _>(( /// take(4usize), /// take(8usize), @@ -204,7 +204,7 @@ where fn take_, const PARTIAL: bool>( bit_input: &mut (I, usize), count: usize, -) -> PResult +) -> ModalResult where I: StreamIsPartial, I: Stream + Clone, @@ -288,7 +288,7 @@ where /// /// Compare the lowest `count` bits of `input` against the lowest `count` bits of `pattern`. /// /// Return Ok and the matching section of `input` if there's a match. /// /// Return Err if there's no match. -/// fn parser(bits: u8, count: u8, input: &mut (Stream<'_>, usize)) -> PResult { +/// fn parser(bits: u8, count: u8, input: &mut (Stream<'_>, usize)) -> ModalResult { /// pattern(bits, count).parse_next(input) /// } /// @@ -350,7 +350,7 @@ where /// ```rust /// # use winnow::prelude::*;; /// # use winnow::error::ContextError; -/// pub fn bool(input: &mut (&[u8], usize)) -> PResult +/// pub fn bool(input: &mut (&[u8], usize)) -> ModalResult /// # { /// # winnow::binary::bits::bool.parse_next(input) /// # } @@ -370,7 +370,7 @@ where /// Bytes::new(b) /// } /// -/// fn parse(input: &mut (Stream<'_>, usize)) -> PResult { +/// fn parse(input: &mut (Stream<'_>, usize)) -> ModalResult { /// bool.parse_next(input) /// } /// @@ -380,7 +380,7 @@ where #[doc(alias = "any")] pub fn bool>( input: &mut (Input, usize), -) -> PResult +) -> ModalResult where Input: Stream + StreamIsPartial + Clone, { diff --git a/src/binary/bits/tests.rs b/src/binary/bits/tests.rs index e7a459d2..fec9aa1d 100644 --- a/src/binary/bits/tests.rs +++ b/src/binary/bits/tests.rs @@ -10,7 +10,7 @@ fn test_complete_byte_consumption_bits() { // Take 3 bit slices with sizes [4, 8, 4]. #[allow(clippy::type_complexity)] - let result: PResult<(&[u8], (u8, u8, u8)), InputError<_>> = + let result: ModalResult<(&[u8], (u8, u8, u8)), InputError<_>> = bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize))) .parse_peek(input); @@ -34,7 +34,7 @@ fn test_partial_byte_consumption_bits() { let input = &[0x12, 0x34, 0x56, 0x78][..]; // Take bit slices with sizes [4, 8]. - let result: PResult<(&[u8], (u8, u8)), InputError<_>> = + let result: ModalResult<(&[u8], (u8, u8)), InputError<_>> = bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize))) .parse_peek(input); @@ -55,7 +55,7 @@ fn test_incomplete_bits() { let input = Partial::new(&[0x12][..]); // Take bit slices with sizes [4, 8]. - let result: PResult<(_, (u8, u8)), InputError<_>> = + let result: ModalResult<(_, (u8, u8)), InputError<_>> = bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input); assert!(result.is_err()); @@ -70,7 +70,7 @@ fn test_take_complete_0() { assert_eq!(count, 0usize); let offset = 0usize; - let result: PResult<((&[u8], usize), usize), InputError<_>> = + let result: ModalResult<((&[u8], usize), usize), InputError<_>> = take(count).parse_peek((input, offset)); assert_eq!(result, Ok(((input, offset), 0))); @@ -80,7 +80,7 @@ fn test_take_complete_0() { fn test_take_complete_eof() { let input = &[0b00010010][..]; - let result: PResult<((&[u8], usize), usize), InputError<_>> = + let result: ModalResult<((&[u8], usize), usize), InputError<_>> = take(1usize).parse_peek((input, 8)); assert_eq!( @@ -96,7 +96,7 @@ fn test_take_complete_eof() { fn test_take_complete_span_over_multiple_bytes() { let input = &[0b00010010, 0b00110100, 0b11111111, 0b11111111][..]; - let result: PResult<((&[u8], usize), usize), InputError<_>> = + let result: ModalResult<((&[u8], usize), usize), InputError<_>> = take(24usize).parse_peek((input, 4)); assert_eq!( @@ -112,7 +112,7 @@ fn test_take_partial_0() { assert_eq!(count, 0usize); let offset = 0usize; - let result: PResult<((_, usize), usize), InputError<_>> = + let result: ModalResult<((_, usize), usize), InputError<_>> = take(count).parse_peek((input, offset)); assert_eq!(result, Ok(((input, offset), 0))); @@ -125,7 +125,7 @@ fn test_pattern_partial_ok() { let bits_to_take = 4usize; let value_to_pattern = 0b0001; - let result: PResult<((_, usize), usize), InputError<_>> = + let result: ModalResult<((_, usize), usize), InputError<_>> = pattern(value_to_pattern, bits_to_take).parse_peek((input, offset)); assert_eq!(result, Ok(((input, bits_to_take), value_to_pattern))); @@ -138,7 +138,7 @@ fn test_pattern_partial_err() { let bits_to_take = 4usize; let value_to_pattern = 0b1111; - let result: PResult<((_, usize), usize), InputError<_>> = + let result: ModalResult<((_, usize), usize), InputError<_>> = pattern(value_to_pattern, bits_to_take).parse_peek((input, offset)); assert_eq!( @@ -154,7 +154,7 @@ fn test_pattern_partial_err() { fn test_bool_0_complete() { let input = [0b10000000].as_ref(); - let result: PResult<((&[u8], usize), bool), InputError<_>> = bool.parse_peek((input, 0)); + let result: ModalResult<((&[u8], usize), bool), InputError<_>> = bool.parse_peek((input, 0)); assert_eq!(result, Ok(((input, 1), true))); } @@ -163,7 +163,7 @@ fn test_bool_0_complete() { fn test_bool_eof_complete() { let input = [0b10000000].as_ref(); - let result: PResult<((&[u8], usize), bool), InputError<_>> = bool.parse_peek((input, 8)); + let result: ModalResult<((&[u8], usize), bool), InputError<_>> = bool.parse_peek((input, 8)); assert_eq!( result, @@ -179,7 +179,7 @@ fn test_bool_0_partial() { let input = Partial::new([0b10000000].as_ref()); #[allow(clippy::type_complexity)] - let result: PResult<((Partial<&[u8]>, usize), bool), InputError<_>> = + let result: ModalResult<((Partial<&[u8]>, usize), bool), InputError<_>> = bool.parse_peek((input, 0)); assert_eq!(result, Ok(((input, 1), true))); @@ -190,7 +190,7 @@ fn test_bool_eof_partial() { let input = Partial::new([0b10000000].as_ref()); #[allow(clippy::type_complexity)] - let result: PResult<((Partial<&[u8]>, usize), bool), InputError<_>> = + let result: ModalResult<((Partial<&[u8]>, usize), bool), InputError<_>> = bool.parse_peek((input, 8)); assert_eq!( diff --git a/src/binary/mod.rs b/src/binary/mod.rs index 16113d80..d7152624 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -17,7 +17,7 @@ use crate::lib::std::ops::{Add, Shl}; use crate::stream::Accumulate; use crate::stream::{Stream, StreamIsPartial}; use crate::stream::{ToUsize, UpdateSlice}; -use crate::PResult; +use crate::ModalResult; use crate::Parser; /// Configurable endianness @@ -45,7 +45,7 @@ pub enum Endianness { /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u8; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_u8.parse_next(s) /// } /// @@ -59,7 +59,7 @@ pub enum Endianness { /// # use winnow::Partial; /// use winnow::binary::be_u8; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_u8.parse_next(s) /// } /// @@ -67,7 +67,7 @@ pub enum Endianness { /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_u8(input: &mut Input) -> PResult +pub fn be_u8(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -89,7 +89,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u16; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_u16.parse_next(s) /// } /// @@ -103,7 +103,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_u16; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_u16.parse_next(s) /// } /// @@ -111,7 +111,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_u16(input: &mut Input) -> PResult +pub fn be_u16(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -133,7 +133,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u24; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_u24.parse_next(s) /// } /// @@ -147,7 +147,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_u24; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_u24.parse_next(s) /// } /// @@ -155,7 +155,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn be_u24(input: &mut Input) -> PResult +pub fn be_u24(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -177,7 +177,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u32; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_u32.parse_next(s) /// } /// @@ -191,7 +191,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_u32; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_u32.parse_next(s) /// } /// @@ -199,7 +199,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_u32(input: &mut Input) -> PResult +pub fn be_u32(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -221,7 +221,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u64; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_u64.parse_next(s) /// } /// @@ -235,7 +235,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_u64; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_u64.parse_next(s) /// } /// @@ -243,7 +243,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_u64(input: &mut Input) -> PResult +pub fn be_u64(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -265,7 +265,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_u128; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_u128.parse_next(s) /// } /// @@ -279,7 +279,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_u128; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_u128.parse_next(s) /// } /// @@ -287,7 +287,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn be_u128(input: &mut Input) -> PResult +pub fn be_u128(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -296,7 +296,7 @@ where } #[inline] -fn be_uint(input: &mut Input, bound: usize) -> PResult +fn be_uint(input: &mut Input, bound: usize) -> ModalResult where Input: StreamIsPartial + Stream, Uint: Default + Shl + Add + From, @@ -347,7 +347,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i8; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_i8.parse_next(s) /// } /// @@ -361,7 +361,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_i8; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_i8.parse_next(s) /// } /// @@ -369,7 +369,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_i8(input: &mut Input) -> PResult +pub fn be_i8(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -391,7 +391,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i16; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_i16.parse_next(s) /// } /// @@ -405,7 +405,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_i16; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_i16.parse_next(s) /// } /// @@ -413,7 +413,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn be_i16(input: &mut Input) -> PResult +pub fn be_i16(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -438,7 +438,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i24; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_i24.parse_next(s) /// } /// @@ -452,7 +452,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_i24; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_i24.parse_next(s) /// } /// @@ -460,7 +460,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_i24(input: &mut Input) -> PResult +pub fn be_i24(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -493,7 +493,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i32; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_i32.parse_next(s) /// } /// @@ -507,7 +507,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_i32; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_i32.parse_next(s) /// } /// @@ -515,7 +515,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(4)))); /// ``` #[inline(always)] -pub fn be_i32(input: &mut Input) -> PResult +pub fn be_i32(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -540,7 +540,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i64; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_i64.parse_next(s) /// } /// @@ -554,7 +554,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_i64; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_i64.parse_next(s) /// } /// @@ -562,7 +562,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_i64(input: &mut Input) -> PResult +pub fn be_i64(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -587,7 +587,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_i128; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_i128.parse_next(s) /// } /// @@ -601,7 +601,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_i128; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_i128.parse_next(s) /// } /// @@ -609,7 +609,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn be_i128(input: &mut Input) -> PResult +pub fn be_i128(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -634,7 +634,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u8; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_u8.parse_next(s) /// } /// @@ -648,7 +648,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_u8; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_u8.parse_next(s) /// } /// @@ -656,7 +656,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_u8(input: &mut Input) -> PResult +pub fn le_u8(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -678,7 +678,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u16; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_u16.parse_next(s) /// } /// @@ -692,7 +692,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_u16; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_u16.parse_next(s) /// } /// @@ -700,7 +700,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_u16(input: &mut Input) -> PResult +pub fn le_u16(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -722,7 +722,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u24; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_u24.parse_next(s) /// } /// @@ -736,7 +736,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_u24; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_u24.parse_next(s) /// } /// @@ -744,7 +744,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn le_u24(input: &mut Input) -> PResult +pub fn le_u24(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -766,7 +766,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u32; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_u32.parse_next(s) /// } /// @@ -780,7 +780,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_u32; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_u32.parse_next(s) /// } /// @@ -788,7 +788,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_u32(input: &mut Input) -> PResult +pub fn le_u32(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -810,7 +810,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u64; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_u64.parse_next(s) /// } /// @@ -824,7 +824,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_u64; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_u64.parse_next(s) /// } /// @@ -832,7 +832,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_u64(input: &mut Input) -> PResult +pub fn le_u64(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -854,7 +854,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_u128; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_u128.parse_next(s) /// } /// @@ -868,7 +868,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_u128; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_u128.parse_next(s) /// } /// @@ -876,7 +876,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn le_u128(input: &mut Input) -> PResult +pub fn le_u128(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -885,7 +885,7 @@ where } #[inline] -fn le_uint(input: &mut Input, bound: usize) -> PResult +fn le_uint(input: &mut Input, bound: usize) -> ModalResult where Input: StreamIsPartial + Stream, Uint: Default + Shl + Add + From, @@ -935,7 +935,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i8; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_i8.parse_next(s) /// } /// @@ -949,7 +949,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_i8; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_i8.parse_next(s) /// } /// @@ -957,7 +957,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_i8(input: &mut Input) -> PResult +pub fn le_i8(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -979,7 +979,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i16; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_i16.parse_next(s) /// } /// @@ -993,7 +993,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_i16; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_i16.parse_next(s) /// } /// @@ -1001,7 +1001,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_i16(input: &mut Input) -> PResult +pub fn le_i16(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1026,7 +1026,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i24; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_i24.parse_next(s) /// } /// @@ -1040,7 +1040,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_i24; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_i24.parse_next(s) /// } /// @@ -1048,7 +1048,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn le_i24(input: &mut Input) -> PResult +pub fn le_i24(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1081,7 +1081,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i32; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_i32.parse_next(s) /// } /// @@ -1095,7 +1095,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_i32; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_i32.parse_next(s) /// } /// @@ -1103,7 +1103,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_i32(input: &mut Input) -> PResult +pub fn le_i32(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1128,7 +1128,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i64; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_i64.parse_next(s) /// } /// @@ -1142,7 +1142,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_i64; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_i64.parse_next(s) /// } /// @@ -1150,7 +1150,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_i64(input: &mut Input) -> PResult +pub fn le_i64(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1175,7 +1175,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_i128; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_i128.parse_next(s) /// } /// @@ -1189,7 +1189,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_i128; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_i128.parse_next(s) /// } /// @@ -1197,7 +1197,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn le_i128(input: &mut Input) -> PResult +pub fn le_i128(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1228,7 +1228,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::u8; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// u8.parse_next(s) /// } /// @@ -1243,7 +1243,7 @@ where /// # use winnow::Partial; /// use winnow::binary::u8; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// u8.parse_next(s) /// } /// @@ -1251,7 +1251,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn u8(input: &mut Input) -> PResult +pub fn u8(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1266,7 +1266,7 @@ where .parse_next(input) } -fn u8_(input: &mut Input) -> PResult +fn u8_(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1297,14 +1297,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::u16; /// -/// fn be_u16(input: &mut &[u8]) -> PResult { +/// fn be_u16(input: &mut &[u8]) -> ModalResult { /// u16(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u16.parse_peek(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); /// assert!(be_u16.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_u16(input: &mut &[u8]) -> PResult { +/// fn le_u16(input: &mut &[u8]) -> ModalResult { /// u16(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1319,14 +1319,14 @@ where /// # use winnow::Partial; /// use winnow::binary::u16; /// -/// fn be_u16(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_u16(input: &mut Partial<&[u8]>) -> ModalResult { /// u16(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u16.parse_peek(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003))); /// assert_eq!(be_u16.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// -/// fn le_u16(input: &mut Partial<&[u8]>) -> PResult< u16> { +/// fn le_u16(input: &mut Partial<&[u8]>) -> ModalResult< u16> { /// u16(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1368,14 +1368,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::u24; /// -/// fn be_u24(input: &mut &[u8]) -> PResult { +/// fn be_u24(input: &mut &[u8]) -> ModalResult { /// u24(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u24.parse_peek(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); /// assert!(be_u24.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_u24(input: &mut &[u8]) -> PResult { +/// fn le_u24(input: &mut &[u8]) -> ModalResult { /// u24(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1390,14 +1390,14 @@ where /// # use winnow::Partial; /// use winnow::binary::u24; /// -/// fn be_u24(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_u24(input: &mut Partial<&[u8]>) -> ModalResult { /// u24(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u24.parse_peek(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x000305))); /// assert_eq!(be_u24.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// -/// fn le_u24(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_u24(input: &mut Partial<&[u8]>) -> ModalResult { /// u24(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1439,14 +1439,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::u32; /// -/// fn be_u32(input: &mut &[u8]) -> PResult { +/// fn be_u32(input: &mut &[u8]) -> ModalResult { /// u32(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u32.parse_peek(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); /// assert!(be_u32.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_u32(input: &mut &[u8]) -> PResult { +/// fn le_u32(input: &mut &[u8]) -> ModalResult { /// u32(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1461,14 +1461,14 @@ where /// # use winnow::Partial; /// use winnow::binary::u32; /// -/// fn be_u32(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_u32(input: &mut Partial<&[u8]>) -> ModalResult { /// u32(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u32.parse_peek(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00030507))); /// assert_eq!(be_u32.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// -/// fn le_u32(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_u32(input: &mut Partial<&[u8]>) -> ModalResult { /// u32(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1510,14 +1510,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::u64; /// -/// fn be_u64(input: &mut &[u8]) -> PResult { +/// fn be_u64(input: &mut &[u8]) -> ModalResult { /// u64(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u64.parse_peek(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); /// assert!(be_u64.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_u64(input: &mut &[u8]) -> PResult { +/// fn le_u64(input: &mut &[u8]) -> ModalResult { /// u64(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1532,14 +1532,14 @@ where /// # use winnow::Partial; /// use winnow::binary::u64; /// -/// fn be_u64(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_u64(input: &mut Partial<&[u8]>) -> ModalResult { /// u64(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u64.parse_peek(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0001020304050607))); /// assert_eq!(be_u64.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// -/// fn le_u64(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_u64(input: &mut Partial<&[u8]>) -> ModalResult { /// u64(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1581,14 +1581,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::u128; /// -/// fn be_u128(input: &mut &[u8]) -> PResult { +/// fn be_u128(input: &mut &[u8]) -> ModalResult { /// u128(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u128.parse_peek(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); /// assert!(be_u128.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_u128(input: &mut &[u8]) -> PResult { +/// fn le_u128(input: &mut &[u8]) -> ModalResult { /// u128(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1603,14 +1603,14 @@ where /// # use winnow::Partial; /// use winnow::binary::u128; /// -/// fn be_u128(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_u128(input: &mut Partial<&[u8]>) -> ModalResult { /// u128(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_u128.parse_peek(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00010203040506070001020304050607))); /// assert_eq!(be_u128.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// -/// fn le_u128(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_u128(input: &mut Partial<&[u8]>) -> ModalResult { /// u128(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1655,7 +1655,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::i8; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// i8.parse_next(s) /// } /// @@ -1670,7 +1670,7 @@ where /// # use winnow::Partial; /// use winnow::binary::i8; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// i8.parse_next(s) /// } /// @@ -1678,7 +1678,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn i8(input: &mut Input) -> PResult +pub fn i8(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1711,14 +1711,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::i16; /// -/// fn be_i16(input: &mut &[u8]) -> PResult { +/// fn be_i16(input: &mut &[u8]) -> ModalResult { /// i16(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i16.parse_peek(&b"\x00\x03abcefg"[..]), Ok((&b"abcefg"[..], 0x0003))); /// assert!(be_i16.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_i16(input: &mut &[u8]) -> PResult { +/// fn le_i16(input: &mut &[u8]) -> ModalResult { /// i16(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1733,14 +1733,14 @@ where /// # use winnow::Partial; /// use winnow::binary::i16; /// -/// fn be_i16(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_i16(input: &mut Partial<&[u8]>) -> ModalResult { /// i16(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i16.parse_peek(Partial::new(&b"\x00\x03abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0003))); /// assert_eq!(be_i16.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// -/// fn le_i16(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_i16(input: &mut Partial<&[u8]>) -> ModalResult { /// i16(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1782,14 +1782,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::i24; /// -/// fn be_i24(input: &mut &[u8]) -> PResult { +/// fn be_i24(input: &mut &[u8]) -> ModalResult { /// i24(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i24.parse_peek(&b"\x00\x03\x05abcefg"[..]), Ok((&b"abcefg"[..], 0x000305))); /// assert!(be_i24.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_i24(input: &mut &[u8]) -> PResult { +/// fn le_i24(input: &mut &[u8]) -> ModalResult { /// i24(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1804,14 +1804,14 @@ where /// # use winnow::Partial; /// use winnow::binary::i24; /// -/// fn be_i24(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_i24(input: &mut Partial<&[u8]>) -> ModalResult { /// i24(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i24.parse_peek(Partial::new(&b"\x00\x03\x05abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x000305))); /// assert_eq!(be_i24.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// -/// fn le_i24(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_i24(input: &mut Partial<&[u8]>) -> ModalResult { /// i24(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1853,14 +1853,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::i32; /// -/// fn be_i32(input: &mut &[u8]) -> PResult { +/// fn be_i32(input: &mut &[u8]) -> ModalResult { /// i32(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i32.parse_peek(&b"\x00\x03\x05\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00030507))); /// assert!(be_i32.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_i32(input: &mut &[u8]) -> PResult { +/// fn le_i32(input: &mut &[u8]) -> ModalResult { /// i32(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1875,14 +1875,14 @@ where /// # use winnow::Partial; /// use winnow::binary::i32; /// -/// fn be_i32(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_i32(input: &mut Partial<&[u8]>) -> ModalResult { /// i32(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i32.parse_peek(Partial::new(&b"\x00\x03\x05\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00030507))); /// assert_eq!(be_i32.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// -/// fn le_i32(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_i32(input: &mut Partial<&[u8]>) -> ModalResult { /// i32(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1924,14 +1924,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::i64; /// -/// fn be_i64(input: &mut &[u8]) -> PResult { +/// fn be_i64(input: &mut &[u8]) -> ModalResult { /// i64(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i64.parse_peek(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x0001020304050607))); /// assert!(be_i64.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_i64(input: &mut &[u8]) -> PResult { +/// fn le_i64(input: &mut &[u8]) -> ModalResult { /// i64(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1946,14 +1946,14 @@ where /// # use winnow::Partial; /// use winnow::binary::i64; /// -/// fn be_i64(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_i64(input: &mut Partial<&[u8]>) -> ModalResult { /// i64(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i64.parse_peek(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x0001020304050607))); /// assert_eq!(be_i64.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// -/// fn le_i64(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_i64(input: &mut Partial<&[u8]>) -> ModalResult { /// i64(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -1995,14 +1995,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::i128; /// -/// fn be_i128(input: &mut &[u8]) -> PResult { +/// fn be_i128(input: &mut &[u8]) -> ModalResult { /// i128(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i128.parse_peek(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..]), Ok((&b"abcefg"[..], 0x00010203040506070001020304050607))); /// assert!(be_i128.parse_peek(&b"\x01"[..]).is_err()); /// -/// fn le_i128(input: &mut &[u8]) -> PResult { +/// fn le_i128(input: &mut &[u8]) -> ModalResult { /// i128(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -2017,14 +2017,14 @@ where /// # use winnow::Partial; /// use winnow::binary::i128; /// -/// fn be_i128(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_i128(input: &mut Partial<&[u8]>) -> ModalResult { /// i128(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_i128.parse_peek(Partial::new(&b"\x00\x01\x02\x03\x04\x05\x06\x07\x00\x01\x02\x03\x04\x05\x06\x07abcefg"[..])), Ok((Partial::new(&b"abcefg"[..]), 0x00010203040506070001020304050607))); /// assert_eq!(be_i128.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// -/// fn le_i128(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_i128(input: &mut Partial<&[u8]>) -> ModalResult { /// i128(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -2064,7 +2064,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_f32; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_f32.parse_next(s) /// } /// @@ -2078,7 +2078,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_f32; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_f32.parse_next(s) /// } /// @@ -2086,7 +2086,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_f32(input: &mut Input) -> PResult +pub fn be_f32(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2111,7 +2111,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::be_f64; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// be_f64.parse_next(s) /// } /// @@ -2125,7 +2125,7 @@ where /// # use winnow::Partial; /// use winnow::binary::be_f64; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// be_f64.parse_next(s) /// } /// @@ -2133,7 +2133,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_f64(input: &mut Input) -> PResult +pub fn be_f64(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2158,7 +2158,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_f32; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_f32.parse_next(s) /// } /// @@ -2172,7 +2172,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_f32; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_f32.parse_next(s) /// } /// @@ -2180,7 +2180,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_f32(input: &mut Input) -> PResult +pub fn le_f32(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2205,7 +2205,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::le_f64; /// -/// fn parser(s: &mut &[u8]) -> PResult { +/// fn parser(s: &mut &[u8]) -> ModalResult { /// le_f64.parse_next(s) /// } /// @@ -2219,7 +2219,7 @@ where /// # use winnow::Partial; /// use winnow::binary::le_f64; /// -/// fn parser(s: &mut Partial<&[u8]>) -> PResult { +/// fn parser(s: &mut Partial<&[u8]>) -> ModalResult { /// le_f64.parse_next(s) /// } /// @@ -2227,7 +2227,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_f64(input: &mut Input) -> PResult +pub fn le_f64(input: &mut Input) -> ModalResult where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2255,14 +2255,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::f32; /// -/// fn be_f32(input: &mut &[u8]) -> PResult { +/// fn be_f32(input: &mut &[u8]) -> ModalResult { /// f32(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_f32.parse_peek(&[0x41, 0x48, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); /// assert!(be_f32.parse_peek(&b"abc"[..]).is_err()); /// -/// fn le_f32(input: &mut &[u8]) -> PResult { +/// fn le_f32(input: &mut &[u8]) -> ModalResult { /// f32(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -2277,14 +2277,14 @@ where /// # use winnow::Partial; /// use winnow::binary::f32; /// -/// fn be_f32(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_f32(input: &mut Partial<&[u8]>) -> ModalResult { /// f32(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_f32.parse_peek(Partial::new(&[0x41, 0x48, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(be_f32.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// -/// fn le_f32(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_f32(input: &mut Partial<&[u8]>) -> ModalResult { /// f32(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -2326,14 +2326,14 @@ where /// # use winnow::error::Needed::Size; /// use winnow::binary::f64; /// -/// fn be_f64(input: &mut &[u8]) -> PResult { +/// fn be_f64(input: &mut &[u8]) -> ModalResult { /// f64(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_f64.parse_peek(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), Ok((&b""[..], 12.5))); /// assert!(be_f64.parse_peek(&b"abc"[..]).is_err()); /// -/// fn le_f64(input: &mut &[u8]) -> PResult { +/// fn le_f64(input: &mut &[u8]) -> ModalResult { /// f64(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -2348,14 +2348,14 @@ where /// # use winnow::Partial; /// use winnow::binary::f64; /// -/// fn be_f64(input: &mut Partial<&[u8]>) -> PResult { +/// fn be_f64(input: &mut Partial<&[u8]>) -> ModalResult { /// f64(winnow::binary::Endianness::Big).parse_next(input) /// }; /// /// assert_eq!(be_f64.parse_peek(Partial::new(&[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), Ok((Partial::new(&b""[..]), 12.5))); /// assert_eq!(be_f64.parse_peek(Partial::new(&b"abc"[..])), Err(ErrMode::Incomplete(Needed::new(5)))); /// -/// fn le_f64(input: &mut Partial<&[u8]>) -> PResult { +/// fn le_f64(input: &mut Partial<&[u8]>) -> ModalResult { /// f64(winnow::binary::Endianness::Little).parse_next(input) /// }; /// @@ -2405,7 +2405,7 @@ where /// Partial::new(Bytes::new(b)) /// } /// -/// fn parser<'i>(s: &mut Stream<'i>) -> PResult<&'i [u8]> { +/// fn parser<'i>(s: &mut Stream<'i>) -> ModalResult<&'i [u8]> { /// length_take(be_u16).parse_next(s) /// } /// @@ -2455,7 +2455,7 @@ where /// p /// } /// -/// fn parser<'i>(s: &mut Stream<'i>) -> PResult<&'i [u8]> { +/// fn parser<'i>(s: &mut Stream<'i>) -> ModalResult<&'i [u8]> { /// length_and_then(be_u16, "abc").parse_next(s) /// } /// @@ -2504,7 +2504,7 @@ where /// Bytes::new(b) /// } /// -/// fn parser<'i>(s: &mut Stream<'i>) -> PResult> { +/// fn parser<'i>(s: &mut Stream<'i>) -> ModalResult> { /// length_repeat(u8.map(|i| { /// println!("got number: {}", i); /// i diff --git a/src/combinator/branch.rs b/src/combinator/branch.rs index 6ed68b4d..048910c4 100644 --- a/src/combinator/branch.rs +++ b/src/combinator/branch.rs @@ -11,7 +11,7 @@ pub use crate::dispatch; /// This trait is implemented for tuples of up to 21 elements pub trait Alt { /// Tests each parser in the tuple and returns the result of the first one that succeeds - fn choice(&mut self, input: &mut I) -> PResult; + fn choice(&mut self, input: &mut I) -> ModalResult; } /// Pick the first successful parser @@ -33,7 +33,7 @@ pub trait Alt { /// use winnow::ascii::{alpha1, digit1}; /// use winnow::combinator::alt; /// # fn main() { -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// alt((alpha1, digit1)).parse_next(input) /// }; /// @@ -64,7 +64,7 @@ where /// This trait is implemented for tuples of up to 21 elements pub trait Permutation { /// Tries to apply all parsers in the tuple in various orders until all of them succeed - fn permutation(&mut self, input: &mut I) -> PResult; + fn permutation(&mut self, input: &mut I) -> ModalResult; } /// Applies a list of parsers in any order. @@ -84,7 +84,7 @@ pub trait Permutation { /// use winnow::ascii::{alpha1, digit1}; /// use winnow::combinator::permutation; /// # fn main() { -/// fn parser<'i>(input: &mut &'i str) -> PResult<(&'i str, &'i str)> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<(&'i str, &'i str)> { /// permutation((alpha1, digit1)).parse_next(input) /// } /// @@ -107,7 +107,7 @@ pub trait Permutation { /// use winnow::combinator::permutation; /// use winnow::token::any; /// -/// fn parser(input: &mut &str) -> PResult<(char, char)> { +/// fn parser(input: &mut &str) -> ModalResult<(char, char)> { /// permutation((any, 'a')).parse_next(input) /// } /// @@ -127,7 +127,7 @@ pub fn permutation, List: Permutation>( } impl, P: Parser> Alt for [P; N] { - fn choice(&mut self, input: &mut I) -> PResult { + fn choice(&mut self, input: &mut I) -> ModalResult { let mut error: Option = None; let start = input.checkpoint(); @@ -152,7 +152,7 @@ impl, P: Parser> Alt, P: Parser> Alt for &mut [P] { - fn choice(&mut self, input: &mut I) -> PResult { + fn choice(&mut self, input: &mut I) -> ModalResult { let mut error: Option = None; let start = input.checkpoint(); @@ -198,7 +198,7 @@ macro_rules! alt_trait_impl( $($id: Parser),+ > Alt for ( $($id),+ ) { - fn choice(&mut self, input: &mut I) -> PResult { + fn choice(&mut self, input: &mut I) -> ModalResult { let start = input.checkpoint(); match self.0.parse_next(input) { Err(ErrMode::Backtrack(e)) => alt_trait_inner!(1, self, input, start, e, $($id)+), @@ -253,7 +253,7 @@ alt_trait!(Alt2 Alt3 Alt4 Alt5 Alt6 Alt7 Alt8 Alt9 Alt10 Alt11 Alt12 Alt13 Alt14 // Manually implement Alt for (A,), the 1-tuple type impl, A: Parser> Alt for (A,) { - fn choice(&mut self, input: &mut I) -> PResult { + fn choice(&mut self, input: &mut I) -> ModalResult { self.0.parse_next(input) } } @@ -285,7 +285,7 @@ macro_rules! permutation_trait_impl( $($name: Parser),+ > Permutation for ( $($name),+ ) { - fn permutation(&mut self, input: &mut I) -> PResult<( $($ty),+ ), Error> { + fn permutation(&mut self, input: &mut I) -> ModalResult<( $($ty),+ ), Error> { let mut res = ($(Option::<$ty>::None),+); loop { diff --git a/src/combinator/core.rs b/src/combinator/core.rs index 8649bd73..a1b58169 100644 --- a/src/combinator/core.rs +++ b/src/combinator/core.rs @@ -6,7 +6,7 @@ use crate::*; /// Deprecated, replaced with [`token::rest`] #[deprecated(since = "0.6.23", note = "replaced with `token::rest`")] #[inline] -pub fn rest(input: &mut Input) -> PResult<::Slice, Error> +pub fn rest(input: &mut Input) -> ModalResult<::Slice, Error> where Input: Stream, Error: ParserError, @@ -17,7 +17,7 @@ where /// Deprecated, replaced with [`token::rest_len`] #[deprecated(since = "0.6.23", note = "replaced with `token::rest_len`")] #[inline] -pub fn rest_len(input: &mut Input) -> PResult +pub fn rest_len(input: &mut Input) -> ModalResult where Input: Stream, Error: ParserError, @@ -37,7 +37,7 @@ where /// use winnow::ascii::alpha1; /// # fn main() { /// -/// fn parser<'i>(i: &mut &'i str) -> PResult> { +/// fn parser<'i>(i: &mut &'i str) -> ModalResult> { /// opt(alpha1).parse_next(i) /// } /// @@ -76,7 +76,7 @@ where /// use winnow::ascii::alpha1; /// # fn main() { /// -/// fn parser<'i>(i: &mut &'i str) -> PResult> { +/// fn parser<'i>(i: &mut &'i str) -> ModalResult> { /// let prefix = opt("-").parse_next(i)?; /// let condition = prefix.is_some(); /// cond(condition, alpha1).parse_next(i) @@ -118,7 +118,7 @@ where /// use winnow::ascii::alpha1; /// # fn main() { /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// peek(alpha1).parse_next(input) /// } /// @@ -153,7 +153,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn eof<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn eof<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::combinator::eof.parse_next(input) /// # } @@ -166,7 +166,7 @@ where /// # use winnow::combinator::eof; /// # use winnow::prelude::*; /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// eof.parse_next(input) /// } /// assert!(parser.parse_peek("abc").is_err()); @@ -174,7 +174,7 @@ where /// ``` #[doc(alias = "end")] #[doc(alias = "eoi")] -pub fn eof(input: &mut Input) -> PResult<::Slice, Error> +pub fn eof(input: &mut Input) -> ModalResult<::Slice, Error> where Input: Stream, Error: ParserError, @@ -205,7 +205,7 @@ where /// use winnow::ascii::alpha1; /// # fn main() { /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<()> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<()> { /// not(alpha1).parse_next(input) /// } /// @@ -250,7 +250,7 @@ where /// # use winnow::prelude::*; /// # fn main() { /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// alt(( /// preceded(one_of(['+', '-']), digit1), /// rest @@ -275,7 +275,7 @@ where /// use winnow::combinator::cut_err; /// # fn main() { /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// alt(( /// preceded(one_of(['+', '-']), cut_err(digit1)), /// rest @@ -331,12 +331,12 @@ where /// # use winnow::prelude::*; /// # use winnow::combinator::todo; /// -/// fn parser(input: &mut &str) -> PResult { +/// fn parser(input: &mut &str) -> ModalResult { /// todo(input) /// } /// ``` #[track_caller] -pub fn todo(input: &mut Input) -> PResult +pub fn todo(input: &mut Input) -> ModalResult where Input: Stream, { @@ -365,7 +365,7 @@ where /// let mut it = iterator(data, terminated(alpha1, "|")); /// /// let parsed = it.map(|v| (v, v.len())).collect::>(); -/// let res: PResult<_> = it.finish(); +/// let res: ModalResult<_> = it.finish(); /// /// assert_eq!(parsed, [("abc", 3usize), ("defg", 4), ("hijkl", 5), ("mnopqr", 6)].iter().cloned().collect()); /// assert_eq!(res, Ok(("123", ()))); @@ -405,7 +405,7 @@ where I: Stream, { /// Returns the remaining input if parsing was successful, or the error if we encountered an error. - pub fn finish(mut self) -> PResult<(I, ()), E> { + pub fn finish(mut self) -> ModalResult<(I, ()), E> { match self.state.take().unwrap() { State::Running | State::Done => Ok((self.input, ())), State::Cut(e) => Err(e), @@ -474,7 +474,7 @@ enum State { /// use winnow::combinator::alt; /// use winnow::combinator::empty; /// -/// fn sign(input: &mut &str) -> PResult { +/// fn sign(input: &mut &str) -> ModalResult { /// alt(( /// '-'.value(-1), /// '+'.value(1), @@ -488,7 +488,7 @@ enum State { #[doc(alias = "value")] #[doc(alias = "success")] #[inline] -pub fn empty(_input: &mut Input) -> PResult<(), Error> +pub fn empty(_input: &mut Input) -> ModalResult<(), Error> where Input: Stream, Error: ParserError, @@ -508,7 +508,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::fail; /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<(), InputError<&'i str>> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<(), InputError<&'i str>> { /// fail.parse_next(input) /// } /// @@ -516,7 +516,7 @@ where /// ``` #[doc(alias = "unexpected")] #[inline] -pub fn fail(i: &mut Input) -> PResult +pub fn fail(i: &mut Input) -> ModalResult where Input: Stream, Error: ParserError, diff --git a/src/combinator/debug/internals.rs b/src/combinator/debug/internals.rs index d2103746..c15fd33c 100644 --- a/src/combinator/debug/internals.rs +++ b/src/combinator/debug/internals.rs @@ -46,7 +46,7 @@ where D: std::fmt::Display, { #[inline] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { let depth = Depth::new(); let original = i.checkpoint(); start(*depth, &self.name, self.call_count, i); diff --git a/src/combinator/debug/mod.rs b/src/combinator/debug/mod.rs index 641db3da..23fa51a7 100644 --- a/src/combinator/debug/mod.rs +++ b/src/combinator/debug/mod.rs @@ -22,7 +22,7 @@ use crate::Parser; /// # use winnow::prelude::*; /// use winnow::combinator::trace; /// -/// fn short_alpha<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8]> { +/// fn short_alpha<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// trace("short_alpha", /// take_while(3..=6, AsChar::is_alpha) /// ).parse_next(s) diff --git a/src/combinator/impls.rs b/src/combinator/impls.rs index 738ab5a7..58c1a780 100644 --- a/src/combinator/impls.rs +++ b/src/combinator/impls.rs @@ -29,7 +29,7 @@ where P: Parser, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { self.p.parse_next(i) } } @@ -54,7 +54,7 @@ where G: FnMut(O) -> O2, { #[inline] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { match self.parser.parse_next(i) { Err(e) => Err(e), Ok(o) => Ok((self.map)(o)), @@ -87,7 +87,7 @@ where E: FromExternalError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult { + fn parse_next(&mut self, input: &mut I) -> ModalResult { let start = input.checkpoint(); let o = self.parser.parse_next(input)?; let res = (self.map)(o).map_err(|err| { @@ -123,7 +123,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult { + fn parse_next(&mut self, input: &mut I) -> ModalResult { let start = input.checkpoint(); let o = self.parser.parse_next(input)?; let res = (self.map)(o).ok_or_else(|| { @@ -159,7 +159,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { let start = i.checkpoint(); let mut o = self.outer.parse_next(i)?; let _ = o.complete(); @@ -194,7 +194,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { let start = i.checkpoint(); let o = self.p.parse_next(i)?; let res = o.parse_slice().ok_or_else(|| { @@ -229,7 +229,7 @@ where H: Parser, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { let o = self.f.parse_next(i)?; (self.g)(o).parse_next(i) } @@ -250,7 +250,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult { + fn parse_next(&mut self, input: &mut I) -> ModalResult { trace("complete_err", |input: &mut I| { match (self.p).parse_next(input) { Err(ErrMode::Incomplete(_)) => { @@ -291,7 +291,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult { + fn parse_next(&mut self, input: &mut I) -> ModalResult { let start = input.checkpoint(); let o = self.parser.parse_next(input)?; let res = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| { @@ -322,7 +322,7 @@ where O2: Clone, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult { + fn parse_next(&mut self, input: &mut I) -> ModalResult { (self.parser).parse_next(input).map(|_| self.val.clone()) } } @@ -346,7 +346,7 @@ where O2: core::default::Default, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult { + fn parse_next(&mut self, input: &mut I) -> ModalResult { (self.parser).parse_next(input).map(|_| O2::default()) } } @@ -367,7 +367,7 @@ where F: Parser, { #[inline(always)] - fn parse_next(&mut self, input: &mut I) -> PResult<(), E> { + fn parse_next(&mut self, input: &mut I) -> ModalResult<(), E> { (self.parser).parse_next(input).map(|_| ()) } } @@ -394,7 +394,7 @@ where I: Stream, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, input: &mut I) -> ModalResult<::Slice, E> { let checkpoint = input.checkpoint(); match (self.parser).parse_next(input) { Ok(_) => { @@ -430,7 +430,7 @@ where I: Stream, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult<(O, ::Slice), E> { + fn parse_next(&mut self, input: &mut I) -> ModalResult<(O, ::Slice), E> { let checkpoint = input.checkpoint(); match (self.parser).parse_next(input) { Ok(result) => { @@ -462,7 +462,7 @@ where I: Stream + Location, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult, E> { + fn parse_next(&mut self, input: &mut I) -> ModalResult, E> { let start = input.current_token_start(); self.parser.parse_next(input).map(move |_| { let end = input.previous_token_end(); @@ -489,7 +489,7 @@ where I: Stream + Location, { #[inline] - fn parse_next(&mut self, input: &mut I) -> PResult<(O, Range), E> { + fn parse_next(&mut self, input: &mut I) -> ModalResult<(O, Range), E> { let start = input.current_token_start(); self.parser.parse_next(input).map(move |output| { let end = input.previous_token_end(); @@ -517,7 +517,7 @@ where O: Into, { #[inline] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { self.parser.parse_next(i).map(|o| o.into()) } } @@ -541,7 +541,7 @@ where E: Into, { #[inline] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { self.parser .parse_next(i) .map_err(|err| err.map(|e| e.into())) @@ -571,7 +571,7 @@ where C: Clone + crate::lib::std::fmt::Debug, { #[inline] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { let context = self.context.clone(); trace(DisplayDebug(self.context.clone()), move |i: &mut I| { let start = i.checkpoint(); @@ -612,7 +612,7 @@ where E: FromRecoverableError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { if I::is_recovery_supported() { retry_after_inner(&mut self.parser, &mut self.recover, i) } else { @@ -623,7 +623,7 @@ where #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] -fn retry_after_inner(parser: &mut P, recover: &mut R, i: &mut I) -> PResult +fn retry_after_inner(parser: &mut P, recover: &mut R, i: &mut I) -> ModalResult where P: Parser, R: Parser, @@ -688,7 +688,7 @@ where E: FromRecoverableError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult, E> { if I::is_recovery_supported() { resume_after_inner(&mut self.parser, &mut self.recover, i) } else { @@ -703,7 +703,7 @@ fn resume_after_inner( parser: &mut P, recover: &mut R, i: &mut I, -) -> PResult, E> +) -> ModalResult, E> where P: Parser, R: Parser, diff --git a/src/combinator/multi.rs b/src/combinator/multi.rs index 28d90c3d..1968dd5f 100644 --- a/src/combinator/multi.rs +++ b/src/combinator/multi.rs @@ -8,7 +8,7 @@ use crate::error::ParserError; use crate::stream::Accumulate; use crate::stream::Range; use crate::stream::Stream; -use crate::PResult; +use crate::ModalResult; use crate::Parser; /// [`Accumulate`] the output of a parser into a container, like `Vec` @@ -37,7 +37,7 @@ use crate::Parser; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat(0.., "abc").parse_next(s) /// } /// @@ -55,7 +55,7 @@ use crate::Parser; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat(1.., "abc").parse_next(s) /// } /// @@ -73,7 +73,7 @@ use crate::Parser; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat(2, "abc").parse_next(s) /// } /// @@ -92,7 +92,7 @@ use crate::Parser; /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat(0..=2, "abc").parse_next(s) /// } /// @@ -181,7 +181,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// - /// fn parser<'i>(s: &mut &'i str) -> PResult> { + /// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat( /// 0.., /// "abc" @@ -206,7 +206,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// - /// fn parser<'i>(s: &mut &'i str) -> PResult> { + /// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat( /// 1.., /// "abc", @@ -231,7 +231,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::repeat; /// - /// fn parser<'i>(s: &mut &'i str) -> PResult> { + /// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat( /// 0..=2, /// "abc", @@ -312,7 +312,7 @@ where /// use winnow::combinator::repeat; /// use std::collections::HashSet; /// - /// fn parser<'i>(s: &mut &'i str) -> PResult> { + /// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// repeat( /// 0.., /// "abc" @@ -389,7 +389,7 @@ where /// use std::io::Write; /// use std::io::Error; /// - /// fn parser(s: &mut &str) -> PResult> { + /// fn parser(s: &mut &str) -> ModalResult> { /// repeat( /// 0.., /// "abc" @@ -442,7 +442,7 @@ where E: ParserError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { let Range { start_inclusive, end_inclusive, @@ -459,7 +459,7 @@ where } } -fn repeat0_(f: &mut F, i: &mut I) -> PResult +fn repeat0_(f: &mut F, i: &mut I) -> ModalResult where I: Stream, C: Accumulate, @@ -488,7 +488,7 @@ where } } -fn repeat1_(f: &mut F, i: &mut I) -> PResult +fn repeat1_(f: &mut F, i: &mut I) -> ModalResult where I: Stream, C: Accumulate, @@ -525,7 +525,7 @@ where } } -fn repeat_n_(count: usize, f: &mut F, i: &mut I) -> PResult +fn repeat_n_(count: usize, f: &mut F, i: &mut I) -> ModalResult where I: Stream, C: Accumulate, @@ -555,7 +555,12 @@ where Ok(res) } -fn repeat_m_n_(min: usize, max: usize, parse: &mut F, input: &mut I) -> PResult +fn repeat_m_n_( + min: usize, + max: usize, + parse: &mut F, + input: &mut I, +) -> ModalResult where I: Stream, C: Accumulate, @@ -629,7 +634,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::repeat_till; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult<(Vec<&'i str>, &'i str)> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult<(Vec<&'i str>, &'i str)> { /// repeat_till(0.., "abc", "end").parse_next(s) /// }; /// @@ -671,7 +676,7 @@ where }) } -fn repeat_till0_(f: &mut F, g: &mut G, i: &mut I) -> PResult<(C, P), E> +fn repeat_till0_(f: &mut F, g: &mut G, i: &mut I) -> ModalResult<(C, P), E> where I: Stream, C: Accumulate, @@ -710,7 +715,7 @@ fn repeat_till_m_n_( f: &mut F, g: &mut G, i: &mut I, -) -> PResult<(C, P), E> +) -> ModalResult<(C, P), E> where I: Stream, C: Accumulate, @@ -794,7 +799,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::separated; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// separated(0.., "abc", "|").parse_next(s) /// } /// @@ -813,7 +818,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::separated; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// separated(1.., "abc", "|").parse_next(s) /// } /// @@ -832,7 +837,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::separated; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// separated(2, "abc", "|").parse_next(s) /// } /// @@ -851,7 +856,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::separated; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult> { /// separated(0..=2, "abc", "|").parse_next(s) /// } /// @@ -906,7 +911,7 @@ fn separated0_( parser: &mut P, separator: &mut S, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, C: Accumulate, @@ -965,7 +970,7 @@ fn separated1_( parser: &mut P, separator: &mut S, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, C: Accumulate, @@ -1021,7 +1026,7 @@ fn separated_n_( parser: &mut P, separator: &mut S, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, C: Accumulate, @@ -1082,7 +1087,7 @@ fn separated_m_n_( parser: &mut P, separator: &mut S, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, C: Accumulate, @@ -1187,7 +1192,7 @@ where /// use winnow::combinator::separated_foldl1; /// use winnow::ascii::dec_int; /// -/// fn parser(s: &mut &str) -> PResult { +/// fn parser(s: &mut &str) -> ModalResult { /// separated_foldl1(dec_int, "-", |l, _, r| l - r).parse_next(s) /// } /// @@ -1254,7 +1259,7 @@ where /// use winnow::combinator::separated_foldr1; /// use winnow::ascii::dec_uint; /// -/// fn parser(s: &mut &str) -> PResult { +/// fn parser(s: &mut &str) -> ModalResult { /// separated_foldr1(dec_uint, "^", |l: u32, _, r: u32| l.pow(r)).parse_next(s) /// } /// @@ -1304,7 +1309,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::fill; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult<[&'i str; 2]> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult<[&'i str; 2]> { /// let mut buf = ["", ""]; /// fill("abc", &mut buf).parse_next(s)?; /// Ok(buf) @@ -1347,7 +1352,7 @@ fn fold_repeat0_( init: &mut H, g: &mut G, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, F: Parser, @@ -1388,7 +1393,7 @@ fn fold_repeat1_( init: &mut H, g: &mut G, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, F: Parser, @@ -1438,7 +1443,7 @@ fn fold_repeat_m_n_( init: &mut H, fold: &mut G, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, F: Parser, @@ -1497,7 +1502,7 @@ fn verify_fold_m_n( init: &mut H, fold: &mut G, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, F: Parser, @@ -1562,7 +1567,7 @@ fn try_fold_m_n( init: &mut H, fold: &mut G, input: &mut I, -) -> PResult +) -> ModalResult where I: Stream, F: Parser, diff --git a/src/combinator/sequence.rs b/src/combinator/sequence.rs index f22b191e..176c23cb 100644 --- a/src/combinator/sequence.rs +++ b/src/combinator/sequence.rs @@ -18,7 +18,7 @@ pub use crate::seq; /// # use winnow::error::Needed::Size; /// use winnow::combinator::preceded; /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// preceded("abc", "efg").parse_next(input) /// } /// @@ -56,7 +56,7 @@ where /// # use winnow::error::Needed::Size; /// use winnow::combinator::terminated; /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// terminated("abc", "efg").parse_next(input) /// } /// @@ -94,7 +94,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::separated_pair; /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<(&'i str, &'i str)> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<(&'i str, &'i str)> { /// separated_pair("abc", "|", "efg").parse_next(input) /// } /// @@ -134,7 +134,7 @@ where /// # use winnow::prelude::*; /// use winnow::combinator::delimited; /// -/// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// delimited("(", "abc", ")").parse_next(input) /// } /// diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index f9bc3368..b0611f1c 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -15,7 +15,7 @@ use crate::lib::std::borrow::ToOwned; use crate::prelude::*; use crate::stream::Stream; use crate::token::take; -use crate::PResult; +use crate::ModalResult; use crate::Partial; #[cfg(feature = "alloc")] @@ -126,7 +126,7 @@ impl ParserError for CustomError { struct CustomError; #[allow(dead_code)] -fn custom_error<'i>(input: &mut &'i [u8]) -> PResult<&'i [u8], CustomError> { +fn custom_error<'i>(input: &mut &'i [u8]) -> ModalResult<&'i [u8], CustomError> { //fix_error!(input, CustomError<_>, alphanumeric) crate::ascii::alphanumeric1.parse_next(input) } @@ -157,7 +157,7 @@ Ok( } #[allow(dead_code)] -fn test_closure_compiles_195(input: &mut &[u8]) -> PResult<()> { +fn test_closure_compiles_195(input: &mut &[u8]) -> ModalResult<()> { u8.flat_map(|num| repeat(num as usize, u16(Endianness::Big))) .parse_next(input) } @@ -1333,26 +1333,26 @@ fn alt_test() { } } - fn work<'i>(input: &mut &'i [u8]) -> PResult<&'i [u8], ErrorStr> { + fn work<'i>(input: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { Ok(input.finish()) } #[allow(unused_variables)] - fn dont_work<'i>(input: &mut &'i [u8]) -> PResult<&'i [u8], ErrorStr> { + fn dont_work<'i>(input: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { Err(ErrMode::Backtrack(ErrorStr("abcd".to_owned()))) } - fn work2<'i>(_input: &mut &'i [u8]) -> PResult<&'i [u8], ErrorStr> { + fn work2<'i>(_input: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { Ok(&b""[..]) } - fn alt1<'i>(i: &mut &'i [u8]) -> PResult<&'i [u8], ErrorStr> { + fn alt1<'i>(i: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { alt((dont_work, dont_work)).parse_next(i) } - fn alt2<'i>(i: &mut &'i [u8]) -> PResult<&'i [u8], ErrorStr> { + fn alt2<'i>(i: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { alt((dont_work, work)).parse_next(i) } - fn alt3<'i>(i: &mut &'i [u8]) -> PResult<&'i [u8], ErrorStr> { + fn alt3<'i>(i: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { alt((dont_work, dont_work, work2, dont_work)).parse_next(i) } //named!(alt1, alt!(dont_work | dont_work)); diff --git a/src/error.rs b/src/error.rs index 09efb2f9..dadd36e7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,7 +8,7 @@ //! - Can be modified according to the user's needs, because some languages need a lot more information //! - Help thread-through the [stream][crate::stream] //! -//! To abstract these needs away from the user, generally `winnow` parsers use the [`PResult`] +//! To abstract these needs away from the user, generally `winnow` parsers use the [`ModalResult`] //! alias, rather than [`Result`]. [`Parser::parse`] is a top-level operation //! that can help convert to a `Result` for integrating with your application's error reporting. //! @@ -46,12 +46,12 @@ pub type ModalResult = Result>; #[deprecated(since = "0.6.23", note = "Replaced with ModalResult")] pub type PResult = ModalResult; -/// Deprecated, replaced with [`PResult`] -#[deprecated(since = "0.6.25", note = "Replaced with `PResult`")] -pub type IResult> = PResult<(I, O), E>; +/// Deprecated, replaced with [`ModalResult`] +#[deprecated(since = "0.6.25", note = "Replaced with `ModalResult`")] +pub type IResult> = ModalResult<(I, O), E>; #[cfg(test)] -pub(crate) type TestResult = PResult>; +pub(crate) type TestResult = ModalResult>; /// Contains information on needed data if a parser returned `Incomplete` /// diff --git a/src/lib.rs b/src/lib.rs index 70e098fb..4ff675fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,7 +133,7 @@ pub mod _tutorial; /// ```rust /// use winnow::prelude::*; /// -/// fn parse_data(input: &mut &str) -> PResult { +/// fn parse_data(input: &mut &str) -> ModalResult { /// // ... /// # winnow::ascii::dec_uint(input) /// } @@ -150,6 +150,7 @@ pub mod prelude { pub use crate::stream::Stream as _; pub use crate::stream::StreamIsPartial as _; pub use crate::ModalResult; + #[allow(deprecated)] pub use crate::PResult; pub use crate::Parser; #[cfg(feature = "unstable-recover")] @@ -161,6 +162,7 @@ pub mod prelude { } pub use error::ModalResult; +#[allow(deprecated)] pub use error::PResult; pub use parser::*; pub use stream::BStr; diff --git a/src/macros/dispatch.rs b/src/macros/dispatch.rs index b763d4ba..4b8b1728 100644 --- a/src/macros/dispatch.rs +++ b/src/macros/dispatch.rs @@ -17,11 +17,11 @@ /// # use winnow::combinator::empty; /// # use winnow::combinator::fail; /// -/// fn escaped(input: &mut &str) -> PResult { +/// fn escaped(input: &mut &str) -> ModalResult { /// preceded('\\', escape_seq_char).parse_next(input) /// } /// -/// fn escape_seq_char(input: &mut &str) -> PResult { +/// fn escape_seq_char(input: &mut &str) -> ModalResult { /// dispatch! {any; /// 'b' => empty.value('\u{8}'), /// 'f' => empty.value('\u{c}'), diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 32bd5247..fbcaac23 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -4,7 +4,7 @@ mod seq; #[cfg(test)] macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: $crate::error::PResult<_, $crate::error::InputError<_>> = $left; + let res: $crate::error::ModalResult<_, $crate::error::InputError<_>> = $left; snapbox::assert_data_eq!(snapbox::data::ToDebug::to_debug(&res), $right); }; ); diff --git a/src/macros/seq.rs b/src/macros/seq.rs index 1b8ea9df..95d30a3c 100644 --- a/src/macros/seq.rs +++ b/src/macros/seq.rs @@ -28,7 +28,7 @@ /// } /// /// // Parse into structs / tuple-structs -/// fn field(input: &mut &[u8]) -> PResult { +/// fn field(input: &mut &[u8]) -> ModalResult { /// seq!{Field { /// namespace: empty.value(5), /// name: alphanumeric1.map(|s: &[u8]| s.to_owned()), @@ -43,7 +43,7 @@ /// } /// /// // Or parse into tuples -/// fn point(input: &mut &[u8]) -> PResult<(u32, u32)> { +/// fn point(input: &mut &[u8]) -> ModalResult<(u32, u32)> { /// let mut num = dec_uint::<_, u32, ContextError>; /// seq!(num, _: (space0, b',', space0), num).parse_next(input) /// } diff --git a/src/parser.rs b/src/parser.rs index aeeb3210..fcaa2bf7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,7 +5,7 @@ use crate::combinator::impls; #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] use crate::error::FromRecoverableError; -use crate::error::{AddContext, FromExternalError, PResult, ParseError, ParserError}; +use crate::error::{AddContext, FromExternalError, ModalResult, ParseError, ParserError}; use crate::stream::{Compare, Location, ParseSlice, Stream, StreamIsPartial}; #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] @@ -17,7 +17,7 @@ use crate::stream::{Recover, Recoverable}; /// ```rust /// use winnow::prelude::*; /// -/// fn empty(input: &mut &str) -> PResult<()> { +/// fn empty(input: &mut &str) -> ModalResult<()> { /// let output = (); /// Ok(output) /// } @@ -30,7 +30,7 @@ use crate::stream::{Recover, Recoverable}; /// ```rust /// use winnow::prelude::*; /// -/// fn empty(output: O) -> impl FnMut(&mut &str) -> PResult { +/// fn empty(output: O) -> impl FnMut(&mut &str) -> ModalResult { /// move |input: &mut &str| { /// let output = output.clone(); /// Ok(output) @@ -78,7 +78,7 @@ pub trait Parser { /// This includes advancing the [`Stream`] to the next location. /// /// On error, `input` will be left pointing at the error location. - fn parse_next(&mut self, input: &mut I) -> PResult; + fn parse_next(&mut self, input: &mut I) -> ModalResult; /// Take tokens from the [`Stream`], turning it into the output /// @@ -95,7 +95,7 @@ pub trait Parser { /// /// #[inline(always)] - fn parse_peek(&mut self, mut input: I) -> PResult<(I, O), E> { + fn parse_peek(&mut self, mut input: I) -> ModalResult<(I, O), E> { match self.parse_next(&mut input) { Ok(o) => Ok((input, o)), Err(err) => Err(err), @@ -168,7 +168,7 @@ pub trait Parser { /// use winnow::ascii::alpha1; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult { /// alpha1.value(1234).parse_next(input) /// } /// @@ -202,7 +202,7 @@ pub trait Parser { /// use winnow::ascii::alpha1; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult { /// alpha1.default_value().parse_next(input) /// } /// @@ -235,7 +235,7 @@ pub trait Parser { /// use winnow::ascii::alpha1; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult<()> { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult<()> { /// alpha1.void().parse_next(input) /// } /// @@ -266,14 +266,14 @@ pub trait Parser { /// use winnow::ascii::alpha1; /// # fn main() { /// - /// fn parser1<'s>(i: &mut &'s str) -> PResult<&'s str> { + /// fn parser1<'s>(i: &mut &'s str) -> ModalResult<&'s str> { /// alpha1(i) /// } /// /// let mut parser2 = parser1.output_into(); /// /// // the parser converts the &str output of the child parser into a Vec - /// let bytes: PResult<(_, Vec), _> = parser2.parse_peek("abcd"); + /// let bytes: ModalResult<(_, Vec), _> = parser2.parse_peek("abcd"); /// assert_eq!(bytes, Ok(("", vec![97, 98, 99, 100]))); /// # } /// ``` @@ -303,7 +303,7 @@ pub trait Parser { /// use winnow::combinator::separated_pair; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// separated_pair(alpha1, ',', alpha1).take().parse_next(input) /// } /// @@ -362,7 +362,7 @@ pub trait Parser { /// use winnow::token::literal; /// use winnow::combinator::separated_pair; /// - /// fn parser<'i>(input: &mut &'i str) -> PResult<(bool, &'i str)> { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult<(bool, &'i str)> { /// separated_pair(alpha1, ',', alpha1).value(true).with_taken().parse_next(input) /// } /// @@ -413,7 +413,7 @@ pub trait Parser { /// use winnow::ascii::alpha1; /// use winnow::combinator::separated_pair; /// - /// fn parser<'i>(input: &mut LocatingSlice<&'i str>) -> PResult<(Range, Range)> { + /// fn parser<'i>(input: &mut LocatingSlice<&'i str>) -> ModalResult<(Range, Range)> { /// separated_pair(alpha1.span(), ',', alpha1.span()).parse_next(input) /// } /// @@ -455,7 +455,7 @@ pub trait Parser { /// use winnow::token::literal; /// use winnow::combinator::separated_pair; /// - /// fn parser<'i>(input: &mut LocatingSlice<&'i str>) -> PResult<((usize, Range), (usize, Range))> { + /// fn parser<'i>(input: &mut LocatingSlice<&'i str>) -> ModalResult<((usize, Range), (usize, Range))> { /// separated_pair(alpha1.value(1).with_span(), ',', alpha1.value(2).with_span()).parse_next(input) /// } /// @@ -486,7 +486,7 @@ pub trait Parser { /// # use winnow::ascii::digit1; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult { /// digit1.map(|s: &str| s.len()).parse_next(input) /// } /// @@ -523,7 +523,7 @@ pub trait Parser { /// use winnow::ascii::digit1; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult { /// digit1.try_map(|s: &str| s.parse::()).parse_next(input) /// } /// @@ -566,7 +566,7 @@ pub trait Parser { /// use winnow::ascii::digit1; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult { /// digit1.verify_map(|s: &str| s.parse::().ok()).parse_next(input) /// } /// @@ -606,11 +606,11 @@ pub trait Parser { /// # Example /// /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, PResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, ModalResult, Parser}; /// use winnow::token::take; /// use winnow::binary::u8; /// - /// fn length_take<'s>(input: &mut &'s [u8]) -> PResult<&'s [u8]> { + /// fn length_take<'s>(input: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// u8.flat_map(take).parse_next(input) /// } /// @@ -620,11 +620,11 @@ pub trait Parser { /// /// which is the same as /// ```rust - /// # use winnow::{error::ErrMode,error::ErrorKind, PResult, Parser}; + /// # use winnow::{error::ErrMode,error::ErrorKind, ModalResult, Parser}; /// use winnow::token::take; /// use winnow::binary::u8; /// - /// fn length_take<'s>(input: &mut &'s [u8]) -> PResult<&'s [u8]> { + /// fn length_take<'s>(input: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// let length = u8.parse_next(input)?; /// let data = take(length).parse_next(input)?; /// Ok(data) @@ -662,7 +662,7 @@ pub trait Parser { /// use winnow::token::take; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// take(5u8).and_then(digit1).parse_next(input) /// } /// @@ -698,7 +698,7 @@ pub trait Parser { /// use winnow::{error::ErrMode,error::ErrorKind, Parser}; /// use winnow::ascii::digit1; /// - /// fn parser<'s>(input: &mut &'s str) -> PResult { + /// fn parser<'s>(input: &mut &'s str) -> ModalResult { /// digit1.parse_to().parse_next(input) /// } /// @@ -739,7 +739,7 @@ pub trait Parser { /// # use winnow::prelude::*; /// # fn main() { /// - /// fn parser<'i>(input: &mut &'i str) -> PResult<&'i str> { + /// fn parser<'i>(input: &mut &'i str) -> ModalResult<&'i str> { /// alpha1.verify(|s: &str| s.len() == 4).parse_next(input) /// } /// @@ -802,7 +802,7 @@ pub trait Parser { /// # use winnow::prelude::*; /// # fn main() { /// - /// fn parser<'i>(input: &mut Partial<&'i str>) -> PResult<&'i str, InputError>> { + /// fn parser<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str, InputError>> { /// take(5u8).complete_err().parse_next(input) /// } /// @@ -893,11 +893,11 @@ pub trait Parser { impl Parser for F where - F: FnMut(&mut I) -> PResult, + F: FnMut(&mut I) -> ModalResult, I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { self(i) } } @@ -909,7 +909,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::{error::ErrMode, error::{ErrorKind, ContextError}}; -/// fn parser<'s>(i: &mut &'s [u8]) -> PResult { +/// fn parser<'s>(i: &mut &'s [u8]) -> ModalResult { /// b'a'.parse_next(i) /// } /// assert_eq!(parser.parse_peek(&b"abc"[..]), Ok((&b"bc"[..], b'a'))); @@ -925,7 +925,7 @@ where E: ParserError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { crate::token::literal(*self).value(*self).parse_next(i) } } @@ -937,7 +937,7 @@ where /// ```rust /// # use winnow::prelude::*; /// # use winnow::{error::ErrMode, error::{ErrorKind, ContextError}}; -/// fn parser<'s>(i: &mut &'s str) -> PResult { +/// fn parser<'s>(i: &mut &'s str) -> ModalResult { /// 'a'.parse_next(i) /// } /// assert_eq!(parser.parse_peek("abc"), Ok(("bc", 'a'))); @@ -953,7 +953,7 @@ where E: ParserError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { crate::token::literal(*self).value(*self).parse_next(i) } } @@ -967,7 +967,7 @@ where /// # use winnow::combinator::alt; /// # use winnow::token::take; /// -/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8]> { +/// fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// alt((&"Hello"[..], take(5usize))).parse_next(s) /// } /// @@ -982,7 +982,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -997,7 +997,7 @@ where /// # use winnow::token::take; /// use winnow::ascii::Caseless; /// -/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8]> { +/// fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// alt((Caseless(&"hello"[..]), take(5usize))).parse_next(s) /// } /// @@ -1014,7 +1014,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1028,7 +1028,7 @@ where /// # use winnow::combinator::alt; /// # use winnow::token::take; /// -/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8]> { +/// fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// alt((b"Hello", take(5usize))).parse_next(s) /// } /// @@ -1043,7 +1043,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1058,7 +1058,7 @@ where /// # use winnow::token::take; /// use winnow::ascii::Caseless; /// -/// fn parser<'s>(s: &mut &'s [u8]) -> PResult<&'s [u8]> { +/// fn parser<'s>(s: &mut &'s [u8]) -> ModalResult<&'s [u8]> { /// alt((Caseless(b"hello"), take(5usize))).parse_next(s) /// } /// @@ -1076,7 +1076,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1090,7 +1090,7 @@ where /// # use winnow::combinator::alt; /// # use winnow::token::take; /// -/// fn parser<'s>(s: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(s: &mut &'s str) -> ModalResult<&'s str> { /// alt(("Hello", take(5usize))).parse_next(s) /// } /// @@ -1105,7 +1105,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1120,7 +1120,7 @@ where /// # use winnow::token::take; /// # use winnow::ascii::Caseless; /// -/// fn parser<'s>(s: &mut &'s str) -> PResult<&'s str> { +/// fn parser<'s>(s: &mut &'s str) -> ModalResult<&'s str> { /// alt((Caseless("hello"), take(5usize))).parse_next(s) /// } /// @@ -1137,14 +1137,14 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { crate::token::literal(*self).parse_next(i) } } impl> Parser for () { #[inline(always)] - fn parse_next(&mut self, _i: &mut I) -> PResult<(), E> { + fn parse_next(&mut self, _i: &mut I) -> ModalResult<(), E> { Ok(()) } } @@ -1157,7 +1157,7 @@ macro_rules! impl_parser_for_tuple { $($parser: Parser),+ { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult<($($output),+,), E> { + fn parse_next(&mut self, i: &mut I) -> ModalResult<($($output),+,), E> { $(let $output = self.$index.parse_next(i)?;)+ Ok(($($output),+,)) @@ -1210,7 +1210,7 @@ use crate::lib::std::boxed::Box; #[cfg(feature = "alloc")] impl Parser for Box + '_> { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> PResult { + fn parse_next(&mut self, i: &mut I) -> ModalResult { (**self).parse_next(i) } } @@ -1287,7 +1287,7 @@ where #[allow(deprecated)] pub fn unpeek<'a, I, O, E>( mut peek: impl FnMut(I) -> crate::error::IResult + 'a, -) -> impl FnMut(&mut I) -> PResult +) -> impl FnMut(&mut I) -> ModalResult where I: Clone, { @@ -1326,8 +1326,8 @@ mod tests { #[test] #[cfg(target_pointer_width = "64")] fn size_test() { - assert_size!(PResult<&[u8], (&[u8], u32)>, 40); - assert_size!(PResult<&str, u32>, 40); + assert_size!(ModalResult<&[u8], (&[u8], u32)>, 40); + assert_size!(ModalResult<&str, u32>, 40); assert_size!(Needed, 8); assert_size!(ErrMode, 16); assert_size!(ErrorKind, 1); diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 34c1c0b6..9f9b3d46 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -307,7 +307,7 @@ where /// /// type Stream<'is> = Stateful<&'is str, State<'is>>; /// -/// fn word<'s>(i: &mut Stream<'s>) -> PResult<&'s str> { +/// fn word<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { /// i.state.count(); /// alpha1.parse_next(i) /// } @@ -370,14 +370,14 @@ impl crate::lib::std::fmt::Display for Stat /// Here is how it works in practice: /// /// ```rust -/// # use winnow::{PResult, error::ErrMode, error::Needed, error::{ContextError, ErrorKind}, token, ascii, stream::Partial}; +/// # use winnow::{ModalResult, error::ErrMode, error::Needed, error::{ContextError, ErrorKind}, token, ascii, stream::Partial}; /// # use winnow::prelude::*; /// -/// fn take_partial<'s>(i: &mut Partial<&'s [u8]>) -> PResult<&'s [u8], ContextError> { +/// fn take_partial<'s>(i: &mut Partial<&'s [u8]>) -> ModalResult<&'s [u8], ContextError> { /// token::take(4u8).parse_next(i) /// } /// -/// fn take_complete<'s>(i: &mut &'s [u8]) -> PResult<&'s [u8], ContextError> { +/// fn take_complete<'s>(i: &mut &'s [u8]) -> ModalResult<&'s [u8], ContextError> { /// token::take(4u8).parse_next(i) /// } /// @@ -393,11 +393,11 @@ impl crate::lib::std::fmt::Display for Stat /// assert!(take_complete.parse_peek(&b"abc"[..]).is_err()); /// /// // the alpha0 function takes 0 or more alphabetic characters -/// fn alpha0_partial<'s>(i: &mut Partial<&'s str>) -> PResult<&'s str, ContextError> { +/// fn alpha0_partial<'s>(i: &mut Partial<&'s str>) -> ModalResult<&'s str, ContextError> { /// ascii::alpha0.parse_next(i) /// } /// -/// fn alpha0_complete<'s>(i: &mut &'s str) -> PResult<&'s str, ContextError> { +/// fn alpha0_complete<'s>(i: &mut &'s str) -> ModalResult<&'s str, ContextError> { /// ascii::alpha0.parse_next(i) /// } /// @@ -2838,7 +2838,7 @@ impl crate::lib::std::fmt::Debug for Checkpoi /// # use winnow::prelude::*; /// # use winnow::token::any; /// # use winnow::combinator::repeat; -/// # fn inner(input: &mut &str) -> PResult { +/// # fn inner(input: &mut &str) -> ModalResult { /// # any.parse_next(input) /// # } /// # let mut input = "0123456789012345678901234567890123456789"; @@ -3415,7 +3415,7 @@ impl AsChar for &char { /// # use winnow::prelude::*; /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError}; /// # use winnow::token::take_while; -/// fn hex_digit1<'s>(input: &mut &'s str) -> PResult<&'s str, ContextError> { +/// fn hex_digit1<'s>(input: &mut &'s str) -> ModalResult<&'s str, ContextError> { /// take_while(1.., ('a'..='f', 'A'..='F', '0'..='9')).parse_next(input) /// } /// diff --git a/src/stream/tests.rs b/src/stream/tests.rs index b95a6f18..a851fa5b 100644 --- a/src/stream/tests.rs +++ b/src/stream/tests.rs @@ -6,7 +6,7 @@ use crate::error::{ErrorKind, InputError}; use crate::token::literal; use crate::{ combinator::{separated, separated_pair}, - PResult, Parser, + ModalResult, Parser, }; use super::*; @@ -15,7 +15,7 @@ use super::*; #[test] fn test_fxhashmap_compiles() { let input = "a=b"; - fn pair(i: &mut &str) -> PResult<(char, char)> { + fn pair(i: &mut &str) -> ModalResult<(char, char)> { let out = separated_pair('a', '=', 'b').parse_next(i)?; Ok(out) } diff --git a/src/token/mod.rs b/src/token/mod.rs index a9ba8a4e..af96713d 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -13,7 +13,7 @@ use crate::lib::std::result::Result::Ok; use crate::stream::Range; use crate::stream::{Compare, CompareResult, ContainsToken, FindSlice, SliceLen, Stream}; use crate::stream::{StreamIsPartial, ToUsize}; -use crate::PResult; +use crate::ModalResult; use crate::Parser; /// Matches one token @@ -27,7 +27,7 @@ use crate::Parser; /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn any(input: &mut &str) -> PResult +/// pub fn any(input: &mut &str) -> ModalResult /// # { /// # winnow::token::any.parse_next(input) /// # } @@ -38,7 +38,7 @@ use crate::Parser; /// ```rust /// # use winnow::{token::any, error::ErrMode, error::{ContextError, ErrorKind}}; /// # use winnow::prelude::*; -/// fn parser(input: &mut &str) -> PResult { +/// fn parser(input: &mut &str) -> ModalResult { /// any.parse_next(input) /// } /// @@ -55,7 +55,7 @@ use crate::Parser; /// ``` #[inline(always)] #[doc(alias = "token")] -pub fn any(input: &mut Input) -> PResult<::Token, Error> +pub fn any(input: &mut Input) -> ModalResult<::Token, Error> where Input: StreamIsPartial + Stream, Error: ParserError, @@ -72,7 +72,7 @@ where fn any_, const PARTIAL: bool>( input: &mut I, -) -> PResult<::Token, E> +) -> ModalResult<::Token, E> where I: StreamIsPartial, I: Stream, @@ -117,7 +117,7 @@ where /// # use winnow::prelude::*; /// # use winnow::{error::ErrMode, error::{ContextError, ErrorKind}, error::Needed}; /// # -/// fn parser<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// "Hello".parse_next(s) /// } /// @@ -131,7 +131,7 @@ where /// # use winnow::{error::ErrMode, error::{ContextError, ErrorKind}, error::Needed}; /// # use winnow::Partial; /// -/// fn parser<'i>(s: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn parser<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// "Hello".parse_next(s) /// } /// @@ -147,7 +147,7 @@ where /// use winnow::token::literal; /// use winnow::ascii::Caseless; /// -/// fn parser<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn parser<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// literal(Caseless("hello")).parse_next(s) /// } /// @@ -182,7 +182,7 @@ where fn literal_, const PARTIAL: bool>( i: &mut I, t: T, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream + Compare, @@ -239,7 +239,7 @@ where /// assert!(one_of::<_, _, ContextError>('a').parse_peek("bc").is_err()); /// assert!(one_of::<_, _, ContextError>('a').parse_peek("").is_err()); /// -/// fn parser_fn(i: &mut &str) -> PResult { +/// fn parser_fn(i: &mut &str) -> ModalResult { /// one_of(|c| c == 'a' || c == 'b').parse_next(i) /// } /// assert_eq!(parser_fn.parse_peek("abc"), Ok(("bc", 'a'))); @@ -256,7 +256,7 @@ where /// assert!(one_of::<_, _, ContextError>('a').parse_peek(Partial::new("bc")).is_err()); /// assert_eq!(one_of::<_, _, ContextError>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// -/// fn parser_fn(i: &mut Partial<&str>) -> PResult { +/// fn parser_fn(i: &mut Partial<&str>) -> ModalResult { /// one_of(|c| c == 'a' || c == 'b').parse_next(i) /// } /// assert_eq!(parser_fn.parse_peek(Partial::new("abc")), Ok((Partial::new("bc"), 'a'))); @@ -369,7 +369,7 @@ where /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// -/// fn alpha<'i>(s: &mut &'i [u8]) -> PResult<&'i [u8]> { +/// fn alpha<'i>(s: &mut &'i [u8]) -> ModalResult<&'i [u8]> { /// take_while(0.., AsChar::is_alpha).parse_next(s) /// } /// @@ -386,7 +386,7 @@ where /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// -/// fn alpha<'i>(s: &mut Partial<&'i [u8]>) -> PResult<&'i [u8]> { +/// fn alpha<'i>(s: &mut Partial<&'i [u8]>) -> ModalResult<&'i [u8]> { /// take_while(0.., AsChar::is_alpha).parse_next(s) /// } /// @@ -403,7 +403,7 @@ where /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// -/// fn alpha<'i>(s: &mut &'i [u8]) -> PResult<&'i [u8]> { +/// fn alpha<'i>(s: &mut &'i [u8]) -> ModalResult<&'i [u8]> { /// take_while(1.., AsChar::is_alpha).parse_next(s) /// } /// @@ -411,7 +411,7 @@ where /// assert_eq!(alpha.parse_peek(b"latin"), Ok((&b""[..], &b"latin"[..]))); /// assert!(alpha.parse_peek(b"12345").is_err()); /// -/// fn hex<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn hex<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// take_while(1.., ('0'..='9', 'A'..='F')).parse_next(s) /// } /// @@ -429,7 +429,7 @@ where /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// -/// fn alpha<'i>(s: &mut Partial<&'i [u8]>) -> PResult<&'i [u8]> { +/// fn alpha<'i>(s: &mut Partial<&'i [u8]>) -> ModalResult<&'i [u8]> { /// take_while(1.., AsChar::is_alpha).parse_next(s) /// } /// @@ -437,7 +437,7 @@ where /// assert_eq!(alpha.parse_peek(Partial::new(b"latin")), Err(ErrMode::Incomplete(Needed::new(1)))); /// assert!(alpha.parse_peek(Partial::new(b"12345")).is_err()); /// -/// fn hex<'i>(s: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn hex<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// take_while(1.., ('0'..='9', 'A'..='F')).parse_next(s) /// } /// @@ -455,7 +455,7 @@ where /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// -/// fn short_alpha<'i>(s: &mut &'i [u8]) -> PResult<&'i [u8]> { +/// fn short_alpha<'i>(s: &mut &'i [u8]) -> ModalResult<&'i [u8]> { /// take_while(3..=6, AsChar::is_alpha).parse_next(s) /// } /// @@ -473,7 +473,7 @@ where /// use winnow::token::take_while; /// use winnow::stream::AsChar; /// -/// fn short_alpha<'i>(s: &mut Partial<&'i [u8]>) -> PResult<&'i [u8]> { +/// fn short_alpha<'i>(s: &mut Partial<&'i [u8]>) -> ModalResult<&'i [u8]> { /// take_while(3..=6, AsChar::is_alpha).parse_next(s) /// } /// @@ -531,7 +531,7 @@ where fn take_till0, const PARTIAL: bool>( input: &mut I, predicate: P, -) -> PResult<::Slice, E> +) -> ModalResult<::Slice, E> where P: FnMut(I::Token) -> bool, { @@ -548,7 +548,7 @@ where fn take_till1, const PARTIAL: bool>( input: &mut I, predicate: P, -) -> PResult<::Slice, E> +) -> ModalResult<::Slice, E> where P: FnMut(I::Token) -> bool, { @@ -572,7 +572,7 @@ fn take_till_m_n, const PARTIAL: bool>( m: usize, n: usize, mut predicate: P, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -652,7 +652,7 @@ where /// # use winnow::prelude::*; /// use winnow::token::take_till; /// -/// fn till_colon<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn till_colon<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// take_till(0.., |c| c == ':').parse_next(s) /// } /// @@ -668,7 +668,7 @@ where /// # use winnow::Partial; /// use winnow::token::take_till; /// -/// fn till_colon<'i>(s: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn till_colon<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// take_till(0.., |c| c == ':').parse_next(s) /// } /// @@ -752,7 +752,7 @@ where /// # use winnow::prelude::*; /// use winnow::token::take; /// -/// fn take6<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn take6<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// take(6usize).parse_next(s) /// } /// @@ -781,7 +781,7 @@ where /// # use winnow::Partial; /// use winnow::token::take; /// -/// fn take6<'i>(s: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn take6<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// take(6usize).parse_next(s) /// } /// @@ -812,7 +812,7 @@ where fn take_, const PARTIAL: bool>( i: &mut I, c: usize, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -860,7 +860,7 @@ where /// # use winnow::prelude::*; /// use winnow::token::take_until; /// -/// fn until_eof<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn until_eof<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// take_until(0.., "eof").parse_next(s) /// } /// @@ -876,7 +876,7 @@ where /// # use winnow::Partial; /// use winnow::token::take_until; /// -/// fn until_eof<'i>(s: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn until_eof<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// take_until(0.., "eof").parse_next(s) /// } /// @@ -891,7 +891,7 @@ where /// # use winnow::prelude::*; /// use winnow::token::take_until; /// -/// fn until_eof<'i>(s: &mut &'i str) -> PResult<&'i str> { +/// fn until_eof<'i>(s: &mut &'i str) -> ModalResult<&'i str> { /// take_until(1.., "eof").parse_next(s) /// } /// @@ -908,7 +908,7 @@ where /// # use winnow::Partial; /// use winnow::token::take_until; /// -/// fn until_eof<'i>(s: &mut Partial<&'i str>) -> PResult<&'i str> { +/// fn until_eof<'i>(s: &mut Partial<&'i str>) -> ModalResult<&'i str> { /// take_until(1.., "eof").parse_next(s) /// } /// @@ -963,7 +963,7 @@ where fn take_until0_, const PARTIAL: bool>( i: &mut I, t: T, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice, @@ -978,7 +978,7 @@ where fn take_until1_, const PARTIAL: bool>( i: &mut I, t: T, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice, @@ -1001,7 +1001,7 @@ fn take_until_m_n_, const PARTIAL: bool>( start: usize, end: usize, t: T, -) -> PResult<::Slice, Error> +) -> ModalResult<::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice, @@ -1041,7 +1041,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn rest<'i>(input: &mut &'i str) -> PResult<&'i str> +/// pub fn rest<'i>(input: &mut &'i str) -> ModalResult<&'i str> /// # { /// # winnow::token::rest.parse_next(input) /// # } @@ -1058,7 +1058,7 @@ where /// assert_eq!(rest::<_,ContextError>.parse_peek(""), Ok(("", ""))); /// ``` #[inline] -pub fn rest(input: &mut Input) -> PResult<::Slice, Error> +pub fn rest(input: &mut Input) -> ModalResult<::Slice, Error> where Input: Stream, Error: ParserError, @@ -1079,7 +1079,7 @@ where /// Assuming you are parsing a `&str` [Stream]: /// ```rust /// # use winnow::prelude::*;; -/// pub fn rest_len(input: &mut &str) -> PResult +/// pub fn rest_len(input: &mut &str) -> ModalResult /// # { /// # winnow::token::rest_len.parse_next(input) /// # } @@ -1096,7 +1096,7 @@ where /// assert_eq!(rest_len::<_,ContextError>.parse_peek(""), Ok(("", 0))); /// ``` #[inline] -pub fn rest_len(input: &mut Input) -> PResult +pub fn rest_len(input: &mut Input) -> ModalResult where Input: Stream, Error: ParserError, diff --git a/src/token/tests.rs b/src/token/tests.rs index a8d5a483..befe2be0 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -67,7 +67,7 @@ fn model_complete_take_while_m_n<'i>( n: usize, valid: usize, input: &mut &'i str, -) -> PResult<&'i str> { +) -> ModalResult<&'i str> { if n < m { Err(crate::error::ErrMode::from_error_kind( input, diff --git a/tests/testsuite/custom_errors.rs b/tests/testsuite/custom_errors.rs index c2d080c0..af965754 100644 --- a/tests/testsuite/custom_errors.rs +++ b/tests/testsuite/custom_errors.rs @@ -33,23 +33,23 @@ impl<'a> ParserError> for CustomError { } } -fn test1<'i>(input: &mut Partial<&'i str>) -> PResult<&'i str, CustomError> { +fn test1<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str, CustomError> { //fix_error!(input, CustomError, tag!("abcd")) "abcd".parse_next(input) } -fn test2<'i>(input: &mut Partial<&'i str>) -> PResult<&'i str, CustomError> { +fn test2<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str, CustomError> { //terminated!(input, test1, fix_error!(CustomError, digit)) terminated(test1, digit).parse_next(input) } -fn test3<'i>(input: &mut Partial<&'i str>) -> PResult<&'i str, CustomError> { +fn test3<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str, CustomError> { test1 .verify(|s: &str| s.starts_with("abcd")) .parse_next(input) } #[cfg(feature = "alloc")] -fn test4<'i>(input: &mut Partial<&'i str>) -> PResult, CustomError> { +fn test4<'i>(input: &mut Partial<&'i str>) -> ModalResult, CustomError> { repeat(4, test1).parse_next(input) } diff --git a/tests/testsuite/issues.rs b/tests/testsuite/issues.rs index d6e3d8a4..e01c7723 100644 --- a/tests/testsuite/issues.rs +++ b/tests/testsuite/issues.rs @@ -17,7 +17,7 @@ struct Range { end: char, } -pub(crate) fn take_char(input: &mut &[u8]) -> PResult { +pub(crate) fn take_char(input: &mut &[u8]) -> ModalResult { if !input.is_empty() { Ok(input.next_token().unwrap() as char) } else { @@ -119,7 +119,7 @@ fn usize_length_bytes_issue() { use winnow::binary::be_u16; use winnow::binary::length_take; #[allow(clippy::type_complexity)] - let _: PResult<(Partial<&[u8]>, &[u8])> = + let _: ModalResult<(Partial<&[u8]>, &[u8])> = length_take(be_u16).parse_peek(Partial::new(b"012346")); } diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 9a2a92ea..86bd726b 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -1,11 +1,11 @@ #[cfg(test)] macro_rules! assert_parse( ($left: expr, $right: expr) => { - let res: winnow::error::PResult<_, winnow::error::InputError<_>> = $left; + let res: winnow::error::ModalResult<_, winnow::error::InputError<_>> = $left; snapbox::assert_data_eq!(snapbox::data::ToDebug::to_debug(&res), $right); }; ); -type TestResult = winnow::PResult>; +type TestResult = winnow::ModalResult>; automod::dir!("tests/testsuite"); diff --git a/tests/testsuite/multiline.rs b/tests/testsuite/multiline.rs index df5bb620..6be8e324 100644 --- a/tests/testsuite/multiline.rs +++ b/tests/testsuite/multiline.rs @@ -7,7 +7,7 @@ use winnow::{ prelude::*, }; -pub(crate) fn end_of_line<'i>(input: &mut &'i str) -> PResult<&'i str> { +pub(crate) fn end_of_line<'i>(input: &mut &'i str) -> ModalResult<&'i str> { if input.is_empty() { Ok(*input) } else { @@ -15,11 +15,11 @@ pub(crate) fn end_of_line<'i>(input: &mut &'i str) -> PResult<&'i str> { } } -pub(crate) fn read_line<'i>(input: &mut &'i str) -> PResult<&'i str> { +pub(crate) fn read_line<'i>(input: &mut &'i str) -> ModalResult<&'i str> { terminated(alphanumeric, end_of_line).parse_next(input) } -pub(crate) fn read_lines<'i>(input: &mut &'i str) -> PResult> { +pub(crate) fn read_lines<'i>(input: &mut &'i str) -> ModalResult> { repeat(0.., read_line).parse_next(input) } From f645c1c8787f31b14c230a3562eafe67dae593cb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Jan 2025 12:33:19 -0600 Subject: [PATCH 03/17] fix!: Add E:ParserError trait bounds This is preparation for moving callers from interacting with `ErrMode` to `ParserError` --- src/combinator/core.rs | 3 +++ src/combinator/debug/internals.rs | 8 +++++++- src/combinator/debug/mod.rs | 5 +++-- src/combinator/impls.rs | 16 ++++++++++------ src/parser.rs | 6 ++++-- src/stream/mod.rs | 1 + 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/combinator/core.rs b/src/combinator/core.rs index a1b58169..2f52e2f4 100644 --- a/src/combinator/core.rs +++ b/src/combinator/core.rs @@ -339,6 +339,7 @@ where pub fn todo(input: &mut Input) -> ModalResult where Input: Stream, + Error: ParserError, { #![allow(clippy::todo)] trace("todo", move |_input: &mut Input| { @@ -403,6 +404,7 @@ impl ParserIterator where F: Parser, I: Stream, + E: ParserError, { /// Returns the remaining input if parsing was successful, or the error if we encountered an error. pub fn finish(mut self) -> ModalResult<(I, ()), E> { @@ -417,6 +419,7 @@ impl core::iter::Iterator for &mut ParserIterator where F: Parser, I: Stream, + E: ParserError, { type Item = O; diff --git a/src/combinator/debug/internals.rs b/src/combinator/debug/internals.rs index c15fd33c..2785db03 100644 --- a/src/combinator/debug/internals.rs +++ b/src/combinator/debug/internals.rs @@ -3,6 +3,7 @@ use std::io::Write; use crate::error::ErrMode; +use crate::error::ParserError; use crate::stream::Stream; use crate::*; @@ -11,6 +12,7 @@ where P: Parser, I: Stream, D: std::fmt::Display, + E: ParserError, { parser: P, name: D, @@ -25,6 +27,7 @@ where P: Parser, I: Stream, D: std::fmt::Display, + E: ParserError, { #[inline(always)] pub(crate) fn new(parser: P, name: D) -> Self { @@ -44,6 +47,7 @@ where P: Parser, I: Stream, D: std::fmt::Display, + E: ParserError, { #[inline] fn parse_next(&mut self, i: &mut I) -> ModalResult { @@ -115,7 +119,9 @@ pub(crate) enum Severity { } impl Severity { - pub(crate) fn with_result(result: &Result>) -> Self { + pub(crate) fn with_result>( + result: &Result>, + ) -> Self { match result { Ok(_) => Self::Success, Err(ErrMode::Backtrack(_)) => Self::Backtrack, diff --git a/src/combinator/debug/mod.rs b/src/combinator/debug/mod.rs index 23fa51a7..4ad460c9 100644 --- a/src/combinator/debug/mod.rs +++ b/src/combinator/debug/mod.rs @@ -4,6 +4,7 @@ mod internals; use crate::error::ErrMode; +use crate::error::ParserError; use crate::stream::Stream; use crate::Parser; @@ -37,7 +38,7 @@ use crate::Parser; #[cfg_attr(not(feature = "debug"), allow(unused_variables))] #[cfg_attr(not(feature = "debug"), allow(unused_mut))] #[cfg_attr(not(feature = "debug"), inline(always))] -pub fn trace( +pub fn trace>( name: impl crate::lib::std::fmt::Display, parser: impl Parser, ) -> impl Parser { @@ -52,7 +53,7 @@ pub fn trace( } #[cfg_attr(not(feature = "debug"), allow(unused_variables))] -pub(crate) fn trace_result( +pub(crate) fn trace_result>( name: impl crate::lib::std::fmt::Display, res: &Result>, ) { diff --git a/src/combinator/impls.rs b/src/combinator/impls.rs index 58c1a780..2bfae7ac 100644 --- a/src/combinator/impls.rs +++ b/src/combinator/impls.rs @@ -69,6 +69,7 @@ where G: FnMut(O) -> Result, I: Stream, E: FromExternalError, + E: ParserError, { pub(crate) parser: F, pub(crate) map: G, @@ -85,6 +86,7 @@ where G: FnMut(O) -> Result, I: Stream, E: FromExternalError, + E: ParserError, { #[inline] fn parse_next(&mut self, input: &mut I) -> ModalResult { @@ -554,6 +556,7 @@ where F: Parser, I: Stream, E: AddContext, + E: ParserError, C: Clone + crate::lib::std::fmt::Debug, { pub(crate) parser: F, @@ -568,6 +571,7 @@ where F: Parser, I: Stream, E: AddContext, + E: ParserError, C: Clone + crate::lib::std::fmt::Debug, { #[inline] @@ -592,7 +596,7 @@ where R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { pub(crate) parser: P, pub(crate) recover: R, @@ -609,7 +613,7 @@ where R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { #[inline(always)] fn parse_next(&mut self, i: &mut I) -> ModalResult { @@ -629,7 +633,7 @@ where R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { loop { let token_start = i.checkpoint(); @@ -668,7 +672,7 @@ where R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { pub(crate) parser: P, pub(crate) recover: R, @@ -685,7 +689,7 @@ where R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { #[inline(always)] fn parse_next(&mut self, i: &mut I) -> ModalResult, E> { @@ -709,7 +713,7 @@ where R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { let token_start = i.checkpoint(); let mut err = match parser.parse_next(i) { diff --git a/src/parser.rs b/src/parser.rs index fcaa2bf7..0ce53436 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -544,6 +544,7 @@ pub trait Parser { G: FnMut(O) -> Result, I: Stream, E: FromExternalError, + E: ParserError, { impls::TryMap { parser: self, @@ -781,6 +782,7 @@ pub trait Parser { Self: core::marker::Sized, I: Stream, E: AddContext, + E: ParserError, C: Clone + crate::lib::std::fmt::Debug, { impls::Context { @@ -855,7 +857,7 @@ pub trait Parser { R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { impls::RetryAfter { parser: self, @@ -879,7 +881,7 @@ pub trait Parser { R: Parser, I: Stream, I: Recover, - E: FromRecoverableError, + E: ParserError + FromRecoverableError, { impls::ResumeAfter { parser: self, diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 9f9b3d46..3abcfcd1 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -1532,6 +1532,7 @@ where I: Stream, R: FromRecoverableError, R: crate::lib::std::fmt::Debug, + E: crate::error::ParserError, { fn record_err( &mut self, From 4ad8793a9aaf721bcd8e2d10fe2cf5f4cc612a06 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 23 Jan 2025 16:06:33 -0600 Subject: [PATCH 04/17] feat(error): Allow creating recoverable errors from ErrMode --- src/combinator/impls.rs | 2 +- src/error.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/combinator/impls.rs b/src/combinator/impls.rs index 2bfae7ac..a7af54f0 100644 --- a/src/combinator/impls.rs +++ b/src/combinator/impls.rs @@ -733,6 +733,6 @@ where } i.reset(&err_start); - err = err.map(|err| E::from_recoverable_error(&token_start, &err_start, i, err)); + err = FromRecoverableError::from_recoverable_error(&token_start, &err_start, i, err); Err(err) } diff --git a/src/error.rs b/src/error.rs index dadd36e7..0504f124 100644 --- a/src/error.rs +++ b/src/error.rs @@ -238,6 +238,22 @@ impl> AddContext for ErrMode { } } +#[cfg(feature = "unstable-recover")] +#[cfg(feature = "std")] +impl, E2> FromRecoverableError> + for ErrMode +{ + #[inline] + fn from_recoverable_error( + token_start: &::Checkpoint, + err_start: &::Checkpoint, + input: &I, + e: ErrMode, + ) -> Self { + e.map(|e| E1::from_recoverable_error(token_start, err_start, input, e)) + } +} + impl ErrMode> { /// Maps `ErrMode>` to `ErrMode>` with the given `F: T -> U` pub fn map_input(self, f: F) -> ErrMode> From 018cb66f669cb5b46a044198144f3c28bfcd89e2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 10 Jan 2025 13:04:29 -0600 Subject: [PATCH 05/17] feat(error): Add ability to ask if error is backtracking --- src/ascii/mod.rs | 2 +- src/combinator/branch.rs | 24 ++++++------ src/combinator/core.rs | 6 +-- src/combinator/multi.rs | 84 ++++++++++++++-------------------------- src/error.rs | 11 ++++++ 5 files changed, 55 insertions(+), 72 deletions(-) diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index dc67581b..f763c848 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -180,7 +180,7 @@ where { let res = match take_until(0.., ('\r', '\n')).parse_next(input) { Ok(slice) => slice, - Err(ErrMode::Backtrack(_)) => input.finish(), + Err(err) if err.is_backtrack() => input.finish(), Err(err) => { return Err(err); } diff --git a/src/combinator/branch.rs b/src/combinator/branch.rs index 048910c4..891ca54d 100644 --- a/src/combinator/branch.rs +++ b/src/combinator/branch.rs @@ -128,13 +128,13 @@ pub fn permutation, List: Permutation>( impl, P: Parser> Alt for [P; N] { fn choice(&mut self, input: &mut I) -> ModalResult { - let mut error: Option = None; + let mut error: Option> = None; let start = input.checkpoint(); for branch in self { input.reset(&start); match branch.parse_next(input) { - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { error = match error { Some(error) => Some(error.or(e)), None => Some(e), @@ -145,7 +145,7 @@ impl, P: Parser> Alt Err(ErrMode::Backtrack(e.append(input, &start, ErrorKind::Alt))), + Some(e) => Err(e.append(input, &start, ErrorKind::Alt)), None => Err(ErrMode::assert(input, "`alt` needs at least one parser")), } } @@ -153,13 +153,13 @@ impl, P: Parser> Alt, P: Parser> Alt for &mut [P] { fn choice(&mut self, input: &mut I) -> ModalResult { - let mut error: Option = None; + let mut error: Option> = None; let start = input.checkpoint(); for branch in self.iter_mut() { input.reset(&start); match branch.parse_next(input) { - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { error = match error { Some(error) => Some(error.or(e)), None => Some(e), @@ -170,7 +170,7 @@ impl, P: Parser> Alt for &mut } match error { - Some(e) => Err(ErrMode::Backtrack(e.append(input, &start, ErrorKind::Alt))), + Some(e) => Err(e.append(input, &start, ErrorKind::Alt)), None => Err(ErrMode::assert(input, "`alt` needs at least one parser")), } } @@ -201,7 +201,7 @@ macro_rules! alt_trait_impl( fn choice(&mut self, input: &mut I) -> ModalResult { let start = input.checkpoint(); match self.0.parse_next(input) { - Err(ErrMode::Backtrack(e)) => alt_trait_inner!(1, self, input, start, e, $($id)+), + Err(e) if e.is_backtrack() => alt_trait_inner!(1, self, input, start, e, $($id)+), res => res, } } @@ -237,7 +237,7 @@ macro_rules! alt_trait_inner( ($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident $($id:ident)+) => ({ $input.reset(&$start); match $self.$it.parse_next($input) { - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { let err = $err.or(e); succ!($it, alt_trait_inner!($self, $input, $start, err, $($id)+)) } @@ -245,7 +245,7 @@ macro_rules! alt_trait_inner( } }); ($it:tt, $self:expr, $input:expr, $start:ident, $err:expr, $head:ident) => ({ - Err(ErrMode::Backtrack($err.append($input, &$start, ErrorKind::Alt))) + Err($err.append($input, &$start, ErrorKind::Alt)) }); ); @@ -289,7 +289,7 @@ macro_rules! permutation_trait_impl( let mut res = ($(Option::<$ty>::None),+); loop { - let mut err: Option = None; + let mut err: Option> = None; let start = input.checkpoint(); permutation_trait_inner!(0, self, input, start, res, err, $($name)+); @@ -298,7 +298,7 @@ macro_rules! permutation_trait_impl( if let Some(err) = err { // There are remaining parsers, and all errored on the remaining input input.reset(&start); - return Err(ErrMode::Backtrack(err.append(input, &start, ErrorKind::Alt))); + return Err(err.append(input, &start, ErrorKind::Alt)); } // All parsers were applied @@ -321,7 +321,7 @@ macro_rules! permutation_trait_inner( $res.$it = Some(o); continue; } - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { $err = Some(match $err { Some(err) => err.or(e), None => e, diff --git a/src/combinator/core.rs b/src/combinator/core.rs index 2f52e2f4..52740289 100644 --- a/src/combinator/core.rs +++ b/src/combinator/core.rs @@ -56,7 +56,7 @@ where let start = input.checkpoint(); match parser.parse_next(input) { Ok(o) => Ok(Some(o)), - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); Ok(None) } @@ -225,7 +225,7 @@ where input.reset(&start); match res { Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Not)), - Err(ErrMode::Backtrack(_)) => Ok(()), + Err(e) if e.is_backtrack() => Ok(()), Err(e) => Err(e), } }) @@ -432,7 +432,7 @@ where self.state = Some(State::Running); Some(o) } - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { self.input.reset(&start); self.state = Some(State::Done); None diff --git a/src/combinator/multi.rs b/src/combinator/multi.rs index 1968dd5f..8a106d3b 100644 --- a/src/combinator/multi.rs +++ b/src/combinator/multi.rs @@ -471,7 +471,7 @@ where let start = i.checkpoint(); let len = i.eof_offset(); match f.parse_next(i) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { i.reset(&start); return Ok(acc); } @@ -506,7 +506,7 @@ where let start = i.checkpoint(); let len = i.eof_offset(); match f.parse_next(i) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { i.reset(&start); return Ok(acc); } @@ -590,13 +590,9 @@ where res.accumulate(value); } - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { if count < min { - return Err(ErrMode::Backtrack(e.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(e.append(input, &start, ErrorKind::Repeat)); } else { input.reset(&start); return Ok(res); @@ -690,7 +686,7 @@ where let len = i.eof_offset(); match g.parse_next(i) { Ok(o) => return Ok((res, o)), - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { i.reset(&start); match f.parse_next(i) { Err(e) => return Err(e.append(i, &start, ErrorKind::Repeat)), @@ -748,9 +744,9 @@ where let len = i.eof_offset(); match g.parse_next(i) { Ok(o) => return Ok((res, o)), - Err(ErrMode::Backtrack(err)) => { + Err(err) if err.is_backtrack() => { if count == max { - return Err(ErrMode::Backtrack(err)); + return Err(err); } i.reset(&start); match f.parse_next(i) { @@ -923,7 +919,7 @@ where let start = input.checkpoint(); match parser.parse_next(input) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); return Ok(acc); } @@ -937,7 +933,7 @@ where let start = input.checkpoint(); let len = input.eof_offset(); match separator.parse_next(input) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); return Ok(acc); } @@ -952,7 +948,7 @@ where } match parser.parse_next(input) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); return Ok(acc); } @@ -992,7 +988,7 @@ where let start = input.checkpoint(); let len = input.eof_offset(); match separator.parse_next(input) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); return Ok(acc); } @@ -1007,7 +1003,7 @@ where } match parser.parse_next(input) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); return Ok(acc); } @@ -1106,16 +1102,12 @@ where let start = input.checkpoint(); match parser.parse_next(input) { - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { if min == 0 { input.reset(&start); return Ok(acc); } else { - return Err(ErrMode::Backtrack(e.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(e.append(input, &start, ErrorKind::Repeat)); } } Err(e) => return Err(e), @@ -1128,13 +1120,9 @@ where let start = input.checkpoint(); let len = input.eof_offset(); match separator.parse_next(input) { - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { if index < min { - return Err(ErrMode::Backtrack(e.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(e.append(input, &start, ErrorKind::Repeat)); } else { input.reset(&start); return Ok(acc); @@ -1153,13 +1141,9 @@ where } match parser.parse_next(input) { - Err(ErrMode::Backtrack(e)) => { + Err(e) if e.is_backtrack() => { if index < min { - return Err(ErrMode::Backtrack(e.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(e.append(input, &start, ErrorKind::Repeat)); } else { input.reset(&start); return Ok(acc); @@ -1219,7 +1203,7 @@ where let start = i.checkpoint(); let len = i.eof_offset(); match sep.parse_next(i) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { i.reset(&start); return Ok(ol); } @@ -1231,7 +1215,7 @@ where } match parser.parse_next(i) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { i.reset(&start); return Ok(ol); } @@ -1377,7 +1361,7 @@ where res = g(res, o); } - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); return Ok(res); } @@ -1412,7 +1396,7 @@ where let start = input.checkpoint(); let len = input.eof_offset(); match f.parse_next(input) { - Err(ErrMode::Backtrack(_)) => { + Err(e) if e.is_backtrack() => { input.reset(&start); break; } @@ -1475,13 +1459,9 @@ where acc = fold(acc, value); } //FInputXMError: handle failure properly - Err(ErrMode::Backtrack(err)) => { + Err(err) if err.is_backtrack() => { if count < min { - return Err(ErrMode::Backtrack(err.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(err.append(input, &start, ErrorKind::Repeat)); } else { input.reset(&start); break; @@ -1540,13 +1520,9 @@ where acc = tmp; } //FInputXMError: handle failure properly - Err(ErrMode::Backtrack(err)) => { + Err(err) if err.is_backtrack() => { if count < min { - return Err(ErrMode::Backtrack(err.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(err.append(input, &start, ErrorKind::Repeat)); } else { input.reset(&start); break; @@ -1607,13 +1583,9 @@ where } } //FInputXMError: handle failure properly - Err(ErrMode::Backtrack(err)) => { + Err(err) if err.is_backtrack() => { if count < min { - return Err(ErrMode::Backtrack(err.append( - input, - &start, - ErrorKind::Repeat, - ))); + return Err(err.append(input, &start, ErrorKind::Repeat)); } else { input.reset(&start); break; diff --git a/src/error.rs b/src/error.rs index 0504f124..169dd069 100644 --- a/src/error.rs +++ b/src/error.rs @@ -209,6 +209,11 @@ impl> ParserError for ErrMode { (ErrMode::Cut(e), _) | (_, ErrMode::Cut(e)) => ErrMode::Cut(e), } } + + #[inline(always)] + fn is_backtrack(&self) -> bool { + matches!(self, ErrMode::Backtrack(_)) + } } impl ErrorConvert> for ErrMode @@ -332,6 +337,12 @@ pub trait ParserError: Sized { fn or(self, other: Self) -> Self { other } + + /// Is backtracking and trying new parse branches allowed? + #[inline(always)] + fn is_backtrack(&self) -> bool { + true + } } /// Used by [`Parser::context`] to add custom data to error while backtracking From 7c55884709e0f53facfc207803b03cdb157d0c10 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 10 Jan 2025 13:17:41 -0600 Subject: [PATCH 06/17] feat(error): Abstract creation of ErrMode::Incomplete --- src/ascii/mod.rs | 8 ++++---- src/binary/bits/mod.rs | 2 +- src/binary/mod.rs | 6 +++--- src/error.rs | 18 ++++++++++++++++++ src/token/mod.rs | 21 +++++++++++---------- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index f763c848..2066c6b1 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -190,7 +190,7 @@ where match comp { CompareResult::Ok(_) => {} CompareResult::Incomplete if PARTIAL && input.is_partial() => { - return Err(ErrMode::Incomplete(Needed::Unknown)); + return Err(ErrMode::incomplete(input, Needed::Unknown)); } CompareResult::Incomplete | CompareResult::Error => { let e: ErrorKind = ErrorKind::Literal; @@ -1344,7 +1344,7 @@ where && invalid_offset == input.eof_offset() { // Only the next byte is guaranteed required - return Err(ErrMode::Incomplete(Needed::new(1))); + return Err(ErrMode::incomplete(input, Needed::new(1))); } else { invalid_offset } @@ -1688,7 +1688,7 @@ where } if PARTIAL && input.is_partial() { - Err(ErrMode::Incomplete(Needed::Unknown)) + Err(ErrMode::incomplete(input, Needed::Unknown)) } else { input.reset(&start); Ok(input.finish()) @@ -1845,7 +1845,7 @@ where } if PARTIAL && input.is_partial() { - Err(ErrMode::Incomplete(Needed::Unknown)) + Err(ErrMode::incomplete(input, Needed::Unknown)) } else { Ok(res) } diff --git a/src/binary/bits/mod.rs b/src/binary/bits/mod.rs index 7965d7ce..52601a27 100644 --- a/src/binary/bits/mod.rs +++ b/src/binary/bits/mod.rs @@ -216,7 +216,7 @@ where let (mut input, bit_offset) = bit_input.clone(); if input.eof_offset() * BYTE < count + bit_offset { if PARTIAL && input.is_partial() { - Err(ErrMode::Incomplete(Needed::new(count))) + Err(ErrMode::incomplete(bit_input, Needed::new(count))) } else { Err(ErrMode::from_error_kind( &(input, bit_offset), diff --git a/src/binary/mod.rs b/src/binary/mod.rs index d7152624..31e57b5c 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -310,7 +310,7 @@ where Ok(res) } Err(e) if ::is_partial_supported() && input.is_partial() => { - Err(ErrMode::Incomplete(e)) + Err(ErrMode::incomplete(input, e)) } Err(_needed) => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)), } @@ -898,7 +898,7 @@ where Ok(res) } Err(e) if ::is_partial_supported() && input.is_partial() => { - Err(ErrMode::Incomplete(e)) + Err(ErrMode::incomplete(input, e)) } Err(_needed) => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)), } @@ -1273,7 +1273,7 @@ where { input.next_token().ok_or_else(|| { if PARTIAL && input.is_partial() { - ErrMode::Incomplete(Needed::new(1)) + ErrMode::incomplete(input, Needed::new(1)) } else { ErrMode::from_error_kind(input, ErrorKind::Token) } diff --git a/src/error.rs b/src/error.rs index 169dd069..f676c1ee 100644 --- a/src/error.rs +++ b/src/error.rs @@ -194,6 +194,11 @@ impl> ParserError for ErrMode { ErrMode::Cut(E::assert(input, message)) } + #[inline(always)] + fn incomplete(_input: &I, needed: Needed) -> Self { + ErrMode::Incomplete(needed) + } + #[inline] fn append(self, input: &I, token_start: &::Checkpoint, kind: ErrorKind) -> Self { match self { @@ -315,6 +320,19 @@ pub trait ParserError: Sized { Self::from_error_kind(input, ErrorKind::Assert) } + /// There was not enough data to determine the appropriate action + /// + /// More data needs to be buffered before retrying the parse. + /// + /// This must only be set when the [`Stream`] is [partial][`crate::stream::StreamIsPartial`], like with + /// [`Partial`][crate::Partial] + /// + /// Convert this into an `Backtrack` with [`Parser::complete_err`] + #[inline(always)] + fn incomplete(input: &I, _needed: Needed) -> Self { + Self::from_error_kind(input, ErrorKind::Complete) + } + /// Like [`ParserError::from_error_kind`] but merges it with the existing error. /// /// This is useful when backtracking through a parse tree, accumulating error context on the diff --git a/src/token/mod.rs b/src/token/mod.rs index af96713d..b386f2b6 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -79,7 +79,7 @@ where { input.next_token().ok_or_else(|| { if PARTIAL && input.is_partial() { - ErrMode::Incomplete(Needed::new(1)) + ErrMode::incomplete(input, Needed::new(1)) } else { ErrMode::from_error_kind(input, ErrorKind::Token) } @@ -191,7 +191,8 @@ where let literal_len = t.slice_len(); match i.compare(t) { CompareResult::Ok(len) => Ok(i.next_slice(len)), - CompareResult::Incomplete if PARTIAL && i.is_partial() => Err(ErrMode::Incomplete( + CompareResult::Incomplete if PARTIAL && i.is_partial() => Err(ErrMode::incomplete( + i, Needed::new(literal_len - i.eof_offset()), )), CompareResult::Incomplete | CompareResult::Error => { @@ -538,7 +539,7 @@ where let offset = match input.offset_for(predicate) { Some(offset) => offset, None if PARTIAL && input.is_partial() => { - return Err(ErrMode::Incomplete(Needed::new(1))); + return Err(ErrMode::incomplete(input, Needed::new(1))); } None => input.eof_offset(), }; @@ -556,7 +557,7 @@ where let offset = match input.offset_for(predicate) { Some(offset) => offset, None if PARTIAL && input.is_partial() => { - return Err(ErrMode::Incomplete(Needed::new(1))); + return Err(ErrMode::incomplete(input, Needed::new(1))); } None => input.eof_offset(), }; @@ -609,7 +610,7 @@ where } else { 1 }; - Err(ErrMode::Incomplete(Needed::new(needed))) + Err(ErrMode::incomplete(input, Needed::new(needed))) } } else { if m <= final_count { @@ -819,7 +820,7 @@ where { match i.offset_at(c) { Ok(offset) => Ok(i.next_slice(offset)), - Err(e) if PARTIAL && i.is_partial() => Err(ErrMode::Incomplete(e)), + Err(e) if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, e)), Err(_needed) => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), } } @@ -970,7 +971,7 @@ where { match i.find_slice(t) { Some(range) => Ok(i.next_slice(range.start)), - None if PARTIAL && i.is_partial() => Err(ErrMode::Incomplete(Needed::Unknown)), + None if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, Needed::Unknown)), None => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), } } @@ -984,7 +985,7 @@ where I: Stream + FindSlice, { match i.find_slice(t) { - None if PARTIAL && i.is_partial() => Err(ErrMode::Incomplete(Needed::Unknown)), + None if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, Needed::Unknown)), None => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), Some(range) => { if range.start == 0 { @@ -1019,7 +1020,7 @@ where let end_offset = i.offset_at(end).unwrap_or_else(|_err| i.eof_offset()); if start_offset.map(|s| range.start < s).unwrap_or(true) { if PARTIAL && i.is_partial() { - return Err(ErrMode::Incomplete(Needed::Unknown)); + return Err(ErrMode::incomplete(i, Needed::Unknown)); } else { return Err(ErrMode::from_error_kind(i, ErrorKind::Slice)); } @@ -1029,7 +1030,7 @@ where } Ok(i.next_slice(range.start)) } - None if PARTIAL && i.is_partial() => Err(ErrMode::Incomplete(Needed::Unknown)), + None if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, Needed::Unknown)), None => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), } } From 3d88117e20678592c4adb756abb593497a2433d6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 10 Jan 2025 13:30:37 -0600 Subject: [PATCH 07/17] feat(error): Abstract over getting Needed from ErrMode --- src/binary/bits/mod.rs | 26 +++++++++++++++----------- src/combinator/debug/internals.rs | 11 ++++------- src/combinator/debug/mod.rs | 3 +-- src/combinator/impls.rs | 11 ++++++----- src/error.rs | 25 +++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/binary/bits/mod.rs b/src/binary/bits/mod.rs index 52601a27..3e8e7489 100644 --- a/src/binary/bits/mod.rs +++ b/src/binary/bits/mod.rs @@ -68,8 +68,10 @@ where *input = rest; Ok(result) } - Err(ErrMode::Incomplete(n)) => Err(ErrMode::Incomplete(n.map(|u| u.get() / BYTE + 1))), - Err(e) => Err(ErrorConvert::convert(e)), + Err(e) => match e.into_needed() { + Ok(n) => Err(ErrMode::incomplete(input, n.map(|u| u.get() / BYTE + 1))), + Err(e) => Err(ErrorConvert::convert(e)), + }, } }) } @@ -131,15 +133,17 @@ where *bit_input = (input, 0); Ok(res) } - Err(ErrMode::Incomplete(Needed::Unknown)) => Err(ErrMode::Incomplete(Needed::Unknown)), - Err(ErrMode::Incomplete(Needed::Size(sz))) => Err(match sz.get().checked_mul(BYTE) { - Some(v) => ErrMode::Incomplete(Needed::new(v)), - None => ErrMode::assert( - bit_input, - "overflow in turning needed bytes into needed bits", - ), - }), - Err(e) => Err(ErrorConvert::convert(e)), + Err(e) => match e.into_needed() { + Ok(Needed::Unknown) => Err(ErrMode::incomplete(bit_input, Needed::Unknown)), + Ok(Needed::Size(sz)) => Err(match sz.get().checked_mul(BYTE) { + Some(v) => ErrMode::incomplete(bit_input, Needed::new(v)), + None => ErrMode::assert( + bit_input, + "overflow in turning needed bytes into needed bits", + ), + }), + Err(e) => Err(ErrorConvert::convert(e)), + }, } }) } diff --git a/src/combinator/debug/internals.rs b/src/combinator/debug/internals.rs index 2785db03..d1ab0de6 100644 --- a/src/combinator/debug/internals.rs +++ b/src/combinator/debug/internals.rs @@ -2,7 +2,6 @@ use std::io::Write; -use crate::error::ErrMode; use crate::error::ParserError; use crate::stream::Stream; use crate::*; @@ -119,14 +118,12 @@ pub(crate) enum Severity { } impl Severity { - pub(crate) fn with_result>( - result: &Result>, - ) -> Self { + pub(crate) fn with_result>(result: &Result) -> Self { match result { Ok(_) => Self::Success, - Err(ErrMode::Backtrack(_)) => Self::Backtrack, - Err(ErrMode::Cut(_)) => Self::Cut, - Err(ErrMode::Incomplete(_)) => Self::Incomplete, + Err(e) if e.is_backtrack() => Self::Backtrack, + Err(e) if e.is_needed() => Self::Incomplete, + _ => Self::Cut, } } } diff --git a/src/combinator/debug/mod.rs b/src/combinator/debug/mod.rs index 4ad460c9..8983d19e 100644 --- a/src/combinator/debug/mod.rs +++ b/src/combinator/debug/mod.rs @@ -3,7 +3,6 @@ #[cfg(feature = "debug")] mod internals; -use crate::error::ErrMode; use crate::error::ParserError; use crate::stream::Stream; use crate::Parser; @@ -55,7 +54,7 @@ pub fn trace>( #[cfg_attr(not(feature = "debug"), allow(unused_variables))] pub(crate) fn trace_result>( name: impl crate::lib::std::fmt::Display, - res: &Result>, + res: &Result, ) { #[cfg(feature = "debug")] { diff --git a/src/combinator/impls.rs b/src/combinator/impls.rs index a7af54f0..a2958618 100644 --- a/src/combinator/impls.rs +++ b/src/combinator/impls.rs @@ -255,9 +255,10 @@ where fn parse_next(&mut self, input: &mut I) -> ModalResult { trace("complete_err", |input: &mut I| { match (self.p).parse_next(input) { - Err(ErrMode::Incomplete(_)) => { - Err(ErrMode::from_error_kind(input, ErrorKind::Complete)) - } + Err(err) => match err.into_needed() { + Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Complete)), + Err(err) => Err(err), + }, rest => rest, } }) @@ -641,7 +642,7 @@ where Ok(o) => { return Ok(o); } - Err(ErrMode::Incomplete(e)) => return Err(ErrMode::Incomplete(e)), + Err(e) if e.is_needed() => return Err(e), Err(err) => err, }; let err_start = i.checkpoint(); @@ -720,7 +721,7 @@ where Ok(o) => { return Ok(Some(o)); } - Err(ErrMode::Incomplete(e)) => return Err(ErrMode::Incomplete(e)), + Err(e) if e.is_needed() => return Err(e), Err(err) => err, }; let err_start = i.checkpoint(); diff --git a/src/error.rs b/src/error.rs index f676c1ee..eeb0345b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -219,6 +219,19 @@ impl> ParserError for ErrMode { fn is_backtrack(&self) -> bool { matches!(self, ErrMode::Backtrack(_)) } + + #[inline(always)] + fn is_needed(&self) -> bool { + matches!(self, ErrMode::Incomplete(_)) + } + + #[inline(always)] + fn into_needed(self) -> Result { + match self { + ErrMode::Incomplete(needed) => Ok(needed), + err => Err(err), + } + } } impl ErrorConvert> for ErrMode @@ -361,6 +374,18 @@ pub trait ParserError: Sized { fn is_backtrack(&self) -> bool { true } + + /// Is more data [`Needed`] + #[inline(always)] + fn is_needed(&self) -> bool { + false + } + + /// Extract the [`Needed`] data, if present + #[inline(always)] + fn into_needed(self) -> Result { + Err(self) + } } /// Used by [`Parser::context`] to add custom data to error while backtracking From 2ca57cb80a5c727b617e9dbef6148f1ab36fe991 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 10 Jan 2025 15:24:18 -0600 Subject: [PATCH 08/17] fix(error)!: Move ErrMode::cut, ErrMode::backtrack to ModalError trait --- src/combinator/core.rs | 2 +- src/error.rs | 40 ++++++++++++++++++++++++---------------- src/lib.rs | 1 + 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/combinator/core.rs b/src/combinator/core.rs index 52740289..7cd38aea 100644 --- a/src/combinator/core.rs +++ b/src/combinator/core.rs @@ -1,5 +1,5 @@ use crate::combinator::trace; -use crate::error::{ErrMode, ErrorKind, ParserError}; +use crate::error::{ErrMode, ErrorKind, ModalError, ParserError}; use crate::stream::Stream; use crate::*; diff --git a/src/error.rs b/src/error.rs index eeb0345b..f888d7a2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -131,22 +131,6 @@ impl ErrMode { matches!(self, ErrMode::Incomplete(_)) } - /// Prevent backtracking, bubbling the error up to the top - pub fn cut(self) -> Self { - match self { - ErrMode::Backtrack(e) => ErrMode::Cut(e), - rest => rest, - } - } - - /// Enable backtracking support - pub fn backtrack(self) -> Self { - match self { - ErrMode::Cut(e) => ErrMode::Backtrack(e), - rest => rest, - } - } - /// Applies the given function to the inner error pub fn map(self, f: F) -> ErrMode where @@ -234,6 +218,22 @@ impl> ParserError for ErrMode { } } +impl ModalError for ErrMode { + fn cut(self) -> Self { + match self { + ErrMode::Backtrack(e) => ErrMode::Cut(e), + rest => rest, + } + } + + fn backtrack(self) -> Self { + match self { + ErrMode::Cut(e) => ErrMode::Backtrack(e), + rest => rest, + } + } +} + impl ErrorConvert> for ErrMode where E1: ErrorConvert, @@ -388,6 +388,14 @@ pub trait ParserError: Sized { } } +/// Manipulate the how parsers respond to this error +pub trait ModalError { + /// Prevent backtracking, bubbling the error up to the top + fn cut(self) -> Self; + /// Enable backtracking support + fn backtrack(self) -> Self; +} + /// Used by [`Parser::context`] to add custom data to error while backtracking /// /// May be implemented multiple times for different kinds of context. diff --git a/src/lib.rs b/src/lib.rs index 4ff675fd..635b9407 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,6 +144,7 @@ pub mod _tutorial; /// } /// ``` pub mod prelude { + pub use crate::error::ModalError as _; pub use crate::error::ParserError as _; pub use crate::stream::AsChar as _; pub use crate::stream::ContainsToken as _; From 85b3b64260ab8bd801a4882e167942890b327590 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 22 Jan 2025 13:17:31 -0600 Subject: [PATCH 09/17] feat(error)!: Abstract over getting error in ErrMode --- examples/custom_error.rs | 6 +++ src/combinator/tests.rs | 18 +++++++++ src/error.rs | 67 ++++++++++++++++++++++++++------ src/parser.rs | 12 +++--- tests/testsuite/custom_errors.rs | 6 +++ 5 files changed, 91 insertions(+), 18 deletions(-) diff --git a/examples/custom_error.rs b/examples/custom_error.rs index 5300a692..5546668b 100644 --- a/examples/custom_error.rs +++ b/examples/custom_error.rs @@ -18,9 +18,15 @@ pub enum CustomError { } impl ParserError for CustomError { + type Inner = Self; + fn from_error_kind(input: &I, kind: ErrorKind) -> Self { CustomError::Winnow(input.clone(), kind) } + + fn into_inner(self) -> Result { + Ok(self) + } } impl AddContext for CustomError { diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index b0611f1c..7196ab86 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -119,9 +119,15 @@ impl From for CustomError { } impl ParserError for CustomError { + type Inner = Self; + fn from_error_kind(_: &I, _: ErrorKind) -> Self { CustomError } + + fn into_inner(self) -> Result { + Ok(self) + } } struct CustomError; @@ -1322,6 +1328,8 @@ fn alt_test() { #[cfg(feature = "alloc")] impl ParserError for ErrorStr { + type Inner = Self; + fn from_error_kind(input: &I, kind: ErrorKind) -> Self { ErrorStr(format!("custom error message: ({input:?}, {kind:?})")) } @@ -1331,6 +1339,10 @@ fn alt_test() { "custom error message: ({input:?}, {kind:?}) - {self:?}" )) } + + fn into_inner(self) -> Result { + Ok(self) + } } fn work<'i>(input: &mut &'i [u8]) -> ModalResult<&'i [u8], ErrorStr> { @@ -3457,9 +3469,15 @@ impl From<(I, ErrorKind)> for NilError { } impl ParserError for NilError { + type Inner = Self; + fn from_error_kind(_: &I, _: ErrorKind) -> NilError { NilError } + + fn into_inner(self) -> Result { + Ok(self) + } } #[test] diff --git a/src/error.rs b/src/error.rs index f888d7a2..98668f69 100644 --- a/src/error.rs +++ b/src/error.rs @@ -39,7 +39,7 @@ use crate::Parser; /// /// When integrating into the result of the application, see /// - [`Parser::parse`] -/// - [`ErrMode::into_inner`] +/// - [`ParserError::into_inner`] pub type ModalResult = Result>; /// Deprecated, replaced with [`ModalResult`] @@ -151,20 +151,11 @@ impl ErrMode { { ErrorConvert::convert(self) } - - /// Unwrap the mode, returning the underlying error - /// - /// Returns `None` for [`ErrMode::Incomplete`] - #[inline(always)] - pub fn into_inner(self) -> Option { - match self { - ErrMode::Backtrack(e) | ErrMode::Cut(e) => Some(e), - ErrMode::Incomplete(_) => None, - } - } } impl> ParserError for ErrMode { + type Inner = E; + #[inline(always)] fn from_error_kind(input: &I, kind: ErrorKind) -> Self { ErrMode::Backtrack(E::from_error_kind(input, kind)) @@ -204,6 +195,15 @@ impl> ParserError for ErrMode { matches!(self, ErrMode::Backtrack(_)) } + /// Unwrap the mode, returning the underlying error + #[inline(always)] + fn into_inner(self) -> Result { + match self { + ErrMode::Backtrack(e) | ErrMode::Cut(e) => Ok(e), + err @ ErrMode::Incomplete(_) => Err(err), + } + } + #[inline(always)] fn is_needed(&self) -> bool { matches!(self, ErrMode::Incomplete(_)) @@ -318,6 +318,11 @@ where /// It provides methods to create an error from some combinators, /// and combine existing errors in combinators like `alt`. pub trait ParserError: Sized { + /// Generally, `Self` + /// + /// Mostly used for [`ErrMode`] + type Inner; + /// Creates an error from the input position and an [`ErrorKind`] fn from_error_kind(input: &I, kind: ErrorKind) -> Self; @@ -375,6 +380,9 @@ pub trait ParserError: Sized { true } + /// Unwrap the mode, returning the underlying error, if present + fn into_inner(self) -> Result; + /// Is more data [`Needed`] #[inline(always)] fn is_needed(&self) -> bool { @@ -490,6 +498,8 @@ where } impl ParserError for InputError { + type Inner = Self; + #[inline] fn from_error_kind(input: &I, kind: ErrorKind) -> Self { Self { @@ -497,6 +507,11 @@ impl ParserError for InputError { kind, } } + + #[inline(always)] + fn into_inner(self) -> Result { + Ok(self) + } } impl AddContext for InputError {} @@ -559,8 +574,15 @@ impl std::error::E } impl ParserError for () { + type Inner = Self; + #[inline] fn from_error_kind(_: &I, _: ErrorKind) -> Self {} + + #[inline(always)] + fn into_inner(self) -> Result { + Ok(self) + } } impl AddContext for () {} @@ -643,10 +665,17 @@ impl Default for ContextError { } impl ParserError for ContextError { + type Inner = Self; + #[inline] fn from_error_kind(_input: &I, _kind: ErrorKind) -> Self { Self::new() } + + #[inline(always)] + fn into_inner(self) -> Result { + Ok(self) + } } impl AddContext for ContextError { @@ -955,6 +984,8 @@ impl ParserError for TreeError where I: Stream + Clone, { + type Inner = Self; + fn from_error_kind(input: &I, kind: ErrorKind) -> Self { TreeError::Base(TreeErrorBase { input: input.clone(), @@ -991,6 +1022,11 @@ where (first, second) => TreeError::Alt(vec![first, second]), } } + + #[inline(always)] + fn into_inner(self) -> Result { + Ok(self) + } } #[cfg(feature = "std")] @@ -1187,10 +1223,17 @@ impl ErrorKind { } impl ParserError for ErrorKind { + type Inner = Self; + #[inline] fn from_error_kind(_input: &I, kind: ErrorKind) -> Self { kind } + + #[inline(always)] + fn into_inner(self) -> Result { + Ok(self) + } } impl AddContext for ErrorKind {} diff --git a/src/parser.rs b/src/parser.rs index 0ce53436..6625dc97 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -65,9 +65,9 @@ pub trait Parser { let (o, _) = (self.by_ref(), crate::combinator::eof) .parse_next(&mut input) .map_err(|e| { - let e = e - .into_inner() - .expect("complete parsers should not report `ErrMode::Incomplete(_)`"); + let e = e.into_inner().unwrap_or_else(|_err| { + panic!("complete parsers should not report `ErrMode::Incomplete(_)`") + }); ParseError::new(input, start, e) })?; Ok(o) @@ -1264,9 +1264,9 @@ where let (o, err) = match result { Ok((o, _)) => (Some(o), None), Err(err) => { - let err = err - .into_inner() - .expect("complete parsers should not report `ErrMode::Incomplete(_)`"); + let err = err.into_inner().unwrap_or_else(|_err| { + panic!("complete parsers should not report `ErrMode::Incomplete(_)`") + }); let err_start = input.checkpoint(); let err = R::from_recoverable_error(&start_token, &err_start, &input, err); (None, Some(err)) diff --git a/tests/testsuite/custom_errors.rs b/tests/testsuite/custom_errors.rs index af965754..9c108c86 100644 --- a/tests/testsuite/custom_errors.rs +++ b/tests/testsuite/custom_errors.rs @@ -19,6 +19,8 @@ impl<'a> From<(&'a str, ErrorKind)> for CustomError { } impl<'a> ParserError> for CustomError { + type Inner = Self; + fn from_error_kind(_: &Partial<&'a str>, kind: ErrorKind) -> Self { CustomError(format!("error code was: {kind:?}")) } @@ -31,6 +33,10 @@ impl<'a> ParserError> for CustomError { ) -> Self { CustomError(format!("{self:?}\nerror code was: {kind:?}")) } + + fn into_inner(self) -> Result { + Ok(self) + } } fn test1<'i>(input: &mut Partial<&'i str>) -> ModalResult<&'i str, CustomError> { From e42f604bd2edb3fe30916920d22382b84ee7028a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 8 Jan 2025 12:48:12 -0600 Subject: [PATCH 10/17] feat(error): Add `Result` alias --- src/error.rs | 7 +++++++ src/lib.rs | 1 + 2 files changed, 8 insertions(+) diff --git a/src/error.rs b/src/error.rs index 98668f69..051e5c2f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,6 +30,13 @@ use crate::stream::Stream; #[allow(unused_imports)] // Here for intra-doc links use crate::Parser; +/// By default, the error type (`E`) is [`ContextError`]. +/// +/// When integrating into the result of the application, see +/// - [`Parser::parse`] +/// - [`ParserError::into_inner`] +pub type Result = core::result::Result; + /// [Modal error reporting][ErrMode] for [`Parser::parse_next`] /// /// - `Ok(O)` is the parsed value diff --git a/src/lib.rs b/src/lib.rs index 635b9407..6ec807fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,6 +165,7 @@ pub mod prelude { pub use error::ModalResult; #[allow(deprecated)] pub use error::PResult; +pub use error::Result; pub use parser::*; pub use stream::BStr; pub use stream::Bytes; From 599fecf8ffdaf0b10d94e125ce10c4181ce51e75 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 23 Jan 2025 10:05:10 -0600 Subject: [PATCH 11/17] refactor: Don't access ErrMode directly --- benches/number.rs | 3 +- fuzz/fuzz_targets/fuzz_arithmetic.rs | 3 +- src/_tutorial/chapter_2.rs | 10 ++--- src/ascii/mod.rs | 23 +++++----- src/binary/bits/mod.rs | 19 +++++---- src/binary/mod.rs | 13 +++--- src/combinator/branch.rs | 10 ++++- src/combinator/core.rs | 6 +-- src/combinator/impls.rs | 8 ++-- src/combinator/multi.rs | 64 ++++++++++++++++++---------- src/combinator/tests.rs | 2 +- src/token/mod.rs | 49 +++++++++++---------- src/token/tests.rs | 4 +- 13 files changed, 118 insertions(+), 96 deletions(-) diff --git a/benches/number.rs b/benches/number.rs index cb70ec2d..c0ae6266 100644 --- a/benches/number.rs +++ b/benches/number.rs @@ -5,7 +5,6 @@ use criterion::Criterion; use winnow::ascii::float; use winnow::binary::be_u64; -use winnow::error::ErrMode; use winnow::error::ErrorKind; use winnow::error::InputError; use winnow::error::ParserError; @@ -52,7 +51,7 @@ fn float_str(c: &mut Criterion) { fn std_float(input: &mut &[u8]) -> ModalResult { match input.parse_slice() { Some(n) => Ok(n), - None => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)), + None => Err(ParserError::from_error_kind(input, ErrorKind::Slice)), } } diff --git a/fuzz/fuzz_targets/fuzz_arithmetic.rs b/fuzz/fuzz_targets/fuzz_arithmetic.rs index 2c15628b..832a1fd6 100644 --- a/fuzz/fuzz_targets/fuzz_arithmetic.rs +++ b/fuzz/fuzz_targets/fuzz_arithmetic.rs @@ -1,7 +1,6 @@ #![no_main] use libfuzzer_sys::fuzz_target; use std::str; -use winnow::error::ParserError; use winnow::prelude::*; use winnow::{ @@ -29,7 +28,7 @@ fn incr(i: &mut &str) -> ModalResult<()> { // limit the number of recursions, the fuzzer keeps running into them if *l.borrow() >= 8192 { - Err(winnow::error::ErrMode::from_error_kind( + Err(winnow::error::ParserError::from_error_kind( i, winnow::error::ErrorKind::Repeat, )) diff --git a/src/_tutorial/chapter_2.rs b/src/_tutorial/chapter_2.rs index d3461e75..1415462a 100644 --- a/src/_tutorial/chapter_2.rs +++ b/src/_tutorial/chapter_2.rs @@ -16,10 +16,10 @@ //! //! fn parse_prefix(input: &mut &str) -> ModalResult { //! let c = input.next_token().ok_or_else(|| { -//! ErrMode::from_error_kind(input, ErrorKind::Token) +//! ParserError::from_error_kind(input, ErrorKind::Token) //! })?; //! if c != '0' { -//! return Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); +//! return Err(ParserError::from_error_kind(input, ErrorKind::Verify)); //! } //! Ok(c) //! } @@ -49,7 +49,7 @@ //! let c = any //! .parse_next(input)?; //! if c != '0' { -//! return Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); +//! return Err(ParserError::from_error_kind(input, ErrorKind::Verify)); //! } //! Ok(c) //! } @@ -129,11 +129,11 @@ //! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { //! let expected = "0x"; //! if input.len() < expected.len() { -//! return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); +//! return Err(ParserError::from_error_kind(input, ErrorKind::Slice)); //! } //! let actual = input.next_slice(expected.len()); //! if actual != expected { -//! return Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); +//! return Err(ParserError::from_error_kind(input, ErrorKind::Verify)); //! } //! Ok(actual) //! } diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index 2066c6b1..d7e8d627 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -15,7 +15,7 @@ use crate::combinator::opt; use crate::combinator::peek; use crate::combinator::trace; use crate::error::ParserError; -use crate::error::{ErrMode, ErrorKind, Needed}; +use crate::error::{ErrorKind, Needed}; use crate::stream::FindSlice; use crate::stream::{AsBStr, AsChar, ParseSlice, Stream, StreamIsPartial}; use crate::stream::{Compare, CompareResult}; @@ -31,7 +31,6 @@ use crate::Parser; /// # Example /// ```rust /// # use winnow::prelude::*; -/// # use winnow::{error::ErrMode, error::{ErrorKind}}; /// # use winnow::ascii::Caseless; /// /// fn parser<'s>(s: &mut &'s str) -> ModalResult<&'s str> { @@ -190,11 +189,11 @@ where match comp { CompareResult::Ok(_) => {} CompareResult::Incomplete if PARTIAL && input.is_partial() => { - return Err(ErrMode::incomplete(input, Needed::Unknown)); + return Err(ParserError::incomplete(input, Needed::Unknown)); } CompareResult::Incomplete | CompareResult::Error => { let e: ErrorKind = ErrorKind::Literal; - return Err(ErrMode::from_error_kind(input, e)); + return Err(ParserError::from_error_kind(input, e)); } } } @@ -1333,7 +1332,7 @@ where Ok(max_offset) => { if max_offset < invalid_offset { // Overflow - return Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); + return Err(ParserError::from_error_kind(input, ErrorKind::Verify)); } else { invalid_offset } @@ -1344,7 +1343,7 @@ where && invalid_offset == input.eof_offset() { // Only the next byte is guaranteed required - return Err(ErrMode::incomplete(input, Needed::new(1))); + return Err(ParserError::incomplete(input, Needed::new(1))); } else { invalid_offset } @@ -1352,7 +1351,7 @@ where }; if offset == 0 { // Must be at least one digit - return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); + return Err(ParserError::from_error_kind(input, ErrorKind::Slice)); } let parsed = input.next_slice(offset); @@ -1478,7 +1477,7 @@ where trace("float", move |input: &mut Input| { let s = take_float_or_exceptions(input)?; s.parse_slice() - .ok_or_else(|| ErrMode::from_error_kind(input, ErrorKind::Verify)) + .ok_or_else(|| ParserError::from_error_kind(input, ErrorKind::Verify)) }) .parse_next(input) } @@ -1669,7 +1668,7 @@ where Some(_) => { // infinite loop check: the parser must always consume if input.eof_offset() == current_len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`take_escaped` parsers must always consume", )); @@ -1688,7 +1687,7 @@ where } if PARTIAL && input.is_partial() { - Err(ErrMode::incomplete(input, Needed::Unknown)) + Err(ParserError::incomplete(input, Needed::Unknown)) } else { input.reset(&start); Ok(input.finish()) @@ -1827,7 +1826,7 @@ where res.accumulate(o); // infinite loop check: the parser must always consume if input.eof_offset() == current_len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`escaped_transform` parsers must always consume", )); @@ -1845,7 +1844,7 @@ where } if PARTIAL && input.is_partial() { - Err(ErrMode::incomplete(input, Needed::Unknown)) + Err(ParserError::incomplete(input, Needed::Unknown)) } else { Ok(res) } diff --git a/src/binary/bits/mod.rs b/src/binary/bits/mod.rs index 3e8e7489..22bf5a23 100644 --- a/src/binary/bits/mod.rs +++ b/src/binary/bits/mod.rs @@ -5,7 +5,7 @@ mod tests; use crate::combinator::trace; -use crate::error::{ErrMode, ErrorConvert, ErrorKind, Needed, ParserError}; +use crate::error::{ErrorConvert, ErrorKind, Needed, ParserError}; use crate::lib::std::ops::{AddAssign, Div, Shl, Shr}; use crate::stream::{Stream, StreamIsPartial, ToUsize}; use crate::{ModalResult, Parser}; @@ -69,7 +69,10 @@ where Ok(result) } Err(e) => match e.into_needed() { - Ok(n) => Err(ErrMode::incomplete(input, n.map(|u| u.get() / BYTE + 1))), + Ok(n) => Err(ParserError::incomplete( + input, + n.map(|u| u.get() / BYTE + 1), + )), Err(e) => Err(ErrorConvert::convert(e)), }, } @@ -134,10 +137,10 @@ where Ok(res) } Err(e) => match e.into_needed() { - Ok(Needed::Unknown) => Err(ErrMode::incomplete(bit_input, Needed::Unknown)), + Ok(Needed::Unknown) => Err(ParserError::incomplete(bit_input, Needed::Unknown)), Ok(Needed::Size(sz)) => Err(match sz.get().checked_mul(BYTE) { - Some(v) => ErrMode::incomplete(bit_input, Needed::new(v)), - None => ErrMode::assert( + Some(v) => ParserError::incomplete(bit_input, Needed::new(v)), + None => ParserError::assert( bit_input, "overflow in turning needed bytes into needed bits", ), @@ -220,9 +223,9 @@ where let (mut input, bit_offset) = bit_input.clone(); if input.eof_offset() * BYTE < count + bit_offset { if PARTIAL && input.is_partial() { - Err(ErrMode::incomplete(bit_input, Needed::new(count))) + Err(ParserError::incomplete(bit_input, Needed::new(count))) } else { - Err(ErrMode::from_error_kind( + Err(ParserError::from_error_kind( &(input, bit_offset), ErrorKind::Eof, )) @@ -340,7 +343,7 @@ where Ok(o) } else { input.reset(&start); - Err(ErrMode::from_error_kind(input, ErrorKind::Literal)) + Err(ParserError::from_error_kind(input, ErrorKind::Literal)) } }) }) diff --git a/src/binary/mod.rs b/src/binary/mod.rs index 31e57b5c..b092b40e 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -9,7 +9,6 @@ mod tests; use crate::combinator::repeat; use crate::combinator::trace; -use crate::error::ErrMode; use crate::error::ErrorKind; use crate::error::Needed; use crate::error::ParserError; @@ -310,9 +309,9 @@ where Ok(res) } Err(e) if ::is_partial_supported() && input.is_partial() => { - Err(ErrMode::incomplete(input, e)) + Err(ParserError::incomplete(input, e)) } - Err(_needed) => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)), + Err(_needed) => Err(ParserError::from_error_kind(input, ErrorKind::Slice)), } } @@ -898,9 +897,9 @@ where Ok(res) } Err(e) if ::is_partial_supported() && input.is_partial() => { - Err(ErrMode::incomplete(input, e)) + Err(ParserError::incomplete(input, e)) } - Err(_needed) => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)), + Err(_needed) => Err(ParserError::from_error_kind(input, ErrorKind::Slice)), } } @@ -1273,9 +1272,9 @@ where { input.next_token().ok_or_else(|| { if PARTIAL && input.is_partial() { - ErrMode::incomplete(input, Needed::new(1)) + ParserError::incomplete(input, Needed::new(1)) } else { - ErrMode::from_error_kind(input, ErrorKind::Token) + ParserError::from_error_kind(input, ErrorKind::Token) } }) } diff --git a/src/combinator/branch.rs b/src/combinator/branch.rs index 891ca54d..693a049d 100644 --- a/src/combinator/branch.rs +++ b/src/combinator/branch.rs @@ -146,7 +146,10 @@ impl, P: Parser> Alt Err(e.append(input, &start, ErrorKind::Alt)), - None => Err(ErrMode::assert(input, "`alt` needs at least one parser")), + None => Err(ParserError::assert( + input, + "`alt` needs at least one parser", + )), } } } @@ -171,7 +174,10 @@ impl, P: Parser> Alt for &mut match error { Some(e) => Err(e.append(input, &start, ErrorKind::Alt)), - None => Err(ErrMode::assert(input, "`alt` needs at least one parser")), + None => Err(ParserError::assert( + input, + "`alt` needs at least one parser", + )), } } } diff --git a/src/combinator/core.rs b/src/combinator/core.rs index 7cd38aea..f5cba2e6 100644 --- a/src/combinator/core.rs +++ b/src/combinator/core.rs @@ -183,7 +183,7 @@ where if input.eof_offset() == 0 { Ok(input.next_slice(0)) } else { - Err(ErrMode::from_error_kind(input, ErrorKind::Eof)) + Err(ParserError::from_error_kind(input, ErrorKind::Eof)) } }) .parse_next(input) @@ -224,7 +224,7 @@ where let res = parser.parse_next(input); input.reset(&start); match res { - Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Not)), + Ok(_) => Err(ParserError::from_error_kind(input, ErrorKind::Not)), Err(e) if e.is_backtrack() => Ok(()), Err(e) => Err(e), } @@ -525,7 +525,7 @@ where Error: ParserError, { trace("fail", |i: &mut Input| { - Err(ErrMode::from_error_kind(i, ErrorKind::Fail)) + Err(ParserError::from_error_kind(i, ErrorKind::Fail)) }) .parse_next(i) } diff --git a/src/combinator/impls.rs b/src/combinator/impls.rs index a2958618..51f6bd9d 100644 --- a/src/combinator/impls.rs +++ b/src/combinator/impls.rs @@ -130,7 +130,7 @@ where let o = self.parser.parse_next(input)?; let res = (self.map)(o).ok_or_else(|| { input.reset(&start); - ErrMode::from_error_kind(input, ErrorKind::Verify) + ParserError::from_error_kind(input, ErrorKind::Verify) }); trace_result("verify", &res); res @@ -201,7 +201,7 @@ where let o = self.p.parse_next(i)?; let res = o.parse_slice().ok_or_else(|| { i.reset(&start); - ErrMode::from_error_kind(i, ErrorKind::Verify) + ParserError::from_error_kind(i, ErrorKind::Verify) }); trace_result("verify", &res); res @@ -256,7 +256,7 @@ where trace("complete_err", |input: &mut I| { match (self.p).parse_next(input) { Err(err) => match err.into_needed() { - Ok(_) => Err(ErrMode::from_error_kind(input, ErrorKind::Complete)), + Ok(_) => Err(ParserError::from_error_kind(input, ErrorKind::Complete)), Err(err) => Err(err), }, rest => rest, @@ -299,7 +299,7 @@ where let o = self.parser.parse_next(input)?; let res = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| { input.reset(&start); - ErrMode::from_error_kind(input, ErrorKind::Verify) + ParserError::from_error_kind(input, ErrorKind::Verify) }); trace_result("verify", &res); res diff --git a/src/combinator/multi.rs b/src/combinator/multi.rs index 8a106d3b..2444f737 100644 --- a/src/combinator/multi.rs +++ b/src/combinator/multi.rs @@ -479,7 +479,10 @@ where Ok(o) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + return Err(ParserError::assert( + i, + "`repeat` parsers must always consume", + )); } acc.accumulate(o); @@ -514,7 +517,10 @@ where Ok(o) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + return Err(ParserError::assert( + i, + "`repeat` parsers must always consume", + )); } acc.accumulate(o); @@ -541,7 +547,10 @@ where Ok(o) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + return Err(ParserError::assert( + i, + "`repeat` parsers must always consume", + )); } res.accumulate(o); @@ -568,7 +577,7 @@ where E: ParserError, { if min > max { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "range should be ascending, rather than descending", )); @@ -582,7 +591,7 @@ where Ok(value) => { // infinite loop check: the parser must always consume if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`repeat` parsers must always consume", )); @@ -693,7 +702,10 @@ where Ok(o) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + return Err(ParserError::assert( + i, + "`repeat` parsers must always consume", + )); } res.accumulate(o); @@ -720,7 +732,7 @@ where E: ParserError, { if min > max { - return Err(ErrMode::assert( + return Err(ParserError::assert( i, "range should be ascending, rather than descending", )); @@ -756,7 +768,10 @@ where Ok(o) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + return Err(ParserError::assert( + i, + "`repeat` parsers must always consume", + )); } res.accumulate(o); @@ -941,7 +956,7 @@ where Ok(_) => { // infinite loop check if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`separated` separator parser must always consume", )); @@ -996,7 +1011,7 @@ where Ok(_) => { // infinite loop check if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`separated` separator parser must always consume", )); @@ -1056,7 +1071,7 @@ where Ok(_) => { // infinite loop check if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`separated` separator parser must always consume", )); @@ -1092,7 +1107,7 @@ where E: ParserError, { if min > max { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "range should be ascending, rather than descending", )); @@ -1134,7 +1149,7 @@ where Ok(_) => { // infinite loop check if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`separated` separator parser must always consume", )); @@ -1211,7 +1226,10 @@ where Ok(s) => { // infinite loop check: the parser must always consume if i.eof_offset() == len { - return Err(ErrMode::assert(i, "`repeat` parsers must always consume")); + return Err(ParserError::assert( + i, + "`repeat` parsers must always consume", + )); } match parser.parse_next(i) { @@ -1353,7 +1371,7 @@ where Ok(o) => { // infinite loop check: the parser must always consume if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`repeat` parsers must always consume", )); @@ -1404,7 +1422,7 @@ where Ok(o) => { // infinite loop check: the parser must always consume if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`repeat` parsers must always consume", )); @@ -1436,7 +1454,7 @@ where E: ParserError, { if min > max { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "range should be ascending, rather than descending", )); @@ -1450,7 +1468,7 @@ where Ok(value) => { // infinite loop check: the parser must always consume if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`repeat` parsers must always consume", )); @@ -1491,7 +1509,7 @@ where E: ParserError, { if min > max { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "range should be ascending, rather than descending", )); @@ -1505,7 +1523,7 @@ where Ok(value) => { // infinite loop check: the parser must always consume if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`repeat` parsers must always consume", )); @@ -1513,7 +1531,7 @@ where let Some(tmp) = fold(acc, value) else { input.reset(&start); - let res = Err(ErrMode::from_error_kind(input, ErrorKind::Verify)); + let res = Err(ParserError::from_error_kind(input, ErrorKind::Verify)); super::debug::trace_result("verify_fold", &res); return res; }; @@ -1552,7 +1570,7 @@ where E: ParserError + FromExternalError, { if min > max { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "range should be ascending, rather than descending", )); @@ -1566,7 +1584,7 @@ where Ok(value) => { // infinite loop check: the parser must always consume if input.eof_offset() == len { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`repeat` parsers must always consume", )); diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index 7196ab86..16c6737c 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -2922,7 +2922,7 @@ Err( fn infinite_many() { fn tst<'i>(input: &mut &'i [u8]) -> TestResult<&'i [u8], &'i [u8]> { println!("input: {input:?}"); - Err(ErrMode::from_error_kind(input, ErrorKind::Literal)) + Err(ParserError::from_error_kind(input, ErrorKind::Literal)) } // should not go into an infinite loop diff --git a/src/token/mod.rs b/src/token/mod.rs index b386f2b6..7ef751ee 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -5,7 +5,6 @@ mod tests; use crate::combinator::trace; use crate::combinator::DisplayDebug; -use crate::error::ErrMode; use crate::error::ErrorKind; use crate::error::Needed; use crate::error::ParserError; @@ -79,9 +78,9 @@ where { input.next_token().ok_or_else(|| { if PARTIAL && input.is_partial() { - ErrMode::incomplete(input, Needed::new(1)) + ParserError::incomplete(input, Needed::new(1)) } else { - ErrMode::from_error_kind(input, ErrorKind::Token) + ParserError::from_error_kind(input, ErrorKind::Token) } }) } @@ -191,13 +190,13 @@ where let literal_len = t.slice_len(); match i.compare(t) { CompareResult::Ok(len) => Ok(i.next_slice(len)), - CompareResult::Incomplete if PARTIAL && i.is_partial() => Err(ErrMode::incomplete( + CompareResult::Incomplete if PARTIAL && i.is_partial() => Err(ParserError::incomplete( i, Needed::new(literal_len - i.eof_offset()), )), CompareResult::Incomplete | CompareResult::Error => { let e: ErrorKind = ErrorKind::Literal; - Err(ErrMode::from_error_kind(i, e)) + Err(ParserError::from_error_kind(i, e)) } } } @@ -539,7 +538,7 @@ where let offset = match input.offset_for(predicate) { Some(offset) => offset, None if PARTIAL && input.is_partial() => { - return Err(ErrMode::incomplete(input, Needed::new(1))); + return Err(ParserError::incomplete(input, Needed::new(1))); } None => input.eof_offset(), }; @@ -557,12 +556,12 @@ where let offset = match input.offset_for(predicate) { Some(offset) => offset, None if PARTIAL && input.is_partial() => { - return Err(ErrMode::incomplete(input, Needed::new(1))); + return Err(ParserError::incomplete(input, Needed::new(1))); } None => input.eof_offset(), }; if offset == 0 { - Err(ErrMode::from_error_kind(input, e)) + Err(ParserError::from_error_kind(input, e)) } else { Ok(input.next_slice(offset)) } @@ -580,7 +579,7 @@ where P: FnMut(I::Token) -> bool, { if n < m { - return Err(ErrMode::assert( + return Err(ParserError::assert( input, "`occurrences` should be ascending, rather than descending", )); @@ -590,7 +589,7 @@ where for (processed, (offset, token)) in input.iter_offsets().enumerate() { if predicate(token) { if processed < m { - return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); + return Err(ParserError::from_error_kind(input, ErrorKind::Slice)); } else { return Ok(input.next_slice(offset)); } @@ -610,13 +609,13 @@ where } else { 1 }; - Err(ErrMode::incomplete(input, Needed::new(needed))) + Err(ParserError::incomplete(input, Needed::new(needed))) } } else { if m <= final_count { Ok(input.finish()) } else { - Err(ErrMode::from_error_kind(input, ErrorKind::Slice)) + Err(ParserError::from_error_kind(input, ErrorKind::Slice)) } } } @@ -820,8 +819,8 @@ where { match i.offset_at(c) { Ok(offset) => Ok(i.next_slice(offset)), - Err(e) if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, e)), - Err(_needed) => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), + Err(e) if PARTIAL && i.is_partial() => Err(ParserError::incomplete(i, e)), + Err(_needed) => Err(ParserError::from_error_kind(i, ErrorKind::Slice)), } } @@ -971,8 +970,8 @@ where { match i.find_slice(t) { Some(range) => Ok(i.next_slice(range.start)), - None if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, Needed::Unknown)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), + None if PARTIAL && i.is_partial() => Err(ParserError::incomplete(i, Needed::Unknown)), + None => Err(ParserError::from_error_kind(i, ErrorKind::Slice)), } } @@ -985,11 +984,11 @@ where I: Stream + FindSlice, { match i.find_slice(t) { - None if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, Needed::Unknown)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), + None if PARTIAL && i.is_partial() => Err(ParserError::incomplete(i, Needed::Unknown)), + None => Err(ParserError::from_error_kind(i, ErrorKind::Slice)), Some(range) => { if range.start == 0 { - Err(ErrMode::from_error_kind(i, ErrorKind::Slice)) + Err(ParserError::from_error_kind(i, ErrorKind::Slice)) } else { Ok(i.next_slice(range.start)) } @@ -1008,7 +1007,7 @@ where I: Stream + FindSlice, { if end < start { - return Err(ErrMode::assert( + return Err(ParserError::assert( i, "`occurrences` should be ascending, rather than descending", )); @@ -1020,18 +1019,18 @@ where let end_offset = i.offset_at(end).unwrap_or_else(|_err| i.eof_offset()); if start_offset.map(|s| range.start < s).unwrap_or(true) { if PARTIAL && i.is_partial() { - return Err(ErrMode::incomplete(i, Needed::Unknown)); + return Err(ParserError::incomplete(i, Needed::Unknown)); } else { - return Err(ErrMode::from_error_kind(i, ErrorKind::Slice)); + return Err(ParserError::from_error_kind(i, ErrorKind::Slice)); } } if end_offset < range.start { - return Err(ErrMode::from_error_kind(i, ErrorKind::Slice)); + return Err(ParserError::from_error_kind(i, ErrorKind::Slice)); } Ok(i.next_slice(range.start)) } - None if PARTIAL && i.is_partial() => Err(ErrMode::incomplete(i, Needed::Unknown)), - None => Err(ErrMode::from_error_kind(i, ErrorKind::Slice)), + None if PARTIAL && i.is_partial() => Err(ParserError::incomplete(i, Needed::Unknown)), + None => Err(ParserError::from_error_kind(i, ErrorKind::Slice)), } } diff --git a/src/token/tests.rs b/src/token/tests.rs index befe2be0..2381fbe3 100644 --- a/src/token/tests.rs +++ b/src/token/tests.rs @@ -69,7 +69,7 @@ fn model_complete_take_while_m_n<'i>( input: &mut &'i str, ) -> ModalResult<&'i str> { if n < m { - Err(crate::error::ErrMode::from_error_kind( + Err(crate::error::ParserError::from_error_kind( input, crate::error::ErrorKind::Slice, )) @@ -77,7 +77,7 @@ fn model_complete_take_while_m_n<'i>( let offset = n.min(valid); Ok(input.next_slice(offset)) } else { - Err(crate::error::ErrMode::from_error_kind( + Err(crate::error::ParserError::from_error_kind( input, crate::error::ErrorKind::Slice, )) From d086f732a2611db4047c31fa9f022b68af323a38 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 22 Jan 2025 15:27:52 -0600 Subject: [PATCH 12/17] fix!: Make ErrMode optional by using Result --- examples/s_expression/parser.rs | 5 +- src/ascii/mod.rs | 191 +++++++++++++++--------------- src/ascii/tests.rs | 2 +- src/binary/bits/mod.rs | 22 ++-- src/binary/bits/tests.rs | 15 ++- src/binary/mod.rs | 68 +++++------ src/combinator/branch.rs | 22 ++-- src/combinator/core.rs | 30 ++--- src/combinator/debug/internals.rs | 2 +- src/combinator/impls.rs | 56 +++++---- src/combinator/multi.rs | 58 ++++----- src/macros/seq.rs | 3 +- src/parser.rs | 44 ++++--- src/stream/mod.rs | 55 +++++---- src/stream/tests.rs | 34 +++--- src/token/mod.rs | 44 ++++--- tests/testsuite/issues.rs | 2 +- tests/testsuite/reborrow_fold.rs | 3 +- 18 files changed, 327 insertions(+), 329 deletions(-) diff --git a/examples/s_expression/parser.rs b/examples/s_expression/parser.rs index dfedf59c..4673f180 100644 --- a/examples/s_expression/parser.rs +++ b/examples/s_expression/parser.rs @@ -2,6 +2,7 @@ //! parser and tiny [lisp](https://en.wikipedia.org/wiki/Lisp_(programming_language)) interpreter. //! Lisp is a simple type of language made up of Atoms and Lists, forming easily parsable trees. +use winnow::error::ErrMode; use winnow::{ ascii::{alpha1, digit1, multispace0, multispace1}, combinator::alt, @@ -224,9 +225,9 @@ fn parse_quote(i: &mut &'_ str) -> ModalResult { //.parse_next/ /// Unlike the previous functions, this function doesn't take or consume input, instead it /// takes a parsing function and returns a new parsing function. -fn s_exp<'a, O1, F>(inner: F) -> impl Parser<&'a str, O1, ContextError> +fn s_exp<'a, O1, F>(inner: F) -> impl Parser<&'a str, O1, ErrMode> where - F: Parser<&'a str, O1, ContextError>, + F: Parser<&'a str, O1, ErrMode>, { delimited( '(', diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index d7e8d627..e2321fec 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -23,8 +23,8 @@ use crate::token::any; use crate::token::one_of; use crate::token::take_until; use crate::token::take_while; -use crate::ModalResult; use crate::Parser; +use crate::Result; /// Mark a value as case-insensitive for ASCII characters /// @@ -90,12 +90,12 @@ impl Caseless<&str> { /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::crlf; -/// assert_eq!(crlf::<_, ContextError>.parse_peek(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); -/// assert!(crlf::<_, ContextError>.parse_peek(Partial::new("ab\r\nc")).is_err()); -/// assert_eq!(crlf::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(2)))); +/// assert_eq!(crlf::<_, ErrMode>.parse_peek(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); +/// assert!(crlf::<_, ErrMode>.parse_peek(Partial::new("ab\r\nc")).is_err()); +/// assert_eq!(crlf::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn crlf(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn crlf(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream + Compare<&'static str>, Error: ParserError, @@ -142,16 +142,14 @@ where /// # use winnow::{error::ErrMode, error::{ContextError, ErrorKind}, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::till_line_ending; -/// assert_eq!(till_line_ending::<_, ContextError>.parse_peek(Partial::new("ab\r\nc")), Ok((Partial::new("\r\nc"), "ab"))); -/// assert_eq!(till_line_ending::<_, ContextError>.parse_peek(Partial::new("abc")), Err(ErrMode::Incomplete(Needed::Unknown))); -/// assert_eq!(till_line_ending::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::Unknown))); -/// assert!(till_line_ending::<_, ContextError>.parse_peek(Partial::new("a\rb\nc")).is_err()); -/// assert!(till_line_ending::<_, ContextError>.parse_peek(Partial::new("a\rbc")).is_err()); +/// assert_eq!(till_line_ending::<_, ErrMode>.parse_peek(Partial::new("ab\r\nc")), Ok((Partial::new("\r\nc"), "ab"))); +/// assert_eq!(till_line_ending::<_, ErrMode>.parse_peek(Partial::new("abc")), Err(ErrMode::Incomplete(Needed::Unknown))); +/// assert_eq!(till_line_ending::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::Unknown))); +/// assert!(till_line_ending::<_, ErrMode>.parse_peek(Partial::new("a\rb\nc")).is_err()); +/// assert!(till_line_ending::<_, ErrMode>.parse_peek(Partial::new("a\rbc")).is_err()); /// ``` #[inline(always)] -pub fn till_line_ending( - input: &mut Input, -) -> ModalResult<::Slice, Error> +pub fn till_line_ending(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream + Compare<&'static str> + FindSlice<(char, char)>, ::Token: AsChar + Clone, @@ -169,7 +167,7 @@ where fn till_line_ending_, const PARTIAL: bool>( input: &mut I, -) -> ModalResult<::Slice, E> +) -> Result<::Slice, E> where I: StreamIsPartial, I: Stream, @@ -177,7 +175,10 @@ where I: FindSlice<(char, char)>, ::Token: AsChar + Clone, { - let res = match take_until(0.., ('\r', '\n')).parse_next(input) { + let res = match take_until(0.., ('\r', '\n')) + .parse_next(input) + .map_err(|e: E| e) + { Ok(slice) => slice, Err(err) if err.is_backtrack() => input.finish(), Err(err) => { @@ -236,12 +237,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::line_ending; -/// assert_eq!(line_ending::<_, ContextError>.parse_peek(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); -/// assert!(line_ending::<_, ContextError>.parse_peek(Partial::new("ab\r\nc")).is_err()); -/// assert_eq!(line_ending::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(line_ending::<_, ErrMode>.parse_peek(Partial::new("\r\nc")), Ok((Partial::new("c"), "\r\n"))); +/// assert!(line_ending::<_, ErrMode>.parse_peek(Partial::new("ab\r\nc")).is_err()); +/// assert_eq!(line_ending::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn line_ending(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn line_ending(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream + Compare<&'static str>, Error: ParserError, @@ -285,12 +286,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::newline; -/// assert_eq!(newline::<_, ContextError>.parse_peek(Partial::new("\nc")), Ok((Partial::new("c"), '\n'))); -/// assert!(newline::<_, ContextError>.parse_peek(Partial::new("\r\nc")).is_err()); -/// assert_eq!(newline::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(newline::<_, ErrMode>.parse_peek(Partial::new("\nc")), Ok((Partial::new("c"), '\n'))); +/// assert!(newline::<_, ErrMode>.parse_peek(Partial::new("\r\nc")).is_err()); +/// assert_eq!(newline::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn newline>(input: &mut I) -> ModalResult +pub fn newline>(input: &mut I) -> Result where I: StreamIsPartial, I: Stream, @@ -335,12 +336,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::tab; -/// assert_eq!(tab::<_, ContextError>.parse_peek(Partial::new("\tc")), Ok((Partial::new("c"), '\t'))); -/// assert!(tab::<_, ContextError>.parse_peek(Partial::new("\r\nc")).is_err()); -/// assert_eq!(tab::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(tab::<_, ErrMode>.parse_peek(Partial::new("\tc")), Ok((Partial::new("c"), '\t'))); +/// assert!(tab::<_, ErrMode>.parse_peek(Partial::new("\r\nc")).is_err()); +/// assert_eq!(tab::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn tab(input: &mut Input) -> ModalResult +pub fn tab(input: &mut Input) -> Result where Input: StreamIsPartial + Stream + Compare, Error: ParserError, @@ -386,12 +387,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alpha0; -/// assert_eq!(alpha0::<_, ContextError>.parse_peek(Partial::new("ab1c")), Ok((Partial::new("1c"), "ab"))); -/// assert_eq!(alpha0::<_, ContextError>.parse_peek(Partial::new("1c")), Ok((Partial::new("1c"), ""))); -/// assert_eq!(alpha0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alpha0::<_, ErrMode>.parse_peek(Partial::new("ab1c")), Ok((Partial::new("1c"), "ab"))); +/// assert_eq!(alpha0::<_, ErrMode>.parse_peek(Partial::new("1c")), Ok((Partial::new("1c"), ""))); +/// assert_eq!(alpha0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alpha0(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn alpha0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -438,12 +439,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alpha1; -/// assert_eq!(alpha1::<_, ContextError>.parse_peek(Partial::new("aB1c")), Ok((Partial::new("1c"), "aB"))); -/// assert!(alpha1::<_, ContextError>.parse_peek(Partial::new("1c")).is_err()); -/// assert_eq!(alpha1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alpha1::<_, ErrMode>.parse_peek(Partial::new("aB1c")), Ok((Partial::new("1c"), "aB"))); +/// assert!(alpha1::<_, ErrMode>.parse_peek(Partial::new("1c")).is_err()); +/// assert_eq!(alpha1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alpha1(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn alpha1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -491,12 +492,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::digit0; -/// assert_eq!(digit0::<_, ContextError>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21"))); -/// assert_eq!(digit0::<_, ContextError>.parse_peek(Partial::new("a21c")), Ok((Partial::new("a21c"), ""))); -/// assert_eq!(digit0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(digit0::<_, ErrMode>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21"))); +/// assert_eq!(digit0::<_, ErrMode>.parse_peek(Partial::new("a21c")), Ok((Partial::new("a21c"), ""))); +/// assert_eq!(digit0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn digit0(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn digit0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -543,9 +544,9 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::digit1; -/// assert_eq!(digit1::<_, ContextError>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21"))); -/// assert!(digit1::<_, ContextError>.parse_peek(Partial::new("c1")).is_err()); -/// assert_eq!(digit1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(digit1::<_, ErrMode>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21"))); +/// assert!(digit1::<_, ErrMode>.parse_peek(Partial::new("c1")).is_err()); +/// assert_eq!(digit1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` /// /// ## Parsing an integer @@ -564,7 +565,7 @@ where /// assert!(parser.parse_peek("b").is_err()); /// ``` #[inline(always)] -pub fn digit1(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn digit1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -611,12 +612,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::hex_digit0; -/// assert_eq!(hex_digit0::<_, ContextError>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); -/// assert_eq!(hex_digit0::<_, ContextError>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(hex_digit0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(hex_digit0::<_, ErrMode>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); +/// assert_eq!(hex_digit0::<_, ErrMode>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(hex_digit0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn hex_digit0(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn hex_digit0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -664,12 +665,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::hex_digit1; -/// assert_eq!(hex_digit1::<_, ContextError>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); -/// assert!(hex_digit1::<_, ContextError>.parse_peek(Partial::new("H2")).is_err()); -/// assert_eq!(hex_digit1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(hex_digit1::<_, ErrMode>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("Z"), "21c"))); +/// assert!(hex_digit1::<_, ErrMode>.parse_peek(Partial::new("H2")).is_err()); +/// assert_eq!(hex_digit1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn hex_digit1(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn hex_digit1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -716,12 +717,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::oct_digit0; -/// assert_eq!(oct_digit0::<_, ContextError>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); -/// assert_eq!(oct_digit0::<_, ContextError>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(oct_digit0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(oct_digit0::<_, ErrMode>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); +/// assert_eq!(oct_digit0::<_, ErrMode>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(oct_digit0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn oct_digit0(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn oct_digit0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial, Input: Stream, @@ -769,12 +770,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::oct_digit1; -/// assert_eq!(oct_digit1::<_, ContextError>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); -/// assert!(oct_digit1::<_, ContextError>.parse_peek(Partial::new("H2")).is_err()); -/// assert_eq!(oct_digit1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(oct_digit1::<_, ErrMode>.parse_peek(Partial::new("21cZ")), Ok((Partial::new("cZ"), "21"))); +/// assert!(oct_digit1::<_, ErrMode>.parse_peek(Partial::new("H2")).is_err()); +/// assert_eq!(oct_digit1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn oct_digit1(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn oct_digit1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -821,14 +822,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alphanumeric0; -/// assert_eq!(alphanumeric0::<_, ContextError>.parse_peek(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); -/// assert_eq!(alphanumeric0::<_, ContextError>.parse_peek(Partial::new("&Z21c")), Ok((Partial::new("&Z21c"), ""))); -/// assert_eq!(alphanumeric0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alphanumeric0::<_, ErrMode>.parse_peek(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); +/// assert_eq!(alphanumeric0::<_, ErrMode>.parse_peek(Partial::new("&Z21c")), Ok((Partial::new("&Z21c"), ""))); +/// assert_eq!(alphanumeric0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alphanumeric0( - input: &mut Input, -) -> ModalResult<::Slice, Error> +pub fn alphanumeric0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -875,14 +874,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::alphanumeric1; -/// assert_eq!(alphanumeric1::<_, ContextError>.parse_peek(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); -/// assert!(alphanumeric1::<_, ContextError>.parse_peek(Partial::new("&H2")).is_err()); -/// assert_eq!(alphanumeric1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(alphanumeric1::<_, ErrMode>.parse_peek(Partial::new("21cZ%1")), Ok((Partial::new("%1"), "21cZ"))); +/// assert!(alphanumeric1::<_, ErrMode>.parse_peek(Partial::new("&H2")).is_err()); +/// assert_eq!(alphanumeric1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn alphanumeric1( - input: &mut Input, -) -> ModalResult<::Slice, Error> +pub fn alphanumeric1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -917,12 +914,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::space0; -/// assert_eq!(space0::<_, ContextError>.parse_peek(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); -/// assert_eq!(space0::<_, ContextError>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(space0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(space0::<_, ErrMode>.parse_peek(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); +/// assert_eq!(space0::<_, ErrMode>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(space0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn space0(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn space0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -969,12 +966,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::space1; -/// assert_eq!(space1::<_, ContextError>.parse_peek(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); -/// assert!(space1::<_, ContextError>.parse_peek(Partial::new("H2")).is_err()); -/// assert_eq!(space1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(space1::<_, ErrMode>.parse_peek(Partial::new(" \t21c")), Ok((Partial::new("21c"), " \t"))); +/// assert!(space1::<_, ErrMode>.parse_peek(Partial::new("H2")).is_err()); +/// assert_eq!(space1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn space1(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn space1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -1021,12 +1018,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::multispace0; -/// assert_eq!(multispace0::<_, ContextError>.parse_peek(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); -/// assert_eq!(multispace0::<_, ContextError>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); -/// assert_eq!(multispace0::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(multispace0::<_, ErrMode>.parse_peek(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); +/// assert_eq!(multispace0::<_, ErrMode>.parse_peek(Partial::new("Z21c")), Ok((Partial::new("Z21c"), ""))); +/// assert_eq!(multispace0::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn multispace0(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn multispace0(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar + Clone, @@ -1073,12 +1070,12 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::ascii::multispace1; -/// assert_eq!(multispace1::<_, ContextError>.parse_peek(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); -/// assert!(multispace1::<_, ContextError>.parse_peek(Partial::new("H2")).is_err()); -/// assert_eq!(multispace1::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(multispace1::<_, ErrMode>.parse_peek(Partial::new(" \t\n\r21c")), Ok((Partial::new("21c"), " \t\n\r"))); +/// assert!(multispace1::<_, ErrMode>.parse_peek(Partial::new("H2")).is_err()); +/// assert_eq!(multispace1::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn multispace1(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn multispace1(input: &mut Input) -> Result<::Slice, Error> where Input: StreamIsPartial + Stream, ::Token: AsChar + Clone, @@ -1108,7 +1105,7 @@ where #[doc(alias = "u32")] #[doc(alias = "u64")] #[doc(alias = "u128")] -pub fn dec_uint(input: &mut Input) -> ModalResult +pub fn dec_uint(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, ::Slice: AsBStr, @@ -1193,7 +1190,7 @@ impl Uint for usize { #[doc(alias = "i32")] #[doc(alias = "i64")] #[doc(alias = "i128")] -pub fn dec_int(input: &mut Input) -> ModalResult +pub fn dec_int(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, ::Slice: AsBStr, @@ -1311,7 +1308,7 @@ impl Int for isize { /// assert!(parser.parse_peek(Partial::new(&b"ggg"[..])).is_err()); /// ``` #[inline] -pub fn hex_uint(input: &mut Input) -> ModalResult +pub fn hex_uint(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, ::Token: AsChar, @@ -1466,7 +1463,7 @@ impl HexUint for u128 { #[doc(alias = "f32")] #[doc(alias = "double")] #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -pub fn float(input: &mut Input) -> ModalResult +pub fn float(input: &mut Input) -> Result where Input: StreamIsPartial + Stream + Compare> + Compare + AsBStr, ::Slice: ParseSlice, @@ -1483,9 +1480,7 @@ where } #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -fn take_float_or_exceptions>( - input: &mut I, -) -> ModalResult<::Slice, E> +fn take_float_or_exceptions>(input: &mut I) -> Result<::Slice, E> where I: StreamIsPartial, I: Stream, @@ -1505,7 +1500,7 @@ where } #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -fn take_unsigned_float_or_exceptions>(input: &mut I) -> ModalResult<(), E> +fn take_unsigned_float_or_exceptions>(input: &mut I) -> Result<(), E> where I: StreamIsPartial, I: Stream, @@ -1524,7 +1519,7 @@ where } #[allow(clippy::trait_duplication_in_bounds)] // HACK: clippy 1.64.0 bug -fn take_exp>(input: &mut I) -> ModalResult<(), E> +fn take_exp>(input: &mut I) -> Result<(), E> where I: StreamIsPartial, I: Stream, @@ -1650,7 +1645,7 @@ fn escaped_internal( normal: &mut F, control_char: char, escapable: &mut G, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -1806,7 +1801,7 @@ fn escaped_transform_internal( normal: &mut F, control_char: char, transform: &mut G, -) -> ModalResult +) -> Result where I: StreamIsPartial, I: Stream, diff --git a/src/ascii/tests.rs b/src/ascii/tests.rs index 9a1d9002..9c917064 100644 --- a/src/ascii/tests.rs +++ b/src/ascii/tests.rs @@ -2076,7 +2076,7 @@ Err( fn floats(s in "\\PC*") { println!("testing {s}"); let res1 = parse_f64.parse_peek(&s); - let res2 = float::<_, f64, ()>.parse_peek(s.as_str()); + let res2 = float::<_, f64, ErrMode<()>>.parse_peek(s.as_str()); assert_eq!(res1, res2); } } diff --git a/src/binary/bits/mod.rs b/src/binary/bits/mod.rs index 22bf5a23..18233d7c 100644 --- a/src/binary/bits/mod.rs +++ b/src/binary/bits/mod.rs @@ -8,7 +8,7 @@ use crate::combinator::trace; use crate::error::{ErrorConvert, ErrorKind, Needed, ParserError}; use crate::lib::std::ops::{AddAssign, Div, Shl, Shr}; use crate::stream::{Stream, StreamIsPartial, ToUsize}; -use crate::{ModalResult, Parser}; +use crate::{Parser, Result}; /// Number of bits in a byte const BYTE: usize = u8::BITS as usize; @@ -23,6 +23,7 @@ const BYTE: usize = u8::BITS as usize; /// # use winnow::Bytes; /// # use winnow::binary::bits::{bits, take}; /// # use winnow::error::ContextError; +/// # use winnow::error::ErrMode; /// type Stream<'i> = &'i Bytes; /// /// fn stream(b: &[u8]) -> Stream<'_> { @@ -30,7 +31,7 @@ const BYTE: usize = u8::BITS as usize; /// } /// /// fn parse(input: &mut Stream<'_>) -> ModalResult<(u8, u8)> { -/// bits::<_, _, ContextError, _, _>((take(4usize), take(8usize))).parse_next(input) +/// bits::<_, _, ErrMode, _, _>((take(4usize), take(8usize))).parse_next(input) /// } /// /// let input = stream(&[0x12, 0x34, 0xff, 0xff]); @@ -91,11 +92,12 @@ where /// # Examples /// /// ``` -/// use winnow::prelude::*; -/// use winnow::Bytes; +/// # use winnow::prelude::*; +/// # use winnow::Bytes; +/// # use winnow::token::rest; +/// # use winnow::error::ContextError; +/// # use winnow::error::ErrMode; /// use winnow::binary::bits::{bits, bytes, take}; -/// use winnow::token::rest; -/// use winnow::error::ContextError; /// /// type Stream<'i> = &'i Bytes; /// @@ -104,10 +106,10 @@ where /// } /// /// fn parse<'i>(input: &mut Stream<'i>) -> ModalResult<(u8, u8, &'i [u8])> { -/// bits::<_, _, ContextError, _, _>(( +/// bits::<_, _, ErrMode, _, _>(( /// take(4usize), /// take(8usize), -/// bytes::<_, _, ContextError, _, _>(rest) +/// bytes::<_, _, ErrMode, _, _>(rest) /// )).parse_next(input) /// } /// @@ -211,7 +213,7 @@ where fn take_, const PARTIAL: bool>( bit_input: &mut (I, usize), count: usize, -) -> ModalResult +) -> Result where I: StreamIsPartial, I: Stream + Clone, @@ -387,7 +389,7 @@ where #[doc(alias = "any")] pub fn bool>( input: &mut (Input, usize), -) -> ModalResult +) -> Result where Input: Stream + StreamIsPartial + Clone, { diff --git a/src/binary/bits/tests.rs b/src/binary/bits/tests.rs index fec9aa1d..e7900f5c 100644 --- a/src/binary/bits/tests.rs +++ b/src/binary/bits/tests.rs @@ -1,5 +1,7 @@ use super::*; +use crate::error::ErrMode; use crate::error::InputError; +use crate::prelude::*; use crate::Partial; #[test] @@ -11,8 +13,12 @@ fn test_complete_byte_consumption_bits() { // Take 3 bit slices with sizes [4, 8, 4]. #[allow(clippy::type_complexity)] let result: ModalResult<(&[u8], (u8, u8, u8)), InputError<_>> = - bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize), take(4usize))) - .parse_peek(input); + bits::<_, _, ErrMode>, _, _>(( + take(4usize), + take(8usize), + take(4usize), + )) + .parse_peek(input); let output = result.expect("We take 2 bytes and the input is longer than 2 bytes"); @@ -35,7 +41,7 @@ fn test_partial_byte_consumption_bits() { // Take bit slices with sizes [4, 8]. let result: ModalResult<(&[u8], (u8, u8)), InputError<_>> = - bits::<_, _, InputError<(&[u8], usize)>, _, _>((take(4usize), take(8usize))) + bits::<_, _, ErrMode>, _, _>((take(4usize), take(8usize))) .parse_peek(input); let output = result.expect("We take 1.5 bytes and the input is longer than 2 bytes"); @@ -56,7 +62,8 @@ fn test_incomplete_bits() { // Take bit slices with sizes [4, 8]. let result: ModalResult<(_, (u8, u8)), InputError<_>> = - bits::<_, _, InputError<(_, usize)>, _, _>((take(4usize), take(8usize))).parse_peek(input); + bits::<_, _, ErrMode>, _, _>((take(4usize), take(8usize))) + .parse_peek(input); assert!(result.is_err()); let error = result.err().unwrap(); diff --git a/src/binary/mod.rs b/src/binary/mod.rs index b092b40e..e6181cd2 100644 --- a/src/binary/mod.rs +++ b/src/binary/mod.rs @@ -16,8 +16,8 @@ use crate::lib::std::ops::{Add, Shl}; use crate::stream::Accumulate; use crate::stream::{Stream, StreamIsPartial}; use crate::stream::{ToUsize, UpdateSlice}; -use crate::ModalResult; use crate::Parser; +use crate::Result; /// Configurable endianness #[derive(Debug, PartialEq, Eq, Clone, Copy)] @@ -66,7 +66,7 @@ pub enum Endianness { /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_u8(input: &mut Input) -> ModalResult +pub fn be_u8(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -110,7 +110,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_u16(input: &mut Input) -> ModalResult +pub fn be_u16(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -154,7 +154,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn be_u24(input: &mut Input) -> ModalResult +pub fn be_u24(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -198,7 +198,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_u32(input: &mut Input) -> ModalResult +pub fn be_u32(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -242,7 +242,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_u64(input: &mut Input) -> ModalResult +pub fn be_u64(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -286,7 +286,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn be_u128(input: &mut Input) -> ModalResult +pub fn be_u128(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -295,7 +295,7 @@ where } #[inline] -fn be_uint(input: &mut Input, bound: usize) -> ModalResult +fn be_uint(input: &mut Input, bound: usize) -> Result where Input: StreamIsPartial + Stream, Uint: Default + Shl + Add + From, @@ -368,7 +368,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn be_i8(input: &mut Input) -> ModalResult +pub fn be_i8(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -412,7 +412,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn be_i16(input: &mut Input) -> ModalResult +pub fn be_i16(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -459,7 +459,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_i24(input: &mut Input) -> ModalResult +pub fn be_i24(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -514,7 +514,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(4)))); /// ``` #[inline(always)] -pub fn be_i32(input: &mut Input) -> ModalResult +pub fn be_i32(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -561,7 +561,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_i64(input: &mut Input) -> ModalResult +pub fn be_i64(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -608,7 +608,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn be_i128(input: &mut Input) -> ModalResult +pub fn be_i128(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -655,7 +655,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_u8(input: &mut Input) -> ModalResult +pub fn le_u8(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -699,7 +699,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_u16(input: &mut Input) -> ModalResult +pub fn le_u16(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -743,7 +743,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn le_u24(input: &mut Input) -> ModalResult +pub fn le_u24(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -787,7 +787,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_u32(input: &mut Input) -> ModalResult +pub fn le_u32(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -831,7 +831,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_u64(input: &mut Input) -> ModalResult +pub fn le_u64(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -875,7 +875,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn le_u128(input: &mut Input) -> ModalResult +pub fn le_u128(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -884,7 +884,7 @@ where } #[inline] -fn le_uint(input: &mut Input, bound: usize) -> ModalResult +fn le_uint(input: &mut Input, bound: usize) -> Result where Input: StreamIsPartial + Stream, Uint: Default + Shl + Add + From, @@ -956,7 +956,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_i8(input: &mut Input) -> ModalResult +pub fn le_i8(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1000,7 +1000,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn le_i16(input: &mut Input) -> ModalResult +pub fn le_i16(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1047,7 +1047,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(2)))); /// ``` #[inline(always)] -pub fn le_i24(input: &mut Input) -> ModalResult +pub fn le_i24(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1102,7 +1102,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_i32(input: &mut Input) -> ModalResult +pub fn le_i32(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1149,7 +1149,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_i64(input: &mut Input) -> ModalResult +pub fn le_i64(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1196,7 +1196,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b"\x01"[..])), Err(ErrMode::Incomplete(Needed::new(15)))); /// ``` #[inline(always)] -pub fn le_i128(input: &mut Input) -> ModalResult +pub fn le_i128(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1250,7 +1250,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn u8(input: &mut Input) -> ModalResult +pub fn u8(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1265,7 +1265,7 @@ where .parse_next(input) } -fn u8_(input: &mut Input) -> ModalResult +fn u8_(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -1677,7 +1677,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&b""[..])), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn i8(input: &mut Input) -> ModalResult +pub fn i8(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2085,7 +2085,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn be_f32(input: &mut Input) -> ModalResult +pub fn be_f32(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2132,7 +2132,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn be_f64(input: &mut Input) -> ModalResult +pub fn be_f64(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2179,7 +2179,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(3)))); /// ``` #[inline(always)] -pub fn le_f32(input: &mut Input) -> ModalResult +pub fn le_f32(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, @@ -2226,7 +2226,7 @@ where /// assert_eq!(parser.parse_peek(Partial::new(&[0x01][..])), Err(ErrMode::Incomplete(Needed::new(7)))); /// ``` #[inline(always)] -pub fn le_f64(input: &mut Input) -> ModalResult +pub fn le_f64(input: &mut Input) -> Result where Input: StreamIsPartial + Stream, Error: ParserError, diff --git a/src/combinator/branch.rs b/src/combinator/branch.rs index 693a049d..e277347f 100644 --- a/src/combinator/branch.rs +++ b/src/combinator/branch.rs @@ -1,5 +1,5 @@ use crate::combinator::trace; -use crate::error::{ErrMode, ErrorKind, ParserError}; +use crate::error::{ErrorKind, ParserError}; use crate::stream::Stream; use crate::*; @@ -11,7 +11,7 @@ pub use crate::dispatch; /// This trait is implemented for tuples of up to 21 elements pub trait Alt { /// Tests each parser in the tuple and returns the result of the first one that succeeds - fn choice(&mut self, input: &mut I) -> ModalResult; + fn choice(&mut self, input: &mut I) -> Result; } /// Pick the first successful parser @@ -64,7 +64,7 @@ where /// This trait is implemented for tuples of up to 21 elements pub trait Permutation { /// Tries to apply all parsers in the tuple in various orders until all of them succeed - fn permutation(&mut self, input: &mut I) -> ModalResult; + fn permutation(&mut self, input: &mut I) -> Result; } /// Applies a list of parsers in any order. @@ -127,8 +127,8 @@ pub fn permutation, List: Permutation>( } impl, P: Parser> Alt for [P; N] { - fn choice(&mut self, input: &mut I) -> ModalResult { - let mut error: Option> = None; + fn choice(&mut self, input: &mut I) -> Result { + let mut error: Option = None; let start = input.checkpoint(); for branch in self { @@ -155,8 +155,8 @@ impl, P: Parser> Alt, P: Parser> Alt for &mut [P] { - fn choice(&mut self, input: &mut I) -> ModalResult { - let mut error: Option> = None; + fn choice(&mut self, input: &mut I) -> Result { + let mut error: Option = None; let start = input.checkpoint(); for branch in self.iter_mut() { @@ -204,7 +204,7 @@ macro_rules! alt_trait_impl( $($id: Parser),+ > Alt for ( $($id),+ ) { - fn choice(&mut self, input: &mut I) -> ModalResult { + fn choice(&mut self, input: &mut I) -> Result { let start = input.checkpoint(); match self.0.parse_next(input) { Err(e) if e.is_backtrack() => alt_trait_inner!(1, self, input, start, e, $($id)+), @@ -259,7 +259,7 @@ alt_trait!(Alt2 Alt3 Alt4 Alt5 Alt6 Alt7 Alt8 Alt9 Alt10 Alt11 Alt12 Alt13 Alt14 // Manually implement Alt for (A,), the 1-tuple type impl, A: Parser> Alt for (A,) { - fn choice(&mut self, input: &mut I) -> ModalResult { + fn choice(&mut self, input: &mut I) -> Result { self.0.parse_next(input) } } @@ -291,11 +291,11 @@ macro_rules! permutation_trait_impl( $($name: Parser),+ > Permutation for ( $($name),+ ) { - fn permutation(&mut self, input: &mut I) -> ModalResult<( $($ty),+ ), Error> { + fn permutation(&mut self, input: &mut I) -> Result<( $($ty),+ ), Error> { let mut res = ($(Option::<$ty>::None),+); loop { - let mut err: Option> = None; + let mut err: Option = None; let start = input.checkpoint(); permutation_trait_inner!(0, self, input, start, res, err, $($name)+); diff --git a/src/combinator/core.rs b/src/combinator/core.rs index f5cba2e6..4ef61b7a 100644 --- a/src/combinator/core.rs +++ b/src/combinator/core.rs @@ -1,12 +1,12 @@ use crate::combinator::trace; -use crate::error::{ErrMode, ErrorKind, ModalError, ParserError}; +use crate::error::{ErrorKind, ModalError, ParserError}; use crate::stream::Stream; use crate::*; /// Deprecated, replaced with [`token::rest`] #[deprecated(since = "0.6.23", note = "replaced with `token::rest`")] #[inline] -pub fn rest(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn rest(input: &mut Input) -> Result<::Slice, Error> where Input: Stream, Error: ParserError, @@ -17,7 +17,7 @@ where /// Deprecated, replaced with [`token::rest_len`] #[deprecated(since = "0.6.23", note = "replaced with `token::rest_len`")] #[inline] -pub fn rest_len(input: &mut Input) -> ModalResult +pub fn rest_len(input: &mut Input) -> Result where Input: Stream, Error: ParserError, @@ -25,7 +25,7 @@ where crate::token::rest_len(input) } -/// Apply a [`Parser`], producing `None` on [`ErrMode::Backtrack`]. +/// Apply a [`Parser`], producing `None` on [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. /// /// To chain an error up, see [`cut_err`]. /// @@ -174,7 +174,7 @@ where /// ``` #[doc(alias = "end")] #[doc(alias = "eoi")] -pub fn eof(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn eof(input: &mut Input) -> Result<::Slice, Error> where Input: Stream, Error: ParserError, @@ -231,7 +231,7 @@ where }) } -/// Transforms an [`ErrMode::Backtrack`] (recoverable) to [`ErrMode::Cut`] (unrecoverable) +/// Transforms an [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack] (recoverable) to [`ErrMode::Cut`][crate::error::ErrMode::Cut] (unrecoverable) /// /// This commits the parse result, preventing alternative branch paths like with /// [`winnow::combinator::alt`][crate::combinator::alt]. @@ -292,7 +292,7 @@ pub fn cut_err( ) -> impl Parser where Input: Stream, - Error: ParserError, + Error: ParserError + ModalError, ParseNext: Parser, { trace("cut_err", move |input: &mut Input| { @@ -300,7 +300,7 @@ where }) } -/// Transforms an [`ErrMode::Cut`] (unrecoverable) to [`ErrMode::Backtrack`] (recoverable) +/// Transforms an [`ErrMode::Cut`][crate::error::ErrMode::Cut] (unrecoverable) to [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack] (recoverable) /// /// This attempts the parse, allowing other parsers to be tried on failure, like with /// [`winnow::combinator::alt`][crate::combinator::alt]. @@ -309,7 +309,7 @@ pub fn backtrack_err( ) -> impl Parser where Input: Stream, - Error: ParserError, + Error: ParserError + ModalError, ParseNext: Parser, { trace("backtrack_err", move |input: &mut Input| { @@ -336,7 +336,7 @@ where /// } /// ``` #[track_caller] -pub fn todo(input: &mut Input) -> ModalResult +pub fn todo(input: &mut Input) -> Result where Input: Stream, Error: ParserError, @@ -353,7 +353,7 @@ where /// Call the iterator's [`ParserIterator::finish`] method to get the remaining input if successful, /// or the error value if we encountered an error. /// -/// On [`ErrMode::Backtrack`], iteration will stop. To instead chain an error up, see [`cut_err`]. +/// On [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack], iteration will stop. To instead chain an error up, see [`cut_err`]. /// /// # Example /// @@ -396,7 +396,7 @@ where { parser: F, input: I, - state: Option>>, + state: Option>, o: core::marker::PhantomData, } @@ -407,7 +407,7 @@ where E: ParserError, { /// Returns the remaining input if parsing was successful, or the error if we encountered an error. - pub fn finish(mut self) -> ModalResult<(I, ()), E> { + pub fn finish(mut self) -> Result<(I, ()), E> { match self.state.take().unwrap() { State::Running | State::Done => Ok((self.input, ())), State::Cut(e) => Err(e), @@ -491,7 +491,7 @@ enum State { #[doc(alias = "value")] #[doc(alias = "success")] #[inline] -pub fn empty(_input: &mut Input) -> ModalResult<(), Error> +pub fn empty(_input: &mut Input) -> Result<(), Error> where Input: Stream, Error: ParserError, @@ -519,7 +519,7 @@ where /// ``` #[doc(alias = "unexpected")] #[inline] -pub fn fail(i: &mut Input) -> ModalResult +pub fn fail(i: &mut Input) -> Result where Input: Stream, Error: ParserError, diff --git a/src/combinator/debug/internals.rs b/src/combinator/debug/internals.rs index d1ab0de6..2f38f8c0 100644 --- a/src/combinator/debug/internals.rs +++ b/src/combinator/debug/internals.rs @@ -49,7 +49,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { let depth = Depth::new(); let original = i.checkpoint(); start(*depth, &self.name, self.call_count, i); diff --git a/src/combinator/impls.rs b/src/combinator/impls.rs index 51f6bd9d..5dd34444 100644 --- a/src/combinator/impls.rs +++ b/src/combinator/impls.rs @@ -6,7 +6,7 @@ use crate::combinator::DisplayDebug; #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] use crate::error::FromRecoverableError; -use crate::error::{AddContext, ErrMode, ErrorKind, FromExternalError, ParserError}; +use crate::error::{AddContext, ErrorKind, FromExternalError, ParserError}; use crate::lib::std::borrow::Borrow; use crate::lib::std::ops::Range; #[cfg(feature = "unstable-recover")] @@ -29,7 +29,7 @@ where P: Parser, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { self.p.parse_next(i) } } @@ -54,7 +54,7 @@ where G: FnMut(O) -> O2, { #[inline] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { match self.parser.parse_next(i) { Err(e) => Err(e), Ok(o) => Ok((self.map)(o)), @@ -89,12 +89,12 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult { + fn parse_next(&mut self, input: &mut I) -> Result { let start = input.checkpoint(); let o = self.parser.parse_next(input)?; let res = (self.map)(o).map_err(|err| { input.reset(&start); - ErrMode::from_external_error(input, ErrorKind::Verify, err) + E::from_external_error(input, ErrorKind::Verify, err) }); trace_result("verify", &res); res @@ -125,7 +125,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult { + fn parse_next(&mut self, input: &mut I) -> Result { let start = input.checkpoint(); let o = self.parser.parse_next(input)?; let res = (self.map)(o).ok_or_else(|| { @@ -161,7 +161,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { let start = i.checkpoint(); let mut o = self.outer.parse_next(i)?; let _ = o.complete(); @@ -196,7 +196,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { let start = i.checkpoint(); let o = self.p.parse_next(i)?; let res = o.parse_slice().ok_or_else(|| { @@ -231,7 +231,7 @@ where H: Parser, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { let o = self.f.parse_next(i)?; (self.g)(o).parse_next(i) } @@ -252,7 +252,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult { + fn parse_next(&mut self, input: &mut I) -> Result { trace("complete_err", |input: &mut I| { match (self.p).parse_next(input) { Err(err) => match err.into_needed() { @@ -294,7 +294,7 @@ where E: ParserError, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult { + fn parse_next(&mut self, input: &mut I) -> Result { let start = input.checkpoint(); let o = self.parser.parse_next(input)?; let res = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| { @@ -325,7 +325,7 @@ where O2: Clone, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult { + fn parse_next(&mut self, input: &mut I) -> Result { (self.parser).parse_next(input).map(|_| self.val.clone()) } } @@ -349,7 +349,7 @@ where O2: core::default::Default, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult { + fn parse_next(&mut self, input: &mut I) -> Result { (self.parser).parse_next(input).map(|_| O2::default()) } } @@ -370,7 +370,7 @@ where F: Parser, { #[inline(always)] - fn parse_next(&mut self, input: &mut I) -> ModalResult<(), E> { + fn parse_next(&mut self, input: &mut I) -> Result<(), E> { (self.parser).parse_next(input).map(|_| ()) } } @@ -397,7 +397,7 @@ where I: Stream, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, input: &mut I) -> Result<::Slice, E> { let checkpoint = input.checkpoint(); match (self.parser).parse_next(input) { Ok(_) => { @@ -433,7 +433,7 @@ where I: Stream, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult<(O, ::Slice), E> { + fn parse_next(&mut self, input: &mut I) -> Result<(O, ::Slice), E> { let checkpoint = input.checkpoint(); match (self.parser).parse_next(input) { Ok(result) => { @@ -465,7 +465,7 @@ where I: Stream + Location, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult, E> { + fn parse_next(&mut self, input: &mut I) -> Result, E> { let start = input.current_token_start(); self.parser.parse_next(input).map(move |_| { let end = input.previous_token_end(); @@ -492,7 +492,7 @@ where I: Stream + Location, { #[inline] - fn parse_next(&mut self, input: &mut I) -> ModalResult<(O, Range), E> { + fn parse_next(&mut self, input: &mut I) -> Result<(O, Range), E> { let start = input.current_token_start(); self.parser.parse_next(input).map(move |output| { let end = input.previous_token_end(); @@ -520,7 +520,7 @@ where O: Into, { #[inline] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { self.parser.parse_next(i).map(|o| o.into()) } } @@ -544,10 +544,8 @@ where E: Into, { #[inline] - fn parse_next(&mut self, i: &mut I) -> ModalResult { - self.parser - .parse_next(i) - .map_err(|err| err.map(|e| e.into())) + fn parse_next(&mut self, i: &mut I) -> Result { + self.parser.parse_next(i).map_err(|err| err.into()) } } @@ -576,7 +574,7 @@ where C: Clone + crate::lib::std::fmt::Debug, { #[inline] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { let context = self.context.clone(); trace(DisplayDebug(self.context.clone()), move |i: &mut I| { let start = i.checkpoint(); @@ -617,7 +615,7 @@ where E: ParserError + FromRecoverableError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { if I::is_recovery_supported() { retry_after_inner(&mut self.parser, &mut self.recover, i) } else { @@ -628,7 +626,7 @@ where #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] -fn retry_after_inner(parser: &mut P, recover: &mut R, i: &mut I) -> ModalResult +fn retry_after_inner(parser: &mut P, recover: &mut R, i: &mut I) -> Result where P: Parser, R: Parser, @@ -659,7 +657,7 @@ where } i.reset(&err_start); - err = err.map(|err| E::from_recoverable_error(&token_start, &err_start, i, err)); + err = E::from_recoverable_error(&token_start, &err_start, i, err); return Err(err); } } @@ -693,7 +691,7 @@ where E: ParserError + FromRecoverableError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult, E> { + fn parse_next(&mut self, i: &mut I) -> Result, E> { if I::is_recovery_supported() { resume_after_inner(&mut self.parser, &mut self.recover, i) } else { @@ -708,7 +706,7 @@ fn resume_after_inner( parser: &mut P, recover: &mut R, i: &mut I, -) -> ModalResult, E> +) -> Result, E> where P: Parser, R: Parser, diff --git a/src/combinator/multi.rs b/src/combinator/multi.rs index 2444f737..44287110 100644 --- a/src/combinator/multi.rs +++ b/src/combinator/multi.rs @@ -1,19 +1,18 @@ //! Combinators applying their child parser multiple times use crate::combinator::trace; -use crate::error::ErrMode; use crate::error::ErrorKind; use crate::error::FromExternalError; use crate::error::ParserError; use crate::stream::Accumulate; use crate::stream::Range; use crate::stream::Stream; -use crate::ModalResult; use crate::Parser; +use crate::Result; /// [`Accumulate`] the output of a parser into a container, like `Vec` /// -/// This stops before `n` when the parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see +/// This stops before `n` when the parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. /// /// To take a series of tokens, [`Accumulate`] into a `()` @@ -157,7 +156,7 @@ where { /// Repeats the embedded parser, calling `op` to gather the results /// - /// This stops before `n` when the parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see + /// This stops before `n` when the parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. /// /// # Arguments @@ -286,7 +285,7 @@ where /// Akin to [`Repeat::fold`], but for containers that can reject an element. /// - /// This stops before `n` when the parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see + /// This stops before `n` when the parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. Additionally, if the fold function returns `None`, the parser will /// stop and return an error. /// @@ -362,7 +361,7 @@ where /// Akin to [`Repeat::fold`], but for containers that can error when an element is accumulated. /// - /// This stops before `n` when the parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see + /// This stops before `n` when the parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. Additionally, if the fold function returns an error, the parser will /// stop and return it. /// @@ -442,7 +441,7 @@ where E: ParserError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { let Range { start_inclusive, end_inclusive, @@ -459,7 +458,7 @@ where } } -fn repeat0_(f: &mut F, i: &mut I) -> ModalResult +fn repeat0_(f: &mut F, i: &mut I) -> Result where I: Stream, C: Accumulate, @@ -491,7 +490,7 @@ where } } -fn repeat1_(f: &mut F, i: &mut I) -> ModalResult +fn repeat1_(f: &mut F, i: &mut I) -> Result where I: Stream, C: Accumulate, @@ -531,7 +530,7 @@ where } } -fn repeat_n_(count: usize, f: &mut F, i: &mut I) -> ModalResult +fn repeat_n_(count: usize, f: &mut F, i: &mut I) -> Result where I: Stream, C: Accumulate, @@ -564,12 +563,7 @@ where Ok(res) } -fn repeat_m_n_( - min: usize, - max: usize, - parse: &mut F, - input: &mut I, -) -> ModalResult +fn repeat_m_n_(min: usize, max: usize, parse: &mut F, input: &mut I) -> Result where I: Stream, C: Accumulate, @@ -621,7 +615,7 @@ where /// /// Returns a tuple of the results of `f` in a `Vec` and the result of `g`. /// -/// `f` keeps going so long as `g` produces [`ErrMode::Backtrack`]. To instead chain an error up, see [`cut_err`][crate::combinator::cut_err]. +/// `f` keeps going so long as `g` produces [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see [`cut_err`][crate::combinator::cut_err]. /// /// To take a series of tokens, [`Accumulate`] into a `()` /// (e.g. with [`.map(|((), _)| ())`][Parser::map]) @@ -681,7 +675,7 @@ where }) } -fn repeat_till0_(f: &mut F, g: &mut G, i: &mut I) -> ModalResult<(C, P), E> +fn repeat_till0_(f: &mut F, g: &mut G, i: &mut I) -> Result<(C, P), E> where I: Stream, C: Accumulate, @@ -723,7 +717,7 @@ fn repeat_till_m_n_( f: &mut F, g: &mut G, i: &mut I, -) -> ModalResult<(C, P), E> +) -> Result<(C, P), E> where I: Stream, C: Accumulate, @@ -786,7 +780,7 @@ where /// [`Accumulate`] the output of a parser, interleaved with `sep` /// -/// This stops when either parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see +/// This stops when either parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. /// /// To take a series of tokens, [`Accumulate`] into a `()` @@ -922,7 +916,7 @@ fn separated0_( parser: &mut P, separator: &mut S, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, C: Accumulate, @@ -981,7 +975,7 @@ fn separated1_( parser: &mut P, separator: &mut S, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, C: Accumulate, @@ -1037,7 +1031,7 @@ fn separated_n_( parser: &mut P, separator: &mut S, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, C: Accumulate, @@ -1098,7 +1092,7 @@ fn separated_m_n_( parser: &mut P, separator: &mut S, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, C: Accumulate, @@ -1180,7 +1174,7 @@ where /// Alternates between two parsers, merging the results (left associative) /// -/// This stops when either parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see +/// This stops when either parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. /// /// # Example @@ -1250,7 +1244,7 @@ where /// Alternates between two parsers, merging the results (right associative) /// -/// This stops when either parser returns [`ErrMode::Backtrack`]. To instead chain an error up, see +/// This stops when either parser returns [`ErrMode::Backtrack`][crate::error::ErrMode::Backtrack]. To instead chain an error up, see /// [`cut_err`][crate::combinator::cut_err]. /// /// # Example @@ -1354,7 +1348,7 @@ fn fold_repeat0_( init: &mut H, g: &mut G, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, F: Parser, @@ -1395,7 +1389,7 @@ fn fold_repeat1_( init: &mut H, g: &mut G, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, F: Parser, @@ -1445,7 +1439,7 @@ fn fold_repeat_m_n_( init: &mut H, fold: &mut G, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, F: Parser, @@ -1500,7 +1494,7 @@ fn verify_fold_m_n( init: &mut H, fold: &mut G, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, F: Parser, @@ -1561,7 +1555,7 @@ fn try_fold_m_n( init: &mut H, fold: &mut G, input: &mut I, -) -> ModalResult +) -> Result where I: Stream, F: Parser, @@ -1594,7 +1588,7 @@ where Ok(tmp) => acc = tmp, Err(e) => { input.reset(&start); - let res = Err(ErrMode::from_external_error(input, ErrorKind::Verify, e)); + let res = Err(E::from_external_error(input, ErrorKind::Verify, e)); super::debug::trace_result("try_fold", &res); return res; } diff --git a/src/macros/seq.rs b/src/macros/seq.rs index 95d30a3c..dd789fe4 100644 --- a/src/macros/seq.rs +++ b/src/macros/seq.rs @@ -16,6 +16,7 @@ /// # use winnow::combinator::delimited; /// # use winnow::combinator::empty; /// # use winnow::error::ContextError; +/// # use winnow::error::ErrMode; /// use winnow::combinator::seq; /// /// #[derive(Default, Debug, PartialEq)] @@ -44,7 +45,7 @@ /// /// // Or parse into tuples /// fn point(input: &mut &[u8]) -> ModalResult<(u32, u32)> { -/// let mut num = dec_uint::<_, u32, ContextError>; +/// let mut num = dec_uint::<_, u32, ErrMode>; /// seq!(num, _: (space0, b',', space0), num).parse_next(input) /// } /// diff --git a/src/parser.rs b/src/parser.rs index 6625dc97..382709d5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,7 +5,7 @@ use crate::combinator::impls; #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] use crate::error::FromRecoverableError; -use crate::error::{AddContext, FromExternalError, ModalResult, ParseError, ParserError}; +use crate::error::{AddContext, FromExternalError, ParseError, ParserError, Result}; use crate::stream::{Compare, Location, ParseSlice, Stream, StreamIsPartial}; #[cfg(feature = "unstable-recover")] #[cfg(feature = "std")] @@ -48,13 +48,14 @@ use crate::stream::{Recover, Recoverable}; pub trait Parser { /// Parse all of `input`, generating `O` from it #[inline] - fn parse(&mut self, mut input: I) -> Result> + fn parse(&mut self, mut input: I) -> Result>::Inner>> where Self: core::marker::Sized, I: Stream, // Force users to deal with `Incomplete` when `StreamIsPartial` I: StreamIsPartial, E: ParserError, + >::Inner: ParserError, { debug_assert!( !I::is_partial_supported(), @@ -78,7 +79,7 @@ pub trait Parser { /// This includes advancing the [`Stream`] to the next location. /// /// On error, `input` will be left pointing at the error location. - fn parse_next(&mut self, input: &mut I) -> ModalResult; + fn parse_next(&mut self, input: &mut I) -> Result; /// Take tokens from the [`Stream`], turning it into the output /// @@ -95,7 +96,7 @@ pub trait Parser { /// /// #[inline(always)] - fn parse_peek(&mut self, mut input: I) -> ModalResult<(I, O), E> { + fn parse_peek(&mut self, mut input: I) -> Result<(I, O), E> { match self.parse_next(&mut input) { Ok(o) => Ok((input, o)), Err(err) => Err(err), @@ -895,11 +896,11 @@ pub trait Parser { impl Parser for F where - F: FnMut(&mut I) -> ModalResult, + F: FnMut(&mut I) -> Result, I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { self(i) } } @@ -927,7 +928,7 @@ where E: ParserError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { crate::token::literal(*self).value(*self).parse_next(i) } } @@ -955,7 +956,7 @@ where E: ParserError, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { crate::token::literal(*self).value(*self).parse_next(i) } } @@ -984,7 +985,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> Result<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1016,7 +1017,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> Result<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1045,7 +1046,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> Result<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1078,7 +1079,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> Result<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1107,7 +1108,7 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> Result<::Slice, E> { crate::token::literal(*self).parse_next(i) } } @@ -1139,14 +1140,14 @@ where I: Stream, { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<::Slice, E> { + fn parse_next(&mut self, i: &mut I) -> Result<::Slice, E> { crate::token::literal(*self).parse_next(i) } } impl> Parser for () { #[inline(always)] - fn parse_next(&mut self, _i: &mut I) -> ModalResult<(), E> { + fn parse_next(&mut self, _i: &mut I) -> Result<(), E> { Ok(()) } } @@ -1159,7 +1160,7 @@ macro_rules! impl_parser_for_tuple { $($parser: Parser),+ { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult<($($output),+,), E> { + fn parse_next(&mut self, i: &mut I) -> Result<($($output),+,), E> { $(let $output = self.$index.parse_next(i)?;)+ Ok(($($output),+,)) @@ -1212,7 +1213,7 @@ use crate::lib::std::boxed::Box; #[cfg(feature = "alloc")] impl Parser for Box + '_> { #[inline(always)] - fn parse_next(&mut self, i: &mut I) -> ModalResult { + fn parse_next(&mut self, i: &mut I) -> Result { (**self).parse_next(i) } } @@ -1264,9 +1265,6 @@ where let (o, err) = match result { Ok((o, _)) => (Some(o), None), Err(err) => { - let err = err.into_inner().unwrap_or_else(|_err| { - panic!("complete parsers should not report `ErrMode::Incomplete(_)`") - }); let err_start = input.checkpoint(); let err = R::from_recoverable_error(&start_token, &err_start, &input, err); (None, Some(err)) @@ -1289,7 +1287,7 @@ where #[allow(deprecated)] pub fn unpeek<'a, I, O, E>( mut peek: impl FnMut(I) -> crate::error::IResult + 'a, -) -> impl FnMut(&mut I) -> ModalResult +) -> impl FnMut(&mut I) -> crate::error::ModalResult where I: Clone, { @@ -1328,8 +1326,8 @@ mod tests { #[test] #[cfg(target_pointer_width = "64")] fn size_test() { - assert_size!(ModalResult<&[u8], (&[u8], u32)>, 40); - assert_size!(ModalResult<&str, u32>, 40); + assert_size!(Result<&[u8], (&[u8], u32)>, 40); + assert_size!(Result<&str, u32>, 40); assert_size!(Needed, 8); assert_size!(ErrMode, 16); assert_size!(ErrorKind, 1); diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 3abcfcd1..cd524f7c 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -370,7 +370,7 @@ impl crate::lib::std::fmt::Display for Stat /// Here is how it works in practice: /// /// ```rust -/// # use winnow::{ModalResult, error::ErrMode, error::Needed, error::{ContextError, ErrorKind}, token, ascii, stream::Partial}; +/// # use winnow::{Result, error::ErrMode, error::Needed, error::{ContextError, ErrorKind}, token, ascii, stream::Partial}; /// # use winnow::prelude::*; /// /// fn take_partial<'s>(i: &mut Partial<&'s [u8]>) -> ModalResult<&'s [u8], ContextError> { @@ -1387,8 +1387,8 @@ pub trait Recover: Stream { &mut self, token_start: &Self::Checkpoint, err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode>; + err: E, + ) -> Result<(), E>; /// Report whether the [`Stream`] can save off errors for recovery fn is_recovery_supported() -> bool; @@ -1405,8 +1405,8 @@ where &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1425,8 +1425,8 @@ impl Recover for &str { &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1445,8 +1445,8 @@ impl Recover for &Bytes { &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1465,8 +1465,8 @@ impl Recover for &BStr { &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1489,8 +1489,8 @@ where &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1513,8 +1513,8 @@ where &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1538,16 +1538,15 @@ where &mut self, token_start: &Self::Checkpoint, err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { if self.is_recoverable { - match err { - ErrMode::Incomplete(need) => Err(ErrMode::Incomplete(need)), - ErrMode::Backtrack(err) | ErrMode::Cut(err) => { - self.errors - .push(R::from_recoverable_error(token_start, err_start, self, err)); - Ok(()) - } + if err.is_needed() { + Err(err) + } else { + self.errors + .push(R::from_recoverable_error(token_start, err_start, self, err)); + Ok(()) } } else { Err(err) @@ -1574,8 +1573,8 @@ where &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } @@ -1598,8 +1597,8 @@ where &mut self, _token_start: &Self::Checkpoint, _err_start: &Self::Checkpoint, - err: ErrMode, - ) -> Result<(), ErrMode> { + err: E, + ) -> Result<(), E> { Err(err) } diff --git a/src/stream/tests.rs b/src/stream/tests.rs index a851fa5b..59105852 100644 --- a/src/stream/tests.rs +++ b/src/stream/tests.rs @@ -1,6 +1,7 @@ #[cfg(feature = "std")] use proptest::prelude::*; +use crate::error::ErrMode; use crate::error::ErrMode::Backtrack; use crate::error::{ErrorKind, InputError}; use crate::token::literal; @@ -153,54 +154,56 @@ fn test_custom_slice() { #[test] fn test_literal_support_char() { assert_eq!( - literal::<_, _, InputError<_>>('π').parse_peek("π"), + literal::<_, _, ErrMode>>('π').parse_peek("π"), Ok(("", "π")) ); assert_eq!( - literal::<_, _, InputError<_>>('π').parse_peek("π3.14"), + literal::<_, _, ErrMode>>('π').parse_peek("π3.14"), Ok(("3.14", "π")) ); assert_eq!( - literal::<_, _, InputError<_>>("π").parse_peek("π3.14"), + literal::<_, _, ErrMode>>("π").parse_peek("π3.14"), Ok(("3.14", "π")) ); assert_eq!( - literal::<_, _, InputError<_>>('-').parse_peek("π"), + literal::<_, _, ErrMode>>('-').parse_peek("π"), Err(Backtrack(InputError::new("π", ErrorKind::Literal))) ); assert_eq!( - literal::<_, Partial<&[u8]>, InputError<_>>('π').parse_peek(Partial::new(b"\xCF\x80")), + literal::<_, Partial<&[u8]>, ErrMode>>('π') + .parse_peek(Partial::new(b"\xCF\x80")), Ok((Partial::new(Default::default()), "π".as_bytes())) ); assert_eq!( - literal::<_, &[u8], InputError<_>>('π').parse_peek(b"\xCF\x80"), + literal::<_, &[u8], ErrMode>>('π').parse_peek(b"\xCF\x80"), Ok((Default::default(), "π".as_bytes())) ); assert_eq!( - literal::<_, Partial<&[u8]>, InputError<_>>('π').parse_peek(Partial::new(b"\xCF\x803.14")), + literal::<_, Partial<&[u8]>, ErrMode>>('π') + .parse_peek(Partial::new(b"\xCF\x803.14")), Ok((Partial::new(&b"3.14"[..]), "π".as_bytes())) ); assert_eq!( - literal::<_, &[u8], InputError<_>>('π').parse_peek(b"\xCF\x80"), + literal::<_, &[u8], ErrMode>>('π').parse_peek(b"\xCF\x80"), Ok((Default::default(), "π".as_bytes())) ); assert_eq!( - literal::<_, &[u8], InputError<_>>('π').parse_peek(b"\xCF\x803.14"), + literal::<_, &[u8], ErrMode>>('π').parse_peek(b"\xCF\x803.14"), Ok((&b"3.14"[..], "π".as_bytes())) ); assert_eq!( - literal::<_, &[u8], InputError<_>>(AsciiCaseless('a')).parse_peek(b"ABCxyz"), + literal::<_, &[u8], ErrMode>>(AsciiCaseless('a')).parse_peek(b"ABCxyz"), Ok((&b"BCxyz"[..], &b"A"[..])) ); assert_eq!( - literal::<_, &[u8], InputError<_>>('a').parse_peek(b"ABCxyz"), + literal::<_, &[u8], ErrMode>>('a').parse_peek(b"ABCxyz"), Err(Backtrack(InputError::new( &b"ABCxyz"[..], ErrorKind::Literal @@ -208,24 +211,25 @@ fn test_literal_support_char() { ); assert_eq!( - literal::<_, &[u8], InputError<_>>(AsciiCaseless('π')).parse_peek(b"\xCF\x803.14"), + literal::<_, &[u8], ErrMode>>(AsciiCaseless('π')).parse_peek(b"\xCF\x803.14"), Ok((&b"3.14"[..], "π".as_bytes())) ); assert_eq!( - literal::<_, _, InputError<_>>(AsciiCaseless('🧑')).parse_peek("🧑你好"), + literal::<_, _, ErrMode>>(AsciiCaseless('🧑')).parse_peek("🧑你好"), Ok(("你好", "🧑")) ); let mut buffer = [0; 4]; let input = '\u{241b}'.encode_utf8(&mut buffer); assert_eq!( - literal::<_, &[u8], InputError<_>>(AsciiCaseless('␛')).parse_peek(input.as_bytes()), + literal::<_, &[u8], ErrMode>>(AsciiCaseless('␛')) + .parse_peek(input.as_bytes()), Ok((&b""[..], [226, 144, 155].as_slice())) ); assert_eq!( - literal::<_, &[u8], InputError<_>>('-').parse_peek(b"\xCF\x80"), + literal::<_, &[u8], ErrMode>>('-').parse_peek(b"\xCF\x80"), Err(Backtrack(InputError::new( &b"\xCF\x80"[..], ErrorKind::Literal diff --git a/src/token/mod.rs b/src/token/mod.rs index 7ef751ee..532b0322 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -12,8 +12,8 @@ use crate::lib::std::result::Result::Ok; use crate::stream::Range; use crate::stream::{Compare, CompareResult, ContainsToken, FindSlice, SliceLen, Stream}; use crate::stream::{StreamIsPartial, ToUsize}; -use crate::ModalResult; use crate::Parser; +use crate::Result; /// Matches one token /// @@ -49,12 +49,12 @@ use crate::Parser; /// # use winnow::{token::any, error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::prelude::*; /// # use winnow::Partial; -/// assert_eq!(any::<_, ContextError>.parse_peek(Partial::new("abc")), Ok((Partial::new("bc"),'a'))); -/// assert_eq!(any::<_, ContextError>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(any::<_, ErrMode>.parse_peek(Partial::new("abc")), Ok((Partial::new("bc"),'a'))); +/// assert_eq!(any::<_, ErrMode>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] #[doc(alias = "token")] -pub fn any(input: &mut Input) -> ModalResult<::Token, Error> +pub fn any(input: &mut Input) -> Result<::Token, Error> where Input: StreamIsPartial + Stream, Error: ParserError, @@ -69,9 +69,7 @@ where .parse_next(input) } -fn any_, const PARTIAL: bool>( - input: &mut I, -) -> ModalResult<::Token, E> +fn any_, const PARTIAL: bool>(input: &mut I) -> Result<::Token, E> where I: StreamIsPartial, I: Stream, @@ -181,7 +179,7 @@ where fn literal_, const PARTIAL: bool>( i: &mut I, t: T, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream + Compare, @@ -252,9 +250,9 @@ where /// # use winnow::{error::ErrMode, error::ErrorKind, error::ContextError, error::Needed}; /// # use winnow::Partial; /// # use winnow::token::one_of; -/// assert_eq!(one_of::<_, _, ContextError>(['a', 'b', 'c']).parse_peek(Partial::new("b")), Ok((Partial::new(""), 'b'))); -/// assert!(one_of::<_, _, ContextError>('a').parse_peek(Partial::new("bc")).is_err()); -/// assert_eq!(one_of::<_, _, ContextError>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(one_of::<_, _, ErrMode>(['a', 'b', 'c']).parse_peek(Partial::new("b")), Ok((Partial::new(""), 'b'))); +/// assert!(one_of::<_, _, ErrMode>('a').parse_peek(Partial::new("bc")).is_err()); +/// assert_eq!(one_of::<_, _, ErrMode>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// /// fn parser_fn(i: &mut Partial<&str>) -> ModalResult { /// one_of(|c| c == 'a' || c == 'b').parse_next(i) @@ -317,9 +315,9 @@ where /// # use winnow::prelude::*; /// # use winnow::Partial; /// # use winnow::token::none_of; -/// assert_eq!(none_of::<_, _, ContextError>(['a', 'b', 'c']).parse_peek(Partial::new("z")), Ok((Partial::new(""), 'z'))); -/// assert!(none_of::<_, _, ContextError>(['a', 'b']).parse_peek(Partial::new("a")).is_err()); -/// assert_eq!(none_of::<_, _, ContextError>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); +/// assert_eq!(none_of::<_, _, ErrMode>(['a', 'b', 'c']).parse_peek(Partial::new("z")), Ok((Partial::new(""), 'z'))); +/// assert!(none_of::<_, _, ErrMode>(['a', 'b']).parse_peek(Partial::new("a")).is_err()); +/// assert_eq!(none_of::<_, _, ErrMode>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] pub fn none_of( @@ -531,7 +529,7 @@ where fn take_till0, const PARTIAL: bool>( input: &mut I, predicate: P, -) -> ModalResult<::Slice, E> +) -> Result<::Slice, E> where P: FnMut(I::Token) -> bool, { @@ -548,7 +546,7 @@ where fn take_till1, const PARTIAL: bool>( input: &mut I, predicate: P, -) -> ModalResult<::Slice, E> +) -> Result<::Slice, E> where P: FnMut(I::Token) -> bool, { @@ -572,7 +570,7 @@ fn take_till_m_n, const PARTIAL: bool>( m: usize, n: usize, mut predicate: P, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -812,7 +810,7 @@ where fn take_, const PARTIAL: bool>( i: &mut I, c: usize, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream, @@ -963,7 +961,7 @@ where fn take_until0_, const PARTIAL: bool>( i: &mut I, t: T, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice, @@ -978,7 +976,7 @@ where fn take_until1_, const PARTIAL: bool>( i: &mut I, t: T, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice, @@ -1001,7 +999,7 @@ fn take_until_m_n_, const PARTIAL: bool>( start: usize, end: usize, t: T, -) -> ModalResult<::Slice, Error> +) -> Result<::Slice, Error> where I: StreamIsPartial, I: Stream + FindSlice, @@ -1058,7 +1056,7 @@ where /// assert_eq!(rest::<_,ContextError>.parse_peek(""), Ok(("", ""))); /// ``` #[inline] -pub fn rest(input: &mut Input) -> ModalResult<::Slice, Error> +pub fn rest(input: &mut Input) -> Result<::Slice, Error> where Input: Stream, Error: ParserError, @@ -1096,7 +1094,7 @@ where /// assert_eq!(rest_len::<_,ContextError>.parse_peek(""), Ok(("", 0))); /// ``` #[inline] -pub fn rest_len(input: &mut Input) -> ModalResult +pub fn rest_len(input: &mut Input) -> Result where Input: Stream, Error: ParserError, diff --git a/tests/testsuite/issues.rs b/tests/testsuite/issues.rs index e01c7723..bfcb6a15 100644 --- a/tests/testsuite/issues.rs +++ b/tests/testsuite/issues.rs @@ -367,7 +367,7 @@ Ok( fn issue_1231_bits_expect_fn_closure() { use winnow::binary::bits::{bits, take}; pub(crate) fn example<'i>(input: &mut &'i [u8]) -> TestResult<&'i [u8], (u8, u8)> { - bits::<_, _, InputError<_>, _, _>((take(1usize), take(1usize))).parse_next(input) + bits::<_, _, ErrMode>, _, _>((take(1usize), take(1usize))).parse_next(input) } assert_parse!( example.parse_peek(&[0xff]), diff --git a/tests/testsuite/reborrow_fold.rs b/tests/testsuite/reborrow_fold.rs index 4159e329..0ce9ff8a 100644 --- a/tests/testsuite/reborrow_fold.rs +++ b/tests/testsuite/reborrow_fold.rs @@ -5,13 +5,14 @@ use std::str; use winnow::combinator::delimited; use winnow::combinator::repeat; +use winnow::error::ErrMode; use winnow::error::InputError; use winnow::prelude::*; use winnow::token::take_till; use crate::TestResult; -fn atom<'a>(_tomb: &mut ()) -> impl Parser<&'a [u8], String, InputError<&'a [u8]>> { +fn atom<'a>(_tomb: &mut ()) -> impl Parser<&'a [u8], String, ErrMode>> { take_till(1.., [' ', '\t', '\r', '\n']) .try_map(str::from_utf8) .map(ToString::to_string) From e9727690030eab7dc1a935750f07b38d5720bab5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Jan 2025 10:24:21 -0600 Subject: [PATCH 13/17] feat: Add ModalParser alias for Parser --- examples/s_expression/parser.rs | 5 ++--- src/lib.rs | 1 + src/parser.rs | 5 +++++ tests/testsuite/reborrow_fold.rs | 3 +-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/s_expression/parser.rs b/examples/s_expression/parser.rs index 4673f180..e11b5f8d 100644 --- a/examples/s_expression/parser.rs +++ b/examples/s_expression/parser.rs @@ -2,7 +2,6 @@ //! parser and tiny [lisp](https://en.wikipedia.org/wiki/Lisp_(programming_language)) interpreter. //! Lisp is a simple type of language made up of Atoms and Lists, forming easily parsable trees. -use winnow::error::ErrMode; use winnow::{ ascii::{alpha1, digit1, multispace0, multispace1}, combinator::alt, @@ -225,9 +224,9 @@ fn parse_quote(i: &mut &'_ str) -> ModalResult { //.parse_next/ /// Unlike the previous functions, this function doesn't take or consume input, instead it /// takes a parsing function and returns a new parsing function. -fn s_exp<'a, O1, F>(inner: F) -> impl Parser<&'a str, O1, ErrMode> +fn s_exp<'a, O1, F>(inner: F) -> impl ModalParser<&'a str, O1, ContextError> where - F: Parser<&'a str, O1, ErrMode>, + F: ModalParser<&'a str, O1, ContextError>, { delimited( '(', diff --git a/src/lib.rs b/src/lib.rs index 6ec807fe..0ed34d1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,6 +150,7 @@ pub mod prelude { pub use crate::stream::ContainsToken as _; pub use crate::stream::Stream as _; pub use crate::stream::StreamIsPartial as _; + pub use crate::ModalParser; pub use crate::ModalResult; #[allow(deprecated)] pub use crate::PResult; diff --git a/src/parser.rs b/src/parser.rs index 382709d5..63459e82 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1218,6 +1218,11 @@ impl Parser for Box + '_> { } } +/// Trait alias for [`Parser`] to be used with [`ModalResult`][crate::error::ModalResult] +pub trait ModalParser: Parser> {} + +impl ModalParser for P where P: Parser> {} + /// Collect all errors when parsing the input /// /// [`Parser`]s will need to use [`Recoverable`] for their input. diff --git a/tests/testsuite/reborrow_fold.rs b/tests/testsuite/reborrow_fold.rs index 0ce9ff8a..a320423b 100644 --- a/tests/testsuite/reborrow_fold.rs +++ b/tests/testsuite/reborrow_fold.rs @@ -5,14 +5,13 @@ use std::str; use winnow::combinator::delimited; use winnow::combinator::repeat; -use winnow::error::ErrMode; use winnow::error::InputError; use winnow::prelude::*; use winnow::token::take_till; use crate::TestResult; -fn atom<'a>(_tomb: &mut ()) -> impl Parser<&'a [u8], String, ErrMode>> { +fn atom<'a>(_tomb: &mut ()) -> impl ModalParser<&'a [u8], String, InputError<&'a [u8]>> { take_till(1.., [' ', '\t', '\r', '\n']) .try_map(str::from_utf8) .map(ToString::to_string) From d2812bbc4c869cf7fabc5d924151ccd3c1217161 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Jan 2025 09:21:59 -0600 Subject: [PATCH 14/17] docs(tutorial): Update for ErrMode being optional --- src/_tutorial/chapter_1.rs | 11 +++--- src/_tutorial/chapter_2.rs | 46 +++++++++++------------ src/_tutorial/chapter_3.rs | 59 ++++++++++++++++------------- src/_tutorial/chapter_4.rs | 22 ++++++----- src/_tutorial/chapter_5.rs | 54 +++++++++++++------------- src/_tutorial/chapter_6.rs | 20 +++++----- src/_tutorial/chapter_7.rs | 77 ++++++++++++++++++++------------------ 7 files changed, 152 insertions(+), 137 deletions(-) diff --git a/src/_tutorial/chapter_1.rs b/src/_tutorial/chapter_1.rs index acbcc6b3..cf0b9069 100644 --- a/src/_tutorial/chapter_1.rs +++ b/src/_tutorial/chapter_1.rs @@ -23,14 +23,14 @@ //! ``` //! //! -//! To represent this model of the world, winnow uses the [`ModalResult`] type. +//! To represent this model of the world, winnow uses the [`Result`] type. //! The `Ok` variant has `output: O`; //! whereas the `Err` variant stores an error. //! //! You can import that from: //! //! ```rust -//! use winnow::ModalResult; +//! use winnow::Result; //! ``` //! //! To combine parsers, we need a common way to refer to them which is where the [`Parser`] @@ -49,7 +49,7 @@ //! The simplest parser we can write is one which successfully does nothing. //! //! To make it easier to implement a [`Parser`], the trait is implemented for -//! functions of the form `Fn(&mut I) -> ModalResult`. +//! functions of the form `Fn(&mut I) -> Result`. //! //! This parser function should take in a `&str`: //! @@ -58,10 +58,10 @@ //! - Since it doesn't parse anything, it also should just return an empty string. //! //! ```rust -//! use winnow::ModalResult; +//! use winnow::Result; //! use winnow::Parser; //! -//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! pub fn do_nothing_parser<'s>(input: &mut &'s str) -> Result<&'s str> { //! Ok("") //! } //! @@ -80,7 +80,6 @@ #![allow(unused_imports)] use super::chapter_6; use super::chapter_7; -use crate::ModalResult; use crate::Parser; pub use super::chapter_0 as previous; diff --git a/src/_tutorial/chapter_2.rs b/src/_tutorial/chapter_2.rs index 1415462a..073fe9d5 100644 --- a/src/_tutorial/chapter_2.rs +++ b/src/_tutorial/chapter_2.rs @@ -8,13 +8,12 @@ //! single token, you can do: //! ```rust //! # use winnow::Parser; -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::stream::Stream; //! use winnow::error::ParserError; //! use winnow::error::ErrorKind; -//! use winnow::error::ErrMode; //! -//! fn parse_prefix(input: &mut &str) -> ModalResult { +//! fn parse_prefix(input: &mut &str) -> Result { //! let c = input.next_token().ok_or_else(|| { //! ParserError::from_error_kind(input, ErrorKind::Token) //! })?; @@ -38,14 +37,13 @@ //! //! This extraction of a token is encapsulated in the [`any`] parser: //! ```rust -//! # use winnow::ModalResult; +//! # use winnow::Result; //! # use winnow::error::ParserError; //! # use winnow::error::ErrorKind; -//! # use winnow::error::ErrMode; //! use winnow::Parser; //! use winnow::token::any; //! -//! fn parse_prefix(input: &mut &str) -> ModalResult { +//! fn parse_prefix(input: &mut &str) -> Result { //! let c = any //! .parse_next(input)?; //! if c != '0' { @@ -69,11 +67,11 @@ //! Using the higher level [`any`] parser opens `parse_prefix` to the helpers on the [`Parser`] trait, //! like [`Parser::verify`] which fails a parse if a condition isn't met, like our check above: //! ```rust -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::Parser; //! use winnow::token::any; //! -//! fn parse_prefix(input: &mut &str) -> ModalResult { +//! fn parse_prefix(input: &mut &str) -> Result { //! let c = any //! .verify(|c| *c == '0') //! .parse_next(input)?; @@ -95,10 +93,10 @@ //! Matching a single token literal is common enough that [`Parser`] is implemented for //! the `char` type, encapsulating both [`any`] and [`Parser::verify`]: //! ```rust -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::Parser; //! -//! fn parse_prefix(input: &mut &str) -> ModalResult { +//! fn parse_prefix(input: &mut &str) -> Result { //! let c = '0'.parse_next(input)?; //! Ok(c) //! } @@ -120,13 +118,12 @@ //! [`Stream`] also supports processing slices of tokens: //! ```rust //! # use winnow::Parser; -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::stream::Stream; //! use winnow::error::ParserError; //! use winnow::error::ErrorKind; -//! use winnow::error::ErrMode; //! -//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> { //! let expected = "0x"; //! if input.len() < expected.len() { //! return Err(ParserError::from_error_kind(input, ErrorKind::Slice)); @@ -151,11 +148,11 @@ //! //! Matching the input position against a string literal is encapsulated in the [`literal`] parser: //! ```rust -//! # use winnow::ModalResult; +//! # use winnow::Result; //! # use winnow::Parser; //! use winnow::token::literal; //! -//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> { //! let expected = "0x"; //! let actual = literal(expected).parse_next(input)?; //! Ok(actual) @@ -174,10 +171,10 @@ //! //! Like for a single token, matching a string literal is common enough that [`Parser`] is implemented for the `&str` type: //! ```rust -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::Parser; //! -//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> { //! let actual = "0x".parse_next(input)?; //! Ok(actual) //! } @@ -202,10 +199,10 @@ //! //! ```rust //! # use winnow::Parser; -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::token::one_of; //! -//! fn parse_digits(input: &mut &str) -> ModalResult { +//! fn parse_digits(input: &mut &str) -> Result { //! one_of(('0'..='9', 'a'..='f', 'A'..='F')).parse_next(input) //! } //! @@ -235,7 +232,7 @@ //! > If you have not programmed in a language where functions are values, the type signature of the //! > [`one_of`] function might be a surprise. //! > The function [`one_of`] *returns a function*. The function it returns is a -//! > `Parser`, taking a `&str` and returning an `ModalResult`. This is a common pattern in winnow for +//! > [`Parser`], taking a `&str` and returning an [`Result`]. This is a common pattern in winnow for //! > configurable or stateful parsers. //! //! Some of character classes are common enough that a named parser is provided, like with: @@ -246,10 +243,10 @@ //! You can then capture sequences of these characters with parsers like [`take_while`]. //! ```rust //! # use winnow::Parser; -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::token::take_while; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! take_while(1.., ('0'..='9', 'a'..='f', 'A'..='F')).parse_next(input) //! } //! @@ -267,10 +264,10 @@ //! We could simplify this further by using one of the built-in character classes, [`hex_digit1`]: //! ```rust //! # use winnow::Parser; -//! # use winnow::ModalResult; +//! # use winnow::Result; //! use winnow::ascii::hex_digit1; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! hex_digit1.parse_next(input) //! } //! @@ -298,6 +295,7 @@ use crate::token::literal; use crate::token::one_of; use crate::token::take_while; use crate::Parser; +use crate::Result; use std::ops::RangeInclusive; pub use super::chapter_1 as previous; diff --git a/src/_tutorial/chapter_3.rs b/src/_tutorial/chapter_3.rs index 8d445448..0c2bf72a 100644 --- a/src/_tutorial/chapter_3.rs +++ b/src/_tutorial/chapter_3.rs @@ -11,13 +11,14 @@ //! //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # -//! fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> { //! "0x".parse_next(input) //! } //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! take_while(1.., ( //! ('0'..='9'), //! ('A'..='F'), @@ -40,13 +41,14 @@ //! To sequence these together, you can just put them in a tuple: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # -//! # fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> { //! # "0x".parse_next(input) //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -74,14 +76,15 @@ //! like [`preceded`]: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! use winnow::combinator::preceded; //! -//! # fn parse_prefix<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_prefix<'s>(input: &mut &'s str) -> Result<&'s str> { //! # "0x".parse_next(input) //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -115,10 +118,11 @@ //! back to that position with [`Stream::reset`]: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! use winnow::stream::Stream; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! let start = input.checkpoint(); //! if let Ok(output) = ("0b", parse_bin_digits).parse_next(input) { //! return Ok(output); @@ -139,25 +143,25 @@ //! } //! //! // ... -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -189,10 +193,11 @@ //! [`opt`] is a parser that encapsulates this pattern of "retry on failure": //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! use winnow::combinator::opt; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! if let Some(output) = opt(("0b", parse_bin_digits)).parse_next(input)? { //! Ok(output) //! } else if let Some(output) = opt(("0o", parse_oct_digits)).parse_next(input)? { @@ -204,25 +209,25 @@ //! } //! } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -246,10 +251,11 @@ //! [`alt`] encapsulates this if/else-if ladder pattern, with the last case being the "else": //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! use winnow::combinator::alt; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! alt(( //! ("0b", parse_bin_digits), //! ("0o", parse_oct_digits), @@ -258,25 +264,25 @@ //! )).parse_next(input) //! } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -309,12 +315,13 @@ //! //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! use winnow::combinator::dispatch; //! use winnow::token::take; //! use winnow::combinator::fail; //! -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! dispatch!(take(2usize); //! "0b" => parse_bin_digits, //! "0o" => parse_oct_digits, @@ -324,25 +331,25 @@ //! ).parse_next(input) //! } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), diff --git a/src/_tutorial/chapter_4.rs b/src/_tutorial/chapter_4.rs index 061896d1..c66a6a2b 100644 --- a/src/_tutorial/chapter_4.rs +++ b/src/_tutorial/chapter_4.rs @@ -1,15 +1,15 @@ //! # Chapter 4: Parsers With Custom Return Types //! //! So far, we have seen mostly functions that take an `&str`, and return a -//! `ModalResult<&str>`. Splitting strings into smaller strings and characters is certainly +//! [`Result<&str>`]. Splitting strings into smaller strings and characters is certainly //! useful, but it's not the only thing winnow is capable of! //! //! A useful operation when parsing is to convert between types; for example //! parsing from `&str` to another primitive, like [`usize`]. //! //! All we need to do for our parser to return a different type is to change -//! the type parameter of [`ModalResult`] to the desired return type. -//! For example, to return a `usize`, return a `ModalResult`. +//! the type parameter of [`Result`] to the desired return type. +//! For example, to return a `usize`, return a `Result`. //! //! One winnow-native way of doing a type conversion is to use the //! [`Parser::parse_to`] combinator @@ -18,9 +18,10 @@ //! The following code converts from a string containing a number to `usize`: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::ascii::digit1; //! # -//! fn parse_digits(input: &mut &str) -> ModalResult { +//! fn parse_digits(input: &mut &str) -> Result { //! digit1 //! .parse_to() //! .parse_next(input) @@ -41,12 +42,13 @@ //! all radices of numbers: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! use winnow::combinator::dispatch; //! use winnow::token::take; //! use winnow::combinator::fail; //! -//! fn parse_digits(input: &mut &str) -> ModalResult { +//! fn parse_digits(input: &mut &str) -> Result { //! dispatch!(take(2usize); //! "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -57,25 +59,25 @@ //! } //! //! // ... -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -98,8 +100,8 @@ //! See also [`Parser`] for more output-modifying parsers. #![allow(unused_imports)] -use crate::ModalResult; use crate::Parser; +use crate::Result; use std::str::FromStr; pub use super::chapter_3 as previous; diff --git a/src/_tutorial/chapter_5.rs b/src/_tutorial/chapter_5.rs index eaa3c18e..9bfb293a 100644 --- a/src/_tutorial/chapter_5.rs +++ b/src/_tutorial/chapter_5.rs @@ -6,6 +6,7 @@ //! Let's collect the result of `parse_digits`: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; @@ -14,7 +15,7 @@ //! use winnow::combinator::repeat; //! use winnow::combinator::terminated; //! -//! fn parse_list(input: &mut &str) -> ModalResult> { +//! fn parse_list(input: &mut &str) -> Result> { //! let mut list = Vec::new(); //! while let Some(output) = opt(terminated(parse_digits, opt(','))).parse_next(input)? { //! list.push(output); @@ -23,7 +24,7 @@ //! } //! //! // ... -//! # fn parse_digits(input: &mut &str) -> ModalResult { +//! # fn parse_digits(input: &mut &str) -> Result { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -33,25 +34,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -74,6 +75,7 @@ //! We can implement this declaratively with [`repeat`]: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; @@ -82,13 +84,13 @@ //! use winnow::combinator::repeat; //! use winnow::combinator::terminated; //! -//! fn parse_list(input: &mut &str) -> ModalResult> { +//! fn parse_list(input: &mut &str) -> Result> { //! repeat(0.., //! terminated(parse_digits, opt(',')) //! ).parse_next(input) //! } //! # -//! # fn parse_digits(input: &mut &str) -> ModalResult { +//! # fn parse_digits(input: &mut &str) -> Result { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -98,25 +100,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -140,18 +142,19 @@ //! can easily be fixed by using [`separated`] instead of [`repeat`]: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; //! # use winnow::combinator::fail; //! use winnow::combinator::separated; //! -//! fn parse_list(input: &mut &str) -> ModalResult> { +//! fn parse_list(input: &mut &str) -> Result> { //! separated(0.., parse_digits, ",").parse_next(input) //! } //! //! // ... -//! # fn parse_digits(input: &mut &str) -> ModalResult { +//! # fn parse_digits(input: &mut &str) -> Result { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -161,25 +164,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -217,21 +220,22 @@ //! //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; //! # use winnow::combinator::fail; //! # use winnow::combinator::separated; //! # -//! fn take_list<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! fn take_list<'s>(input: &mut &'s str) -> Result<&'s str> { //! parse_list.take().parse_next(input) //! } //! -//! fn parse_list(input: &mut &str) -> ModalResult<()> { +//! fn parse_list(input: &mut &str) -> Result<()> { //! separated(0.., parse_digits, ",").parse_next(input) //! } //! -//! # fn parse_digits(input: &mut &str) -> ModalResult { +//! # fn parse_digits(input: &mut &str) -> Result { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -241,25 +245,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), diff --git a/src/_tutorial/chapter_6.rs b/src/_tutorial/chapter_6.rs index 4fa8de24..e1d1b611 100644 --- a/src/_tutorial/chapter_6.rs +++ b/src/_tutorial/chapter_6.rs @@ -6,17 +6,16 @@ //! Parsers we've been working with look like: //! ```rust //! # use winnow::error::ContextError; -//! # use winnow::error::ErrMode; //! # use winnow::Parser; -//! use winnow::ModalResult; +//! use winnow::Result; //! -//! pub fn parser<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! pub fn parser<'s>(input: &mut &'s str) -> Result<&'s str> { //! // ... //! # Ok("") //! } //! ``` //! 1. We have to decide what to do about the "remainder" of the `input`. -//! 2. The [`ModalResult`] is not compatible with the rest of the Rust ecosystem. +//! 2. The [`Result`] may not be compatible with the rest of the Rust ecosystem. //! Normally, Rust applications want errors that are `std::error::Error + Send + Sync + 'static` //! meaning: //! - They implement the [`std::error::Error`] trait @@ -26,18 +25,19 @@ //! //! winnow provides [`Parser::parse`] to help with this: //! - Ensures we hit [`eof`] -//! - Converts from [`ModalResult`] to [`Result`] //! - Wraps the error in [`ParseError`] //! - For simple cases, [`ParseError`] provides a [`std::fmt::Display`] impl to render the error. //! - For more involved cases, [`ParseError`] provides the original [`input`][ParseError::input] and the //! [`offset`][ParseError::offset] of where it failed so you can capture this information in //! your error, [rendering it as you wish][chapter_7#error-adaptation-and-rendering]. +//! - Converts from [`ModalResult`] to [`Result`] (if used, more on this in [`chapter_7`]) //! //! However, [`ParseError`] will still need some level of adaptation to integrate with your //! application's error type (like with `?`). //! //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::dispatch; //! # use winnow::token::take; @@ -59,7 +59,7 @@ //! } //! //! // ... -//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult { +//! # fn parse_digits<'s>(input: &mut &'s str) -> Result { //! # dispatch!(take(2usize); //! # "0b" => parse_bin_digits.try_map(|s| usize::from_str_radix(s, 2)), //! # "0o" => parse_oct_digits.try_map(|s| usize::from_str_radix(s, 8)), @@ -69,25 +69,25 @@ //! # ).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), diff --git a/src/_tutorial/chapter_7.rs b/src/_tutorial/chapter_7.rs index 8f16f854..c7452474 100644 --- a/src/_tutorial/chapter_7.rs +++ b/src/_tutorial/chapter_7.rs @@ -6,6 +6,7 @@ //! the failure: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; //! # use winnow::token::take; @@ -35,7 +36,7 @@ //! # //! // ... //! -//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! # alt(( //! # ("0b", parse_bin_digits), //! # ("0o", parse_oct_digits), @@ -44,25 +45,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -79,14 +80,15 @@ //! } //! ``` //! -//! Back in [`chapter_1`], we glossed over the `Err` variant of [`ModalResult`]. `ModalResult` is -//! actually short for `ModalResult` where [`ContextError`] is a relatively cheap +//! Back in [`chapter_1`], we glossed over the `Err` variant of [`Result`]. `Result` is +//! actually short for `Result` where [`ContextError`] is a relatively cheap //! way of building up reasonable errors for humans. //! //! You can use [`Parser::context`] to annotate the error with custom types //! while unwinding to further clarify the error: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; //! # use winnow::token::take; @@ -117,7 +119,7 @@ //! # } //! # } //! # -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! alt(( //! ("0b", parse_bin_digits) //! .context(StrContext::Label("digit")) @@ -137,25 +139,25 @@ //! // ... //! //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -178,6 +180,7 @@ //! hexadecimal value: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; //! # use winnow::token::take; @@ -208,7 +211,7 @@ //! # } //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! # alt(( //! # ("0b", parse_bin_digits) //! # .context(StrContext::Label("digit")) @@ -225,25 +228,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -263,6 +266,7 @@ //! We can improve this with [`fail`]: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; //! # use winnow::token::take; @@ -293,7 +297,7 @@ //! # } //! # } //! # -//! fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! alt(( //! ("0b", parse_bin_digits) //! .context(StrContext::Label("digit")) @@ -319,25 +323,25 @@ //! // ... //! //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -361,6 +365,7 @@ //! don't match it: //! ```rust //! # use winnow::prelude::*; +//! # use winnow::Result; //! # use winnow::token::take_while; //! # use winnow::combinator::alt; //! # use winnow::token::take; @@ -391,7 +396,7 @@ //! # } //! # } //! # -//! # fn parse_digits<'s>(input: &mut &'s str) -> ModalResult<(&'s str, &'s str)> { +//! # fn parse_digits<'s>(input: &mut &'s str) -> Result<(&'s str, &'s str)> { //! # alt(( //! # ("0b", parse_bin_digits) //! # .context(StrContext::Label("digit")) @@ -414,25 +419,25 @@ //! # )).parse_next(input) //! # } //! # -//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_bin_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='1'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_oct_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='7'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_dec_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # )).parse_next(input) //! # } //! # -//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> ModalResult<&'s str> { +//! # fn parse_hex_digits<'s>(input: &mut &'s str) -> Result<&'s str> { //! # take_while(1.., ( //! # ('0'..='9'), //! # ('A'..='F'), @@ -450,21 +455,20 @@ //! } //! ``` //! -//! Let's break down `ModalResult` one step further: +//! Winnow provides an error wrapper, [`ErrMode`], so different failure modes can affect parsing. +//! [`ErrMode`] is an enum with [`Backtrack`] and [`Cut`] variants (ignore [`Incomplete`] as its only +//! relevant for [streaming][_topic::stream]). By default, errors are [`Backtrack`], meaning that +//! other parsing branches will be attempted on failure, like the next case of an [`alt`]. [`Cut`] +//! shortcircuits all other branches, immediately reporting the error. +//! +//! To make [`ErrMode`] more convenient, Winnow provides [`ModalResult`]: //! ```rust //! # use winnow::error::ErrorKind; //! # use winnow::error::ErrMode; //! pub type ModalResult = Result>; //! ``` -//! [`ModalResult`] is just a fancy wrapper around `Result` that wraps our error in an [`ErrMode`] -//! type. -//! -//! [`ErrMode`] is an enum with [`Backtrack`] and [`Cut`] variants (ignore [`Incomplete`] as its only -//! relevant for [streaming][_topic::stream]). By default, errors are [`Backtrack`], meaning that -//! other parsing branches will be attempted on failure, like the next case of an [`alt`]. [`Cut`] -//! shortcircuits all other branches, immediately reporting the error. //! -//! So we can get the correct `context` by modifying the above example with [`cut_err`]: +//! So we can get the correct `context` by changing to [`ModalResult`] and adding [`cut_err`]: //! ```rust //! # use winnow::prelude::*; //! # use winnow::token::take_while; @@ -721,6 +725,7 @@ use crate::error::ErrMode::*; use crate::error::ErrorKind; use crate::ModalResult; use crate::Parser; +use crate::Result; use crate::_topic; pub use super::chapter_6 as previous; From 172b995a59a56b7f4e6c653dd3dcf2c628d5045e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Jan 2025 09:30:05 -0600 Subject: [PATCH 15/17] docs(topic): Explain ErrMode change to Nom users --- src/_topic/nom.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/_topic/nom.rs b/src/_topic/nom.rs index 2245fb41..6141de20 100644 --- a/src/_topic/nom.rs +++ b/src/_topic/nom.rs @@ -99,6 +99,18 @@ //! }).parse_next(i) //! } //! ``` +//! +//! ### Optional [`ErrMode`] +//! +//! Called `Err` in `nom`, [`ErrMode`] is responsible for +//! - Deciding whether to backtrack and try another branch in cases like `alt` or report back to +//! the error back to users +//! - Tracking incomplete input on partial parsing +//! +//! As this isn't needed in every parser, it was made optional. [`ModalResult`] is a convenience +//! type for using [`ErrMode`]. #![allow(unused_imports)] +use crate::error::ErrMode; +use crate::error::ModalResult; use crate::stream::Stream; From 381da5b0dea0233af1ef94c99fb503029b0b4b5b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Jan 2025 09:32:33 -0600 Subject: [PATCH 16/17] docs(topic): Clarify streaming/complete API change --- src/_topic/nom.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/_topic/nom.rs b/src/_topic/nom.rs index 6141de20..2c0d940a 100644 --- a/src/_topic/nom.rs +++ b/src/_topic/nom.rs @@ -64,6 +64,13 @@ //! - Search the docs for the `nom` parser //! - See the [List of combinators][crate::combinator] //! +//! ### Partial/streaming parsers +//! +//! `nom` differentiated some parsers by being `streaming` or `complete`. +//! Instead, we tag the input type (`I`) by wrapping it in [`Partial`] and parsers will adjust +//! their behavior accordingly. +//! See [partial] special topic. +//! //! ### `&mut I` //! //! For an explanation of this change, see [Why `winnow`][super::why] @@ -111,6 +118,8 @@ //! type for using [`ErrMode`]. #![allow(unused_imports)] +use crate::_topic::partial; use crate::error::ErrMode; use crate::error::ModalResult; +use crate::stream::Partial; use crate::stream::Stream; From 1550ff31dacc015cfbef87db1b0a9879bba5742c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 24 Jan 2025 09:41:30 -0600 Subject: [PATCH 17/17] docs(examples): Move away from ModalResult where possible --- examples/arithmetic/parser.rs | 9 +++++---- examples/arithmetic/parser_ast.rs | 9 +++++---- examples/arithmetic/parser_lexer.rs | 15 ++++++++------- examples/css/parser.rs | 5 +++-- examples/ini/bench.rs | 3 ++- examples/ini/parser.rs | 7 ++++--- examples/ini/parser_str.rs | 15 ++++++++------- examples/string/parser.rs | 13 +++++++------ 8 files changed, 42 insertions(+), 34 deletions(-) diff --git a/examples/arithmetic/parser.rs b/examples/arithmetic/parser.rs index 2a7a5f1e..45460de1 100644 --- a/examples/arithmetic/parser.rs +++ b/examples/arithmetic/parser.rs @@ -1,6 +1,7 @@ use std::str::FromStr; use winnow::prelude::*; +use winnow::Result; use winnow::{ ascii::{digit1 as digits, multispace0 as multispaces}, combinator::alt, @@ -11,7 +12,7 @@ use winnow::{ // Parser definition -pub(crate) fn expr(i: &mut &str) -> ModalResult { +pub(crate) fn expr(i: &mut &str) -> Result { let init = term.parse_next(i)?; repeat(0.., (one_of(['+', '-']), term)) @@ -31,7 +32,7 @@ pub(crate) fn expr(i: &mut &str) -> ModalResult { // We read an initial factor and for each time we find // a * or / operator followed by another factor, we do // the math by folding everything -fn term(i: &mut &str) -> ModalResult { +fn term(i: &mut &str) -> Result { let init = factor.parse_next(i)?; repeat(0.., (one_of(['*', '/']), factor)) @@ -52,7 +53,7 @@ fn term(i: &mut &str) -> ModalResult { // We look for a digit suite, and try to convert it. // If either str::from_utf8 or FromStr::from_str fail, // we fallback to the parens parser defined above -fn factor(i: &mut &str) -> ModalResult { +fn factor(i: &mut &str) -> Result { delimited( multispaces, alt((digits.try_map(FromStr::from_str), parens)), @@ -62,7 +63,7 @@ fn factor(i: &mut &str) -> ModalResult { } // We parse any expr surrounded by parens, ignoring all whitespace around those -fn parens(i: &mut &str) -> ModalResult { +fn parens(i: &mut &str) -> Result { delimited('(', expr, ')').parse_next(i) } diff --git a/examples/arithmetic/parser_ast.rs b/examples/arithmetic/parser_ast.rs index e7bc6e11..1cd359b0 100644 --- a/examples/arithmetic/parser_ast.rs +++ b/examples/arithmetic/parser_ast.rs @@ -4,6 +4,7 @@ use std::fmt::{Debug, Display, Formatter}; use std::str::FromStr; use winnow::prelude::*; +use winnow::Result; use winnow::{ ascii::{digit1 as digits, multispace0 as multispaces}, combinator::alt, @@ -49,7 +50,7 @@ impl Display for Expr { } } -pub(crate) fn expr(i: &mut &str) -> ModalResult { +pub(crate) fn expr(i: &mut &str) -> Result { let init = term.parse_next(i)?; repeat(0.., (one_of(['+', '-']), term)) @@ -66,7 +67,7 @@ pub(crate) fn expr(i: &mut &str) -> ModalResult { .parse_next(i) } -fn term(i: &mut &str) -> ModalResult { +fn term(i: &mut &str) -> Result { let init = factor.parse_next(i)?; repeat(0.., (one_of(['*', '/']), factor)) @@ -83,7 +84,7 @@ fn term(i: &mut &str) -> ModalResult { .parse_next(i) } -fn factor(i: &mut &str) -> ModalResult { +fn factor(i: &mut &str) -> Result { delimited( multispaces, alt((digits.try_map(FromStr::from_str).map(Expr::Value), parens)), @@ -92,7 +93,7 @@ fn factor(i: &mut &str) -> ModalResult { .parse_next(i) } -fn parens(i: &mut &str) -> ModalResult { +fn parens(i: &mut &str) -> Result { delimited("(", expr, ")") .map(|e| Expr::Paren(Box::new(e))) .parse_next(i) diff --git a/examples/arithmetic/parser_lexer.rs b/examples/arithmetic/parser_lexer.rs index 5a5eaea1..46ecc178 100644 --- a/examples/arithmetic/parser_lexer.rs +++ b/examples/arithmetic/parser_lexer.rs @@ -4,6 +4,7 @@ use std::fmt::{Debug, Display, Formatter}; use std::str::FromStr; use winnow::prelude::*; +use winnow::Result; use winnow::{ ascii::{digit1 as digits, multispace0 as multispaces}, combinator::alt, @@ -98,16 +99,16 @@ impl winnow::stream::ContainsToken for [Token; LEN] { } #[allow(dead_code)] -pub(crate) fn expr2(i: &mut &str) -> ModalResult { +pub(crate) fn expr2(i: &mut &str) -> Result { let tokens = lex.parse_next(i)?; expr.parse_next(&mut tokens.as_slice()) } -pub(crate) fn lex(i: &mut &str) -> ModalResult> { +pub(crate) fn lex(i: &mut &str) -> Result> { preceded(multispaces, repeat(1.., terminated(token, multispaces))).parse_next(i) } -fn token(i: &mut &str) -> ModalResult { +fn token(i: &mut &str) -> Result { dispatch! {peek(any); '0'..='9' => digits.try_map(FromStr::from_str).map(Token::Value), '(' => '('.value(Token::OpenParen), @@ -121,7 +122,7 @@ fn token(i: &mut &str) -> ModalResult { .parse_next(i) } -pub(crate) fn expr(i: &mut &[Token]) -> ModalResult { +pub(crate) fn expr(i: &mut &[Token]) -> Result { let init = term.parse_next(i)?; repeat( @@ -144,7 +145,7 @@ pub(crate) fn expr(i: &mut &[Token]) -> ModalResult { .parse_next(i) } -fn term(i: &mut &[Token]) -> ModalResult { +fn term(i: &mut &[Token]) -> Result { let init = factor.parse_next(i)?; repeat( @@ -167,7 +168,7 @@ fn term(i: &mut &[Token]) -> ModalResult { .parse_next(i) } -fn factor(i: &mut &[Token]) -> ModalResult { +fn factor(i: &mut &[Token]) -> Result { alt(( one_of(|t| matches!(t, Token::Value(_))).map(|t| match t { Token::Value(v) => Expr::Value(v), @@ -178,7 +179,7 @@ fn factor(i: &mut &[Token]) -> ModalResult { .parse_next(i) } -fn parens(i: &mut &[Token]) -> ModalResult { +fn parens(i: &mut &[Token]) -> Result { delimited(one_of(Token::OpenParen), expr, one_of(Token::CloseParen)) .map(|e| Expr::Paren(Box::new(e))) .parse_next(i) diff --git a/examples/css/parser.rs b/examples/css/parser.rs index e2f23b88..94d23cc8 100644 --- a/examples/css/parser.rs +++ b/examples/css/parser.rs @@ -1,6 +1,7 @@ use winnow::combinator::seq; use winnow::prelude::*; use winnow::token::take_while; +use winnow::Result; #[derive(Debug, Eq, PartialEq)] pub(crate) struct Color { @@ -18,7 +19,7 @@ impl std::str::FromStr for Color { } } -pub(crate) fn hex_color(input: &mut &str) -> ModalResult { +pub(crate) fn hex_color(input: &mut &str) -> Result { seq!(Color { _: '#', red: hex_primary, @@ -28,7 +29,7 @@ pub(crate) fn hex_color(input: &mut &str) -> ModalResult { .parse_next(input) } -fn hex_primary(input: &mut &str) -> ModalResult { +fn hex_primary(input: &mut &str) -> Result { take_while(2, |c: char| c.is_ascii_hexdigit()) .try_map(|input| u8::from_str_radix(input, 16)) .parse_next(input) diff --git a/examples/ini/bench.rs b/examples/ini/bench.rs index 677c2754..3169b4e3 100644 --- a/examples/ini/bench.rs +++ b/examples/ini/bench.rs @@ -1,5 +1,6 @@ use winnow::combinator::repeat; use winnow::prelude::*; +use winnow::Result; mod parser; mod parser_str; @@ -31,7 +32,7 @@ port=143 file=payroll.dat \0"; - fn acc<'s>(i: &mut parser::Stream<'s>) -> ModalResult> { + fn acc<'s>(i: &mut parser::Stream<'s>) -> Result> { repeat(0.., parser::key_value).parse_next(i) } diff --git a/examples/ini/parser.rs b/examples/ini/parser.rs index 1dbfc3df..b74e2f9a 100644 --- a/examples/ini/parser.rs +++ b/examples/ini/parser.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::str; use winnow::prelude::*; +use winnow::Result; use winnow::{ ascii::{alphanumeric1 as alphanumeric, multispace0 as multispace, space0 as space}, combinator::opt, @@ -14,7 +15,7 @@ pub(crate) type Stream<'i> = &'i [u8]; pub(crate) fn categories<'s>( i: &mut Stream<'s>, -) -> ModalResult>> { +) -> Result>> { repeat( 0.., separated_pair( @@ -26,13 +27,13 @@ pub(crate) fn categories<'s>( .parse_next(i) } -fn category<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { +fn category<'s>(i: &mut Stream<'s>) -> Result<&'s str> { delimited('[', take_while(0.., |c| c != b']'), ']') .try_map(str::from_utf8) .parse_next(i) } -pub(crate) fn key_value<'s>(i: &mut Stream<'s>) -> ModalResult<(&'s str, &'s str)> { +pub(crate) fn key_value<'s>(i: &mut Stream<'s>) -> Result<(&'s str, &'s str)> { let key = alphanumeric.try_map(str::from_utf8).parse_next(i)?; let _ = (opt(space), '=', opt(space)).parse_next(i)?; let val = take_while(0.., |c| c != b'\n' && c != b';') diff --git a/examples/ini/parser_str.rs b/examples/ini/parser_str.rs index df2a9ed9..383f469b 100644 --- a/examples/ini/parser_str.rs +++ b/examples/ini/parser_str.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use winnow::prelude::*; +use winnow::Result; use winnow::{ ascii::{alphanumeric1 as alphanumeric, space0 as space}, combinator::opt, @@ -13,15 +14,15 @@ pub(crate) type Stream<'i> = &'i str; pub(crate) fn categories<'s>( input: &mut Stream<'s>, -) -> ModalResult>> { +) -> Result>> { repeat(0.., category_and_keys).parse_next(input) } -fn category_and_keys<'s>(i: &mut Stream<'s>) -> ModalResult<(&'s str, HashMap<&'s str, &'s str>)> { +fn category_and_keys<'s>(i: &mut Stream<'s>) -> Result<(&'s str, HashMap<&'s str, &'s str>)> { (category, keys_and_values).parse_next(i) } -fn category<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { +fn category<'s>(i: &mut Stream<'s>) -> Result<&'s str> { terminated( delimited('[', take_while(0.., |c| c != ']'), ']'), opt(take_while(1.., [' ', '\r', '\n'])), @@ -29,11 +30,11 @@ fn category<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { .parse_next(i) } -fn keys_and_values<'s>(input: &mut Stream<'s>) -> ModalResult> { +fn keys_and_values<'s>(input: &mut Stream<'s>) -> Result> { repeat(0.., key_value).parse_next(input) } -fn key_value<'s>(i: &mut Stream<'s>) -> ModalResult<(&'s str, &'s str)> { +fn key_value<'s>(i: &mut Stream<'s>) -> Result<(&'s str, &'s str)> { let key = alphanumeric.parse_next(i)?; let _ = (opt(space), "=", opt(space)).parse_next(i)?; let val = take_till(0.., is_line_ending_or_comment).parse_next(i)?; @@ -48,11 +49,11 @@ fn is_line_ending_or_comment(chr: char) -> bool { chr == ';' || chr == '\n' } -fn till_line_ending<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { +fn till_line_ending<'s>(i: &mut Stream<'s>) -> Result<&'s str> { take_while(0.., |c| c != '\r' && c != '\n').parse_next(i) } -fn space_or_line_ending<'s>(i: &mut Stream<'s>) -> ModalResult<&'s str> { +fn space_or_line_ending<'s>(i: &mut Stream<'s>) -> Result<&'s str> { take_while(1.., [' ', '\r', '\n']).parse_next(i) } diff --git a/examples/string/parser.rs b/examples/string/parser.rs index 4940b83f..ab290b6e 100644 --- a/examples/string/parser.rs +++ b/examples/string/parser.rs @@ -16,10 +16,11 @@ use winnow::combinator::{delimited, preceded}; use winnow::error::{FromExternalError, ParserError}; use winnow::prelude::*; use winnow::token::{take_till, take_while}; +use winnow::Result; /// Parse a string. Use a loop of `parse_fragment` and push all of the fragments /// into an output string. -pub(crate) fn parse_string<'a, E>(input: &mut &'a str) -> ModalResult +pub(crate) fn parse_string<'a, E>(input: &mut &'a str) -> Result where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -64,7 +65,7 @@ enum StringFragment<'a> { /// Combine `parse_literal`, `parse_escaped_whitespace`, and `parse_escaped_char` /// into a `StringFragment`. -fn parse_fragment<'a, E>(input: &mut &'a str) -> ModalResult, E> +fn parse_fragment<'a, E>(input: &mut &'a str) -> Result, E> where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -79,7 +80,7 @@ where } /// Parse a non-empty block of text that doesn't include \ or " -fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> ModalResult<&'a str, E> { +fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> Result<&'a str, E> { // `take_till` parses a string of 0 or more characters that aren't one of the // given characters. let not_quote_slash = take_till(1.., ['"', '\\']); @@ -98,7 +99,7 @@ fn parse_literal<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> ModalResul // then combine them into larger parsers. /// Parse an escaped character: \n, \t, \r, \u{00AC}, etc. -fn parse_escaped_char<'a, E>(input: &mut &'a str) -> ModalResult +fn parse_escaped_char<'a, E>(input: &mut &'a str) -> Result where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -128,7 +129,7 @@ where /// Parse a unicode sequence, of the form u{XXXX}, where XXXX is 1 to 6 /// hexadecimal numerals. We will combine this later with `parse_escaped_char` /// to parse sequences like \u{00AC}. -fn parse_unicode<'a, E>(input: &mut &'a str) -> ModalResult +fn parse_unicode<'a, E>(input: &mut &'a str) -> Result where E: ParserError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, { @@ -162,6 +163,6 @@ where /// to discard any escaped whitespace. fn parse_escaped_whitespace<'a, E: ParserError<&'a str>>( input: &mut &'a str, -) -> ModalResult<&'a str, E> { +) -> Result<&'a str, E> { preceded('\\', multispace1).parse_next(input) }