diff --git a/examples/json/bench.rs b/examples/json/bench.rs index d5c1d1d0..c789da26 100644 --- a/examples/json/bench.rs +++ b/examples/json/bench.rs @@ -13,11 +13,6 @@ fn json_bench(c: &mut criterion::Criterion) { let len = sample.len(); group.throughput(criterion::Throughput::Bytes(len as u64)); - group.bench_with_input(criterion::BenchmarkId::new("basic", name), &len, |b, _| { - type Error<'i> = winnow::error::InputError>; - - b.iter(|| parser::json::>.parse_peek(sample).unwrap()); - }); group.bench_with_input(criterion::BenchmarkId::new("unit", name), &len, |b, _| { type Error<'i> = (); @@ -27,32 +22,28 @@ fn json_bench(c: &mut criterion::Criterion) { criterion::BenchmarkId::new("context", name), &len, |b, _| { - type Error<'i> = winnow::error::ContextError>; + type Error = winnow::error::ContextError; - b.iter(|| parser::json::>.parse_peek(sample).unwrap()); + b.iter(|| parser::json::.parse_peek(sample).unwrap()); }, ); group.bench_with_input( criterion::BenchmarkId::new("dispatch", name), &len, |b, _| { - type Error<'i> = winnow::error::InputError>; + type Error = winnow::error::ContextError; - b.iter(|| { - parser_dispatch::json::> - .parse_peek(sample) - .unwrap() - }); + b.iter(|| parser_dispatch::json::.parse_peek(sample).unwrap()); }, ); group.bench_with_input( criterion::BenchmarkId::new("streaming", name), &len, |b, _| { - type Error<'i> = winnow::error::InputError>; + type Error = winnow::error::ContextError; b.iter(|| { - parser_partial::json::> + parser_partial::json:: .parse_peek(Partial::new(sample)) .unwrap() }); diff --git a/examples/json/parser.rs b/examples/json/parser.rs index fdb728a9..a9597f2c 100644 --- a/examples/json/parser.rs +++ b/examples/json/parser.rs @@ -8,7 +8,7 @@ use winnow::{ combinator::cut_err, combinator::{delimited, preceded, separated_pair, terminated}, combinator::{repeat, separated}, - error::{AddContext, ParserError}, + error::{AddContext, ParserError, StrContext}, token::{any, none_of, take, take_while}, }; @@ -19,7 +19,7 @@ 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: +/// `&mut Stream -> PResult`, with `PResult` defined as: /// `type PResult = Result>;` /// /// most of the times you can ignore the error type and use the default (but this @@ -28,7 +28,7 @@ pub(crate) type Stream<'i> = &'i str; /// Here we use `&str` as input type, but parsers can be generic over /// the input type, work directly with `&[u8]`, or any other type that /// implements the required traits. -pub(crate) fn json<'i, E: ParserError> + AddContext, &'static str>>( +pub(crate) fn json<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { delimited(ws, json_value, ws).parse_next(input) @@ -36,7 +36,7 @@ pub(crate) fn json<'i, E: ParserError> + AddContext, &'sta /// `alt` is a combinator that tries multiple parsers one by one, until /// one of them succeeds -fn json_value<'i, E: ParserError> + AddContext, &'static str>>( +fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { // `alt` combines the each value parser. It returns the result of the first @@ -77,7 +77,7 @@ fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn string<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { preceded( @@ -96,7 +96,7 @@ fn string<'i, E: ParserError> + AddContext, &'static str>> ) // `context` lets you add a static string to errors to provide more information in the // error chain (to indicate which parser had an error) - .context("string") + .context(StrContext::Expected("string".into())) .parse_next(input) } @@ -157,7 +157,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn array<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -167,11 +167,11 @@ fn array<'i, E: ParserError> + AddContext, &'static str>>( (ws, ']'), )), ) - .context("array") + .context(StrContext::Expected("array".into())) .parse_next(input) } -fn object<'i, E: ParserError> + AddContext, &'static str>>( +fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -181,11 +181,11 @@ fn object<'i, E: ParserError> + AddContext, &'static str>> (ws, '}'), )), ) - .context("object") + .context(StrContext::Expected("object".into())) .parse_next(input) } -fn key_value<'i, E: ParserError> + AddContext, &'static str>>( +fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) @@ -205,42 +205,37 @@ const WS: &[char] = &[' ', '\t', '\r', '\n']; #[cfg(test)] mod test { #[allow(clippy::useless_attribute)] - #[allow(dead_code)] // its dead for benches + #[allow(unused_imports)] // its dead for benches use super::*; #[allow(clippy::useless_attribute)] #[allow(dead_code)] // its dead for benches - type Error<'i> = winnow::error::InputError<&'i str>; + type Error = winnow::error::ContextError; #[test] fn json_string() { + assert_eq!(string::.parse_peek("\"\""), Ok(("", "".to_owned()))); assert_eq!( - string::>.parse_peek("\"\""), - Ok(("", "".to_owned())) - ); - assert_eq!( - string::>.parse_peek("\"abc\""), + string::.parse_peek("\"abc\""), Ok(("", "abc".to_owned())) ); assert_eq!( - string::> + string:: .parse_peek("\"abc\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0001\\u2014\u{2014}def\""), Ok(("", "abc\"\\/\x08\x0C\n\r\t\x01β€”β€”def".to_owned())), ); assert_eq!( - string::>.parse_peek("\"\\uD83D\\uDE10\""), + string::.parse_peek("\"\\uD83D\\uDE10\""), Ok(("", "😐".to_owned())) ); - assert!(string::>.parse_peek("\"").is_err()); - assert!(string::>.parse_peek("\"abc").is_err()); - assert!(string::>.parse_peek("\"\\\"").is_err()); - assert!(string::>.parse_peek("\"\\u123\"").is_err()); - assert!(string::>.parse_peek("\"\\uD800\"").is_err()); - assert!(string::> - .parse_peek("\"\\uD800\\uD800\"") - .is_err()); - assert!(string::>.parse_peek("\"\\uDC00\"").is_err()); + assert!(string::.parse_peek("\"").is_err()); + assert!(string::.parse_peek("\"abc").is_err()); + assert!(string::.parse_peek("\"\\\"").is_err()); + assert!(string::.parse_peek("\"\\u123\"").is_err()); + assert!(string::.parse_peek("\"\\uD800\"").is_err()); + assert!(string::.parse_peek("\"\\uD800\\uD800\"").is_err()); + assert!(string::.parse_peek("\"\\uDC00\"").is_err()); } #[test] @@ -258,7 +253,7 @@ mod test { .collect(), ); - assert_eq!(json::>.parse_peek(input), Ok(("", expected))); + assert_eq!(json::.parse_peek(input), Ok(("", expected))); } #[test] @@ -269,7 +264,7 @@ mod test { let expected = Array(vec![Num(42.0), Str("x".to_owned())]); - assert_eq!(json::>.parse_peek(input), Ok(("", expected))); + assert_eq!(json::.parse_peek(input), Ok(("", expected))); } #[test] @@ -291,7 +286,7 @@ mod test { "#; assert_eq!( - json::>.parse_peek(input), + json::.parse_peek(input), Ok(( "", Object( diff --git a/examples/json/parser_dispatch.rs b/examples/json/parser_dispatch.rs index f8de8849..06b391b4 100644 --- a/examples/json/parser_dispatch.rs +++ b/examples/json/parser_dispatch.rs @@ -11,7 +11,7 @@ use winnow::{ combinator::{alt, dispatch}, combinator::{delimited, preceded, separated_pair, terminated}, combinator::{repeat, separated}, - error::{AddContext, ParserError}, + error::{AddContext, ParserError, StrContext}, token::{any, none_of, take, take_while}, }; @@ -22,7 +22,7 @@ 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: +/// `&mut Stream -> PResult`, with `PResult` defined as: /// `type PResult = Result>;` /// /// most of the times you can ignore the error type and use the default (but this @@ -31,7 +31,7 @@ pub(crate) type Stream<'i> = &'i str; /// Here we use `&str` as input type, but parsers can be generic over /// the input type, work directly with `&[u8]`, or any other type that /// implements the required traits. -pub(crate) fn json<'i, E: ParserError> + AddContext, &'static str>>( +pub(crate) fn json<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { delimited(ws, json_value, ws).parse_next(input) @@ -39,7 +39,7 @@ pub(crate) fn json<'i, E: ParserError> + AddContext, &'sta /// `alt` is a combinator that tries multiple parsers one by one, until /// one of them succeeds -fn json_value<'i, E: ParserError> + AddContext, &'static str>>( +fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { // `dispatch` gives you `match`-like behavior compared to `alt` successively trying different @@ -86,7 +86,7 @@ fn false_<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn string<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { preceded( @@ -105,7 +105,7 @@ fn string<'i, E: ParserError> + AddContext, &'static str>> ) // `context` lets you add a static string to errors to provide more information in the // error chain (to indicate which parser had an error) - .context("string") + .context(StrContext::Expected("string".into())) .parse_next(input) } @@ -164,7 +164,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn array<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -174,11 +174,11 @@ fn array<'i, E: ParserError> + AddContext, &'static str>>( (ws, ']'), )), ) - .context("array") + .context(StrContext::Expected("array".into())) .parse_next(input) } -fn object<'i, E: ParserError> + AddContext, &'static str>>( +fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -188,11 +188,11 @@ fn object<'i, E: ParserError> + AddContext, &'static str>> (ws, '}'), )), ) - .context("object") + .context(StrContext::Expected("object".into())) .parse_next(input) } -fn key_value<'i, E: ParserError> + AddContext, &'static str>>( +fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) @@ -212,42 +212,37 @@ const WS: &[char] = &[' ', '\t', '\r', '\n']; #[cfg(test)] mod test { #[allow(clippy::useless_attribute)] - #[allow(dead_code)] // its dead for benches + #[allow(unused_imports)] // its dead for benches use super::*; #[allow(clippy::useless_attribute)] #[allow(dead_code)] // its dead for benches - type Error<'i> = winnow::error::InputError<&'i str>; + type Error = winnow::error::ContextError; #[test] fn json_string() { + assert_eq!(string::.parse_peek("\"\""), Ok(("", "".to_owned()))); assert_eq!( - string::>.parse_peek("\"\""), - Ok(("", "".to_owned())) - ); - assert_eq!( - string::>.parse_peek("\"abc\""), + string::.parse_peek("\"abc\""), Ok(("", "abc".to_owned())) ); assert_eq!( - string::> + string:: .parse_peek("\"abc\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0001\\u2014\u{2014}def\""), Ok(("", "abc\"\\/\x08\x0C\n\r\t\x01β€”β€”def".to_owned())), ); assert_eq!( - string::>.parse_peek("\"\\uD83D\\uDE10\""), + string::.parse_peek("\"\\uD83D\\uDE10\""), Ok(("", "😐".to_owned())) ); - assert!(string::>.parse_peek("\"").is_err()); - assert!(string::>.parse_peek("\"abc").is_err()); - assert!(string::>.parse_peek("\"\\\"").is_err()); - assert!(string::>.parse_peek("\"\\u123\"").is_err()); - assert!(string::>.parse_peek("\"\\uD800\"").is_err()); - assert!(string::> - .parse_peek("\"\\uD800\\uD800\"") - .is_err()); - assert!(string::>.parse_peek("\"\\uDC00\"").is_err()); + assert!(string::.parse_peek("\"").is_err()); + assert!(string::.parse_peek("\"abc").is_err()); + assert!(string::.parse_peek("\"\\\"").is_err()); + assert!(string::.parse_peek("\"\\u123\"").is_err()); + assert!(string::.parse_peek("\"\\uD800\"").is_err()); + assert!(string::.parse_peek("\"\\uD800\\uD800\"").is_err()); + assert!(string::.parse_peek("\"\\uDC00\"").is_err()); } #[test] @@ -265,7 +260,7 @@ mod test { .collect(), ); - assert_eq!(json::>.parse_peek(input), Ok(("", expected))); + assert_eq!(json::.parse_peek(input), Ok(("", expected))); } #[test] @@ -276,7 +271,7 @@ mod test { let expected = Array(vec![Num(42.0), Str("x".to_owned())]); - assert_eq!(json::>.parse_peek(input), Ok(("", expected))); + assert_eq!(json::.parse_peek(input), Ok(("", expected))); } #[test] @@ -298,7 +293,7 @@ mod test { "#; assert_eq!( - json::>.parse_peek(input), + json::.parse_peek(input), Ok(( "", Object( diff --git a/examples/json/parser_partial.rs b/examples/json/parser_partial.rs index 89cc9c3a..b1a61f25 100644 --- a/examples/json/parser_partial.rs +++ b/examples/json/parser_partial.rs @@ -8,7 +8,7 @@ use winnow::{ combinator::{cut_err, rest}, combinator::{delimited, preceded, separated_pair, terminated}, combinator::{repeat, separated}, - error::{AddContext, ParserError}, + error::{AddContext, ParserError, StrContext}, stream::Partial, token::{any, none_of, take, take_while}, }; @@ -20,7 +20,7 @@ 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: +/// `&mut Stream -> PResult`, with `PResult` defined as: /// `type PResult = Result>;` /// /// most of the times you can ignore the error type and use the default (but this @@ -29,7 +29,7 @@ pub(crate) type Stream<'i> = Partial<&'i str>; /// Here we use `&str` as input type, but parsers can be generic over /// the input type, work directly with `&[u8]`, or any other type that /// implements the required traits. -pub(crate) fn json<'i, E: ParserError> + AddContext, &'static str>>( +pub(crate) fn json<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { delimited(ws, json_value, ws_or_eof).parse_next(input) @@ -37,7 +37,7 @@ pub(crate) fn json<'i, E: ParserError> + AddContext, &'sta /// `alt` is a combinator that tries multiple parsers one by one, until /// one of them succeeds -fn json_value<'i, E: ParserError> + AddContext, &'static str>>( +fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { // `alt` combines the each value parser. It returns the result of the first @@ -78,7 +78,7 @@ fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn string<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { preceded( @@ -97,7 +97,7 @@ fn string<'i, E: ParserError> + AddContext, &'static str>> ) // `context` lets you add a static string to errors to provide more information in the // error chain (to indicate which parser had an error) - .context("string") + .context(StrContext::Expected("string".into())) .parse_next(input) } @@ -158,7 +158,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn array<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -168,11 +168,11 @@ fn array<'i, E: ParserError> + AddContext, &'static str>>( (ws, ']'), )), ) - .context("array") + .context(StrContext::Expected("array".into())) .parse_next(input) } -fn object<'i, E: ParserError> + AddContext, &'static str>>( +fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -182,11 +182,11 @@ fn object<'i, E: ParserError> + AddContext, &'static str>> (ws, '}'), )), ) - .context("object") + .context(StrContext::Expected("object".into())) .parse_next(input) } -fn key_value<'i, E: ParserError> + AddContext, &'static str>>( +fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) @@ -211,25 +211,25 @@ const WS: &[char] = &[' ', '\t', '\r', '\n']; #[cfg(test)] mod test { #[allow(clippy::useless_attribute)] - #[allow(dead_code)] // its dead for benches + #[allow(unused_imports)] // its dead for benches use super::*; #[allow(clippy::useless_attribute)] #[allow(dead_code)] // its dead for benches - type Error<'i> = winnow::error::InputError>; + type Error = winnow::error::ContextError; #[test] fn json_string() { assert_eq!( - string::>.parse_peek(Partial::new("\"\"")), + string::.parse_peek(Partial::new("\"\"")), Ok((Partial::new(""), "".to_owned())) ); assert_eq!( - string::>.parse_peek(Partial::new("\"abc\"")), + string::.parse_peek(Partial::new("\"abc\"")), Ok((Partial::new(""), "abc".to_owned())) ); assert_eq!( - string::>.parse_peek(Partial::new( + string::.parse_peek(Partial::new( "\"abc\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0001\\u2014\u{2014}def\"" )), Ok(( @@ -238,27 +238,23 @@ mod test { )), ); assert_eq!( - string::>.parse_peek(Partial::new("\"\\uD83D\\uDE10\"")), + string::.parse_peek(Partial::new("\"\\uD83D\\uDE10\"")), Ok((Partial::new(""), "😐".to_owned())) ); - assert!(string::>.parse_peek(Partial::new("\"")).is_err()); - assert!(string::> - .parse_peek(Partial::new("\"abc")) - .is_err()); - assert!(string::> - .parse_peek(Partial::new("\"\\\"")) - .is_err()); - assert!(string::> + assert!(string::.parse_peek(Partial::new("\"")).is_err()); + assert!(string::.parse_peek(Partial::new("\"abc")).is_err()); + assert!(string::.parse_peek(Partial::new("\"\\\"")).is_err()); + assert!(string:: .parse_peek(Partial::new("\"\\u123\"")) .is_err()); - assert!(string::> + assert!(string:: .parse_peek(Partial::new("\"\\uD800\"")) .is_err()); - assert!(string::> + assert!(string:: .parse_peek(Partial::new("\"\\uD800\\uD800\"")) .is_err()); - assert!(string::> + assert!(string:: .parse_peek(Partial::new("\"\\uDC00\"")) .is_err()); } @@ -279,7 +275,7 @@ mod test { ); assert_eq!( - json::>.parse_peek(Partial::new(input)), + json::.parse_peek(Partial::new(input)), Ok((Partial::new(""), expected)) ); } @@ -293,7 +289,7 @@ mod test { let expected = Array(vec![Num(42.0), Str("x".to_owned())]); assert_eq!( - json::>.parse_peek(Partial::new(input)), + json::.parse_peek(Partial::new(input)), Ok((Partial::new(""), expected)) ); } @@ -317,7 +313,7 @@ mod test { "#; assert_eq!( - json::>.parse_peek(Partial::new(input)), + json::.parse_peek(Partial::new(input)), Ok(( Partial::new(""), Object( diff --git a/examples/ndjson/main.rs b/examples/ndjson/main.rs index 23dc16ac..8670de55 100644 --- a/examples/ndjson/main.rs +++ b/examples/ndjson/main.rs @@ -2,8 +2,8 @@ mod parser; use std::io::Read; +use winnow::error::ContextError; use winnow::error::ErrMode; -use winnow::error::InputError; use winnow::error::Needed; use winnow::prelude::*; use winnow::stream::Offset; @@ -39,7 +39,7 @@ fn main() -> Result<(), lexopt::Error> { loop { let input = parser::Stream::new(std::str::from_utf8(buffer.data()).map_err(to_lexopt)?); - match parser::ndjson::>>.parse_peek(input) { + match parser::ndjson::.parse_peek(input) { Ok((remainder, value)) => { println!("{value:?}"); println!(); diff --git a/examples/ndjson/parser.rs b/examples/ndjson/parser.rs index 00358bf2..4d5f495f 100644 --- a/examples/ndjson/parser.rs +++ b/examples/ndjson/parser.rs @@ -9,7 +9,7 @@ use winnow::{ combinator::cut_err, combinator::{delimited, preceded, separated_pair, terminated}, combinator::{repeat, separated}, - error::{AddContext, ParserError}, + error::{AddContext, ParserError, StrContext}, stream::Partial, token::{any, none_of, take, take_while}, }; @@ -27,7 +27,7 @@ pub(crate) enum JsonValue { /// Use `Partial` to cause `ErrMode::Incomplete` while parsing pub(crate) type Stream<'i> = Partial<&'i str>; -pub(crate) fn ndjson<'i, E: ParserError> + AddContext, &'static str>>( +pub(crate) fn ndjson<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { alt(( @@ -41,7 +41,7 @@ pub(crate) fn ndjson<'i, E: ParserError> + AddContext, &'s /// `alt` is a combinator that tries multiple parsers one by one, until /// one of them succeeds -fn json_value<'i, E: ParserError> + AddContext, &'static str>>( +fn json_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { // `alt` combines the each value parser. It returns the result of the first @@ -82,7 +82,7 @@ fn boolean<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn string<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult { preceded( @@ -101,7 +101,7 @@ fn string<'i, E: ParserError> + AddContext, &'static str>> ) // `context` lets you add a static string to errors to provide more information in the // error chain (to indicate which parser had an error) - .context("string") + .context(StrContext::Expected("string".into())) .parse_next(input) } @@ -162,7 +162,7 @@ fn u16_hex<'i, E: ParserError>>(input: &mut Stream<'i>) -> PResult> + AddContext, &'static str>>( +fn array<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -172,11 +172,11 @@ fn array<'i, E: ParserError> + AddContext, &'static str>>( (ws, ']'), )), ) - .context("array") + .context(StrContext::Expected("array".into())) .parse_next(input) } -fn object<'i, E: ParserError> + AddContext, &'static str>>( +fn object<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult, E> { preceded( @@ -186,11 +186,11 @@ fn object<'i, E: ParserError> + AddContext, &'static str>> (ws, '}'), )), ) - .context("object") + .context(StrContext::Expected("object".into())) .parse_next(input) } -fn key_value<'i, E: ParserError> + AddContext, &'static str>>( +fn key_value<'i, E: ParserError> + AddContext, StrContext>>( input: &mut Stream<'i>, ) -> PResult<(String, JsonValue), E> { separated_pair(string, cut_err((ws, ':', ws)), json_value).parse_next(input) @@ -210,25 +210,25 @@ const WS: &[char] = &[' ', '\t']; #[cfg(test)] mod test { #[allow(clippy::useless_attribute)] - #[allow(dead_code)] // its dead for benches + #[allow(unused_imports)] // its dead for benches use super::*; #[allow(clippy::useless_attribute)] #[allow(dead_code)] // its dead for benches - type Error<'i> = winnow::error::InputError>; + type Error = winnow::error::ContextError; #[test] fn json_string() { assert_eq!( - string::>.parse_peek(Partial::new("\"\"")), + string::.parse_peek(Partial::new("\"\"")), Ok((Partial::new(""), "".to_owned())) ); assert_eq!( - string::>.parse_peek(Partial::new("\"abc\"")), + string::.parse_peek(Partial::new("\"abc\"")), Ok((Partial::new(""), "abc".to_owned())) ); assert_eq!( - string::>.parse_peek(Partial::new( + string::.parse_peek(Partial::new( "\"abc\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0001\\u2014\u{2014}def\"" )), Ok(( @@ -237,27 +237,23 @@ mod test { )), ); assert_eq!( - string::>.parse_peek(Partial::new("\"\\uD83D\\uDE10\"")), + string::.parse_peek(Partial::new("\"\\uD83D\\uDE10\"")), Ok((Partial::new(""), "😐".to_owned())) ); - assert!(string::>.parse_peek(Partial::new("\"")).is_err()); - assert!(string::> - .parse_peek(Partial::new("\"abc")) - .is_err()); - assert!(string::> - .parse_peek(Partial::new("\"\\\"")) - .is_err()); - assert!(string::> + assert!(string::.parse_peek(Partial::new("\"")).is_err()); + assert!(string::.parse_peek(Partial::new("\"abc")).is_err()); + assert!(string::.parse_peek(Partial::new("\"\\\"")).is_err()); + assert!(string:: .parse_peek(Partial::new("\"\\u123\"")) .is_err()); - assert!(string::> + assert!(string:: .parse_peek(Partial::new("\"\\uD800\"")) .is_err()); - assert!(string::> + assert!(string:: .parse_peek(Partial::new("\"\\uD800\\uD800\"")) .is_err()); - assert!(string::> + assert!(string:: .parse_peek(Partial::new("\"\\uDC00\"")) .is_err()); } @@ -279,7 +275,7 @@ mod test { ); assert_eq!( - ndjson::>.parse_peek(Partial::new(input)), + ndjson::.parse_peek(Partial::new(input)), Ok((Partial::new(""), Some(expected))) ); } @@ -294,7 +290,7 @@ mod test { let expected = Array(vec![Num(42.0), Str("x".to_owned())]); assert_eq!( - ndjson::>.parse_peek(Partial::new(input)), + ndjson::.parse_peek(Partial::new(input)), Ok((Partial::new(""), Some(expected))) ); } @@ -307,7 +303,7 @@ mod test { "#; assert_eq!( - ndjson::>.parse_peek(Partial::new(input)), + ndjson::.parse_peek(Partial::new(input)), Ok(( Partial::new(""), Some(Object(