Skip to content

Commit 4776fca

Browse files
authored
Merge pull request #827 from epage/winnow07
chore: Upgrade to Winnow 0.7
2 parents e92c3b6 + 754769b commit 4776fca

File tree

13 files changed

+96
-102
lines changed

13 files changed

+96
-102
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/toml_edit/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unbounded = []
4141

4242
[dependencies]
4343
indexmap = { version = "2.3.0", features = ["std"] }
44-
winnow = { version = "0.6.18", optional = true }
44+
winnow = { version = "0.7.0", optional = true }
4545
serde = { version = "1.0.145", optional = true }
4646
kstring = { version = "2.0.0", features = ["max_inline"], optional = true }
4747
toml_datetime = { version = "0.6.8", path = "../toml_datetime" }

crates/toml_edit/src/parser/array.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::parser::prelude::*;
1414
// ;; Array
1515

1616
// array = array-open array-values array-close
17-
pub(crate) fn array<'i>(input: &mut Input<'i>) -> PResult<Array> {
17+
pub(crate) fn array<'i>(input: &mut Input<'i>) -> ModalResult<Array> {
1818
trace("array", move |input: &mut Input<'i>| {
1919
delimited(
2020
ARRAY_OPEN,
@@ -39,7 +39,7 @@ const ARRAY_SEP: u8 = b',';
3939

4040
// array-values = ws-comment-newline val ws-comment-newline array-sep array-values
4141
// array-values =/ ws-comment-newline val ws-comment-newline [ array-sep ]
42-
pub(crate) fn array_values(input: &mut Input<'_>) -> PResult<Array> {
42+
pub(crate) fn array_values(input: &mut Input<'_>) -> ModalResult<Array> {
4343
if peek(opt(ARRAY_CLOSE)).parse_next(input)?.is_some() {
4444
// Optimize for empty arrays, avoiding `value` from being expected to fail
4545
return Ok(Array::new());
@@ -57,7 +57,7 @@ pub(crate) fn array_values(input: &mut Input<'_>) -> PResult<Array> {
5757
Ok(array)
5858
}
5959

60-
pub(crate) fn array_value(input: &mut Input<'_>) -> PResult<Item> {
60+
pub(crate) fn array_value(input: &mut Input<'_>) -> ModalResult<Item> {
6161
let prefix = ws_comment_newline.span().parse_next(input)?;
6262
let value = value.parse_next(input)?;
6363
let suffix = ws_comment_newline.span().parse_next(input)?;

crates/toml_edit/src/parser/datetime.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use winnow::token::take_while;
2222
// local-date = full-date
2323
// local-time = partial-time
2424
// full-time = partial-time time-offset
25-
pub(crate) fn date_time(input: &mut Input<'_>) -> PResult<Datetime> {
25+
pub(crate) fn date_time(input: &mut Input<'_>) -> ModalResult<Datetime> {
2626
trace(
2727
"date-time",
2828
alt((
@@ -53,11 +53,11 @@ pub(crate) fn date_time(input: &mut Input<'_>) -> PResult<Datetime> {
5353
}
5454

5555
// full-date = date-fullyear "-" date-month "-" date-mday
56-
pub(crate) fn full_date(input: &mut Input<'_>) -> PResult<Date> {
56+
pub(crate) fn full_date(input: &mut Input<'_>) -> ModalResult<Date> {
5757
trace("full-date", full_date_).parse_next(input)
5858
}
5959

60-
fn full_date_(input: &mut Input<'_>) -> PResult<Date> {
60+
fn full_date_(input: &mut Input<'_>) -> ModalResult<Date> {
6161
let year = date_fullyear.parse_next(input)?;
6262
let _ = b'-'.parse_next(input)?;
6363
let month = cut_err(date_month).parse_next(input)?;
@@ -74,19 +74,16 @@ fn full_date_(input: &mut Input<'_>) -> PResult<Date> {
7474
};
7575
if max_days_in_month < day {
7676
input.reset(&day_start);
77-
return Err(winnow::error::ErrMode::from_external_error(
78-
input,
79-
winnow::error::ErrorKind::Verify,
80-
CustomError::OutOfRange,
81-
)
82-
.cut());
77+
return Err(
78+
winnow::error::ErrMode::from_external_error(input, CustomError::OutOfRange).cut(),
79+
);
8380
}
8481

8582
Ok(Date { year, month, day })
8683
}
8784

8885
// partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
89-
pub(crate) fn partial_time(input: &mut Input<'_>) -> PResult<Time> {
86+
pub(crate) fn partial_time(input: &mut Input<'_>) -> ModalResult<Time> {
9087
trace(
9188
"partial-time",
9289
(
@@ -106,7 +103,7 @@ pub(crate) fn partial_time(input: &mut Input<'_>) -> PResult<Time> {
106103

107104
// time-offset = "Z" / time-numoffset
108105
// time-numoffset = ( "+" / "-" ) time-hour ":" time-minute
109-
pub(crate) fn time_offset(input: &mut Input<'_>) -> PResult<Offset> {
106+
pub(crate) fn time_offset(input: &mut Input<'_>) -> ModalResult<Offset> {
110107
trace(
111108
"time-offset",
112109
alt((
@@ -132,14 +129,14 @@ pub(crate) fn time_offset(input: &mut Input<'_>) -> PResult<Offset> {
132129
}
133130

134131
// date-fullyear = 4DIGIT
135-
pub(crate) fn date_fullyear(input: &mut Input<'_>) -> PResult<u16> {
132+
pub(crate) fn date_fullyear(input: &mut Input<'_>) -> ModalResult<u16> {
136133
unsigned_digits::<4, 4>
137134
.map(|s: &str| s.parse::<u16>().expect("4DIGIT should match u8"))
138135
.parse_next(input)
139136
}
140137

141138
// date-month = 2DIGIT ; 01-12
142-
pub(crate) fn date_month(input: &mut Input<'_>) -> PResult<u8> {
139+
pub(crate) fn date_month(input: &mut Input<'_>) -> ModalResult<u8> {
143140
unsigned_digits::<2, 2>
144141
.try_map(|s: &str| {
145142
let d = s.parse::<u8>().expect("2DIGIT should match u8");
@@ -153,7 +150,7 @@ pub(crate) fn date_month(input: &mut Input<'_>) -> PResult<u8> {
153150
}
154151

155152
// date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
156-
pub(crate) fn date_mday(input: &mut Input<'_>) -> PResult<u8> {
153+
pub(crate) fn date_mday(input: &mut Input<'_>) -> ModalResult<u8> {
157154
unsigned_digits::<2, 2>
158155
.try_map(|s: &str| {
159156
let d = s.parse::<u8>().expect("2DIGIT should match u8");
@@ -167,14 +164,14 @@ pub(crate) fn date_mday(input: &mut Input<'_>) -> PResult<u8> {
167164
}
168165

169166
// time-delim = "T" / %x20 ; T, t, or space
170-
pub(crate) fn time_delim(input: &mut Input<'_>) -> PResult<u8> {
167+
pub(crate) fn time_delim(input: &mut Input<'_>) -> ModalResult<u8> {
171168
one_of(TIME_DELIM).parse_next(input)
172169
}
173170

174171
const TIME_DELIM: (u8, u8, u8) = (b'T', b't', b' ');
175172

176173
// time-hour = 2DIGIT ; 00-23
177-
pub(crate) fn time_hour(input: &mut Input<'_>) -> PResult<u8> {
174+
pub(crate) fn time_hour(input: &mut Input<'_>) -> ModalResult<u8> {
178175
unsigned_digits::<2, 2>
179176
.try_map(|s: &str| {
180177
let d = s.parse::<u8>().expect("2DIGIT should match u8");
@@ -188,7 +185,7 @@ pub(crate) fn time_hour(input: &mut Input<'_>) -> PResult<u8> {
188185
}
189186

190187
// time-minute = 2DIGIT ; 00-59
191-
pub(crate) fn time_minute(input: &mut Input<'_>) -> PResult<u8> {
188+
pub(crate) fn time_minute(input: &mut Input<'_>) -> ModalResult<u8> {
192189
unsigned_digits::<2, 2>
193190
.try_map(|s: &str| {
194191
let d = s.parse::<u8>().expect("2DIGIT should match u8");
@@ -202,7 +199,7 @@ pub(crate) fn time_minute(input: &mut Input<'_>) -> PResult<u8> {
202199
}
203200

204201
// time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
205-
pub(crate) fn time_second(input: &mut Input<'_>) -> PResult<u8> {
202+
pub(crate) fn time_second(input: &mut Input<'_>) -> ModalResult<u8> {
206203
unsigned_digits::<2, 2>
207204
.try_map(|s: &str| {
208205
let d = s.parse::<u8>().expect("2DIGIT should match u8");
@@ -216,7 +213,7 @@ pub(crate) fn time_second(input: &mut Input<'_>) -> PResult<u8> {
216213
}
217214

218215
// time-secfrac = "." 1*DIGIT
219-
pub(crate) fn time_secfrac(input: &mut Input<'_>) -> PResult<u32> {
216+
pub(crate) fn time_secfrac(input: &mut Input<'_>) -> ModalResult<u32> {
220217
static SCALE: [u32; 10] = [
221218
0,
222219
100_000_000,
@@ -253,7 +250,7 @@ pub(crate) fn time_secfrac(input: &mut Input<'_>) -> PResult<u32> {
253250

254251
pub(crate) fn unsigned_digits<'i, const MIN: usize, const MAX: usize>(
255252
input: &mut Input<'i>,
256-
) -> PResult<&'i str> {
253+
) -> ModalResult<&'i str> {
257254
take_while(MIN..=MAX, DIGIT)
258255
.map(|b: &[u8]| unsafe { from_utf8_unchecked(b, "`is_ascii_digit` filters out on-ASCII") })
259256
.parse_next(input)

crates/toml_edit/src/parser/document.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::RawString;
3030
// ws )
3131
pub(crate) fn document<'s, 'i>(
3232
state_ref: &'s RefCell<ParseState>,
33-
) -> impl Parser<Input<'i>, (), ContextError> + 's {
33+
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
3434
move |i: &mut Input<'i>| {
3535
(
3636
// Remove BOM if present
@@ -54,7 +54,7 @@ pub(crate) fn document<'s, 'i>(
5454

5555
pub(crate) fn parse_comment<'s, 'i>(
5656
state: &'s RefCell<ParseState>,
57-
) -> impl Parser<Input<'i>, (), ContextError> + 's {
57+
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
5858
move |i: &mut Input<'i>| {
5959
(comment, line_ending)
6060
.span()
@@ -67,7 +67,7 @@ pub(crate) fn parse_comment<'s, 'i>(
6767

6868
pub(crate) fn parse_ws<'s, 'i>(
6969
state: &'s RefCell<ParseState>,
70-
) -> impl Parser<Input<'i>, (), ContextError> + 's {
70+
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
7171
move |i: &mut Input<'i>| {
7272
ws.span()
7373
.map(|span| state.borrow_mut().on_ws(span))
@@ -77,7 +77,7 @@ pub(crate) fn parse_ws<'s, 'i>(
7777

7878
pub(crate) fn parse_newline<'s, 'i>(
7979
state: &'s RefCell<ParseState>,
80-
) -> impl Parser<Input<'i>, (), ContextError> + 's {
80+
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
8181
move |i: &mut Input<'i>| {
8282
newline
8383
.span()
@@ -88,7 +88,7 @@ pub(crate) fn parse_newline<'s, 'i>(
8888

8989
pub(crate) fn keyval<'s, 'i>(
9090
state: &'s RefCell<ParseState>,
91-
) -> impl Parser<Input<'i>, (), ContextError> + 's {
91+
) -> impl ModalParser<Input<'i>, (), ContextError> + 's {
9292
move |i: &mut Input<'i>| {
9393
parse_keyval
9494
.try_map(|(p, kv)| state.borrow_mut().on_keyval(p, kv))
@@ -97,7 +97,7 @@ pub(crate) fn keyval<'s, 'i>(
9797
}
9898

9999
// keyval = key keyval-sep val
100-
pub(crate) fn parse_keyval(input: &mut Input<'_>) -> PResult<(Vec<Key>, (Key, Item))> {
100+
pub(crate) fn parse_keyval(input: &mut Input<'_>) -> ModalResult<(Vec<Key>, (Key, Item))> {
101101
trace(
102102
"keyval",
103103
(

crates/toml_edit/src/parser/inline_table.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use indexmap::map::Entry;
1717
// ;; Inline Table
1818

1919
// inline-table = inline-table-open inline-table-keyvals inline-table-close
20-
pub(crate) fn inline_table<'i>(input: &mut Input<'i>) -> PResult<InlineTable> {
20+
pub(crate) fn inline_table<'i>(input: &mut Input<'i>) -> ModalResult<InlineTable> {
2121
trace("inline-table", move |input: &mut Input<'i>| {
2222
delimited(
2323
INLINE_TABLE_OPEN,
@@ -117,15 +117,15 @@ pub(crate) const KEYVAL_SEP: u8 = b'=';
117117

118118
fn inline_table_keyvals(
119119
input: &mut Input<'_>,
120-
) -> PResult<(Vec<(Vec<Key>, (Key, Item))>, RawString)> {
120+
) -> ModalResult<(Vec<(Vec<Key>, (Key, Item))>, RawString)> {
121121
(
122122
separated(0.., keyval, INLINE_TABLE_SEP),
123123
ws.span().map(RawString::with_span),
124124
)
125125
.parse_next(input)
126126
}
127127

128-
fn keyval(input: &mut Input<'_>) -> PResult<(Vec<Key>, (Key, Item))> {
128+
fn keyval(input: &mut Input<'_>) -> ModalResult<(Vec<Key>, (Key, Item))> {
129129
(
130130
key,
131131
cut_err((

crates/toml_edit/src/parser/key.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::RawString;
1717

1818
// key = simple-key / dotted-key
1919
// dotted-key = simple-key 1*( dot-sep simple-key )
20-
pub(crate) fn key(input: &mut Input<'_>) -> PResult<Vec<Key>> {
20+
pub(crate) fn key(input: &mut Input<'_>) -> ModalResult<Vec<Key>> {
2121
let mut key_path = trace(
2222
"dotted-key",
2323
separated(
@@ -68,7 +68,7 @@ pub(crate) fn key(input: &mut Input<'_>) -> PResult<Vec<Key>> {
6868

6969
// simple-key = quoted-key / unquoted-key
7070
// quoted-key = basic-string / literal-string
71-
pub(crate) fn simple_key(input: &mut Input<'_>) -> PResult<(RawString, InternalString)> {
71+
pub(crate) fn simple_key(input: &mut Input<'_>) -> ModalResult<(RawString, InternalString)> {
7272
trace(
7373
"simple-key",
7474
dispatch! {peek(any);
@@ -87,7 +87,7 @@ pub(crate) fn simple_key(input: &mut Input<'_>) -> PResult<(RawString, InternalS
8787
}
8888

8989
// unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
90-
fn unquoted_key<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
90+
fn unquoted_key<'i>(input: &mut Input<'i>) -> ModalResult<&'i str> {
9191
trace(
9292
"unquoted-key",
9393
take_while(1.., UNQUOTED_CHAR)

crates/toml_edit/src/parser/mod.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ pub(crate) mod prelude {
8383
pub(crate) use winnow::error::FromExternalError;
8484
pub(crate) use winnow::error::StrContext;
8585
pub(crate) use winnow::error::StrContextValue;
86-
pub(crate) use winnow::PResult;
87-
pub(crate) use winnow::Parser;
86+
pub(crate) use winnow::ModalParser;
87+
pub(crate) use winnow::ModalResult;
88+
pub(crate) use winnow::Parser as _;
8889

89-
pub(crate) type Input<'b> = winnow::Stateful<winnow::Located<&'b winnow::BStr>, RecursionCheck>;
90+
pub(crate) type Input<'b> =
91+
winnow::Stateful<winnow::LocatingSlice<&'b winnow::BStr>, RecursionCheck>;
9092

9193
pub(crate) fn new_input(s: &str) -> Input<'_> {
9294
winnow::Stateful {
93-
input: winnow::Located::new(winnow::BStr::new(s)),
95+
input: winnow::LocatingSlice::new(winnow::BStr::new(s)),
9496
state: Default::default(),
9597
}
9698
}
@@ -134,17 +136,13 @@ pub(crate) mod prelude {
134136
}
135137

136138
pub(crate) fn check_recursion<'b, O>(
137-
mut parser: impl Parser<Input<'b>, O, ContextError>,
138-
) -> impl Parser<Input<'b>, O, ContextError> {
139+
mut parser: impl ModalParser<Input<'b>, O, ContextError>,
140+
) -> impl ModalParser<Input<'b>, O, ContextError> {
139141
move |input: &mut Input<'b>| {
140-
input.state.enter().map_err(|err| {
141-
winnow::error::ErrMode::from_external_error(
142-
input,
143-
winnow::error::ErrorKind::Eof,
144-
err,
145-
)
146-
.cut()
147-
})?;
142+
input
143+
.state
144+
.enter()
145+
.map_err(|err| winnow::error::ErrMode::from_external_error(input, err).cut())?;
148146
let result = parser.parse_next(input);
149147
input.state.exit();
150148
result

0 commit comments

Comments
 (0)