Skip to content

Commit

Permalink
feat(multi): Switch from Vec to impl Accumulate
Browse files Browse the repository at this point in the history
I went with a custom trait so we could cover `()` and `usize` as well as
support custom capacities.

This comes at the cost of supporting any container possible.  We could
possibly do that with a `Extendable(T: Default + Extend)` newtype.

As an alternative to `()` and `usize` is GATs.  The main difference is
who drives the types.

Fixes winnow-rs#122
  • Loading branch information
epage committed Feb 3, 2023
1 parent 64d46e3 commit 4fd5961
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 93 deletions.
3 changes: 1 addition & 2 deletions examples/ini/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ pub fn categories(i: Input<'_>) -> IResult<Input<'_>, HashMap<&str, HashMap<&str
many0(separated_pair(
category,
opt(multispace),
many0(terminated(key_value, opt(multispace))).map(|vec: Vec<_>| vec.into_iter().collect()),
many0(terminated(key_value, opt(multispace))),
))
.map(|vec: Vec<_>| vec.into_iter().collect())
.parse_next(i)
}

Expand Down
19 changes: 2 additions & 17 deletions examples/ini/parser_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ use winnow::{
pub type Input<'i> = &'i str;

pub fn categories(input: Input<'_>) -> IResult<Input<'_>, HashMap<&str, HashMap<&str, &str>>> {
match categories_aggregator(input) {
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
Err(e) => Err(e),
}
}

#[allow(clippy::type_complexity)]
fn categories_aggregator(i: Input<'_>) -> IResult<Input<'_>, Vec<(&str, HashMap<&str, &str>)>> {
many0(category_and_keys)(i)
many0(category_and_keys)(input)
}

fn category_and_keys(i: Input<'_>) -> IResult<Input<'_>, (&str, HashMap<&str, &str>)> {
Expand All @@ -35,14 +27,7 @@ fn category(i: Input<'_>) -> IResult<Input<'_>, &str> {
}

fn keys_and_values(input: Input<'_>) -> IResult<Input<'_>, HashMap<&str, &str>> {
match keys_and_values_aggregator(input) {
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
Err(e) => Err(e),
}
}

fn keys_and_values_aggregator(i: Input<'_>) -> IResult<Input<'_>, Vec<(&str, &str)>> {
many0(key_value)(i)
many0(key_value)(input)
}

fn key_value(i: Input<'_>) -> IResult<Input<'_>, (&str, &str)> {
Expand Down
3 changes: 1 addition & 2 deletions examples/json/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ fn object<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str>>(
preceded(
('{', ws),
cut_err(terminated(
separated_list0((ws, ',', ws), key_value)
.map(|tuple_vec| tuple_vec.into_iter().map(|(k, v)| (k, v)).collect()),
separated_list0((ws, ',', ws), key_value),
(ws, '}'),
)),
)
Expand Down
3 changes: 1 addition & 2 deletions examples/json/parser_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ fn object<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str>>(
preceded(
('{', ws),
cut_err(terminated(
separated_list0((ws, ',', ws), key_value)
.map(|tuple_vec| tuple_vec.into_iter().map(|(k, v)| (k, v)).collect()),
separated_list0((ws, ',', ws), key_value),
(ws, '}'),
)),
)
Expand Down
3 changes: 1 addition & 2 deletions examples/json/parser_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ fn object<'i, E: ParseError<Input<'i>> + ContextError<Input<'i>, &'static str>>(
preceded(
(one_of('{'), ws),
cut_err(terminated(
separated_list0((ws, one_of(','), ws), key_value)
.map(|tuple_vec| tuple_vec.into_iter().map(|(k, v)| (k, v)).collect()),
separated_list0((ws, one_of(','), ws), key_value),
(ws, one_of('}')),
)),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/json_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn array(i: &str) -> IResult<&str, ()> {
preceded(
'[',
cut_err(terminated(
separated_list0(preceded(sp, ','), value).map(|_| ()),
separated_list0(preceded(sp, ','), value),
preceded(sp, ']'),
)),
)
Expand All @@ -243,7 +243,7 @@ fn hash(i: &str) -> IResult<&str, ()> {
preceded(
'{',
cut_err(terminated(
separated_list0(preceded(sp, ','), key_value).map(|_| ()),
separated_list0(preceded(sp, ','), key_value),
preceded(sp, '}'),
)),
)
Expand Down
24 changes: 12 additions & 12 deletions src/_cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
//! preceded(
//! alt(("0x", "0X")),
//! many1(
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_'))
//! ).recognize()
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! )(input)
//! }
//! ```
Expand All @@ -191,8 +191,8 @@
//! preceded(
//! alt(("0x", "0X")),
//! many1(
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_'))
//! ).recognize()
//! terminated(one_of("0123456789abcdefABCDEF"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! ).map_res(
//! |out: &str| i64::from_str_radix(&str::replace(&out, "_", ""), 16)
//! ).parse_next(input)
Expand All @@ -215,8 +215,8 @@
//! preceded(
//! alt(("0o", "0O")),
//! many1(
//! terminated(one_of("01234567"), many0('_'))
//! ).recognize()
//! terminated(one_of("01234567"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! )(input)
//! }
//! ```
Expand All @@ -237,8 +237,8 @@
//! preceded(
//! alt(("0b", "0B")),
//! many1(
//! terminated(one_of("01"), many0('_'))
//! ).recognize()
//! terminated(one_of("01"), many0('_').map(|()| ()))
//! ).map(|()| ()).recognize()
//! )(input)
//! }
//! ```
Expand All @@ -256,8 +256,8 @@
//!
//! fn decimal(input: &str) -> IResult<&str, &str> {
//! many1(
//! terminated(one_of("0123456789"), many0('_'))
//! )
//! terminated(one_of("0123456789"), many0('_').map(|()| ()))
//! ).map(|()| ())
//! .recognize()
//! .parse_next(input)
//! }
Expand Down Expand Up @@ -311,8 +311,8 @@
//!
//! fn decimal(input: &str) -> IResult<&str, &str> {
//! many1(
//! terminated(one_of("0123456789"), many0('_'))
//! )
//! terminated(one_of("0123456789"), many0('_').map(|()| ()))
//! ).map(|()| ())
//! .recognize()
//! .parse_next(input)
//! }
Expand Down
Loading

0 comments on commit 4fd5961

Please sign in to comment.