Skip to content

Commit b3f0418

Browse files
committed
Parse explicitly in prep for 0.4
1 parent fee441d commit b3f0418

File tree

13 files changed

+66
-51
lines changed

13 files changed

+66
-51
lines changed

gix-actor/src/signature/decode.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ pub(crate) mod function {
2626
identity,
2727
b" ",
2828
(|i| {
29-
terminated(take_until0(SPACE), take(1usize))(i).and_then(|(i, v)| {
30-
btoi::<SecondsSinceUnixEpoch>(v)
31-
.map(|v| (i, v))
32-
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
33-
})
29+
terminated(take_until0(SPACE), take(1usize))
30+
.parse_next(i)
31+
.and_then(|(i, v)| {
32+
btoi::<SecondsSinceUnixEpoch>(v)
33+
.map(|v| (i, v))
34+
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
35+
})
3436
})
3537
.context("<timestamp>"),
3638
alt((
@@ -39,19 +41,23 @@ pub(crate) mod function {
3941
))
4042
.context("+|-"),
4143
(|i| {
42-
take_while_m_n(2usize, 2, AsChar::is_dec_digit)(i).and_then(|(i, v)| {
43-
btoi::<OffsetInSeconds>(v)
44-
.map(|v| (i, v))
45-
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
46-
})
44+
take_while_m_n(2usize, 2, AsChar::is_dec_digit)
45+
.parse_next(i)
46+
.and_then(|(i, v)| {
47+
btoi::<OffsetInSeconds>(v)
48+
.map(|v| (i, v))
49+
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
50+
})
4751
})
4852
.context("HH"),
4953
(|i| {
50-
take_while_m_n(1usize, 2, AsChar::is_dec_digit)(i).and_then(|(i, v)| {
51-
btoi::<OffsetInSeconds>(v)
52-
.map(|v| (i, v))
53-
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
54-
})
54+
take_while_m_n(1usize, 2, AsChar::is_dec_digit)
55+
.parse_next(i)
56+
.and_then(|(i, v)| {
57+
btoi::<OffsetInSeconds>(v)
58+
.map(|v| (i, v))
59+
.map_err(|_| winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::MapRes))
60+
})
5561
})
5662
.context("MM"),
5763
)

gix-object/src/commit/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn commit<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
5353
)))
5454
.context("<field> <single-line|multi-line>")
5555
.parse_next(i)?;
56-
let (i, message) = terminated(message, eof)(i)?;
56+
let (i, message) = terminated(message, eof).parse_next(i)?;
5757

5858
Ok((
5959
i,

gix-object/src/commit/message/body.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use winnow::{
44
bytes::take_until1,
55
combinator::eof,
66
error::{ErrorKind, ParseError},
7+
prelude::*,
78
sequence::terminated,
8-
IResult,
99
};
1010

1111
use crate::{
@@ -33,7 +33,7 @@ pub struct TrailerRef<'a> {
3333
}
3434

3535
fn parse_single_line_trailer<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
36-
let (value, token) = terminated(take_until1(b":".as_ref()), b": ")(i.trim_end())?;
36+
let (value, token) = terminated(take_until1(b":".as_ref()), b": ").parse_next(i.trim_end())?;
3737
if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
3838
Err(winnow::error::ErrMode::from_error_kind(i, ErrorKind::Fail).cut())
3939
} else {
@@ -50,13 +50,13 @@ impl<'a> Iterator for Trailers<'a> {
5050
}
5151
for line in self.cursor.lines_with_terminator() {
5252
self.cursor = &self.cursor[line.len()..];
53-
if let Some(trailer) =
54-
terminated(parse_single_line_trailer::<()>, eof)(line)
55-
.ok()
56-
.map(|(_, (token, value))| TrailerRef {
57-
token: token.trim().as_bstr(),
58-
value: value.trim().as_bstr(),
59-
})
53+
if let Some(trailer) = terminated(parse_single_line_trailer::<()>, eof)
54+
.parse_next(line)
55+
.ok()
56+
.map(|(_, (token, value))| TrailerRef {
57+
token: token.trim().as_bstr(),
58+
value: value.trim().as_bstr(),
59+
})
6060
{
6161
return Some(trailer);
6262
}

gix-object/src/commit/message/decode.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use winnow::{branch::alt, bytes::take_till1, combinator::eof, error::ParseError,
33
use crate::bstr::{BStr, ByteSlice};
44

55
pub(crate) fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
6-
alt((b"\r\n", b"\n"))(i)
6+
alt((b"\r\n", b"\n")).parse_next(i)
77
}
88

99
fn subject_and_body<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, Option<&'a BStr>), E> {
1010
let mut c = i;
1111
let mut consumed_bytes = 0;
1212
while !c.is_empty() {
13-
c = match take_till1::<_, _, E>(|c| c == b'\n' || c == b'\r')(c) {
13+
c = match take_till1::<_, _, E>(|c| c == b'\n' || c == b'\r').parse_next(c) {
1414
Ok((i1, segment)) => {
1515
consumed_bytes += segment.len();
1616
match (newline::<E>, newline::<E>).parse_next(i1) {
@@ -46,5 +46,8 @@ fn subject_and_body<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8
4646

4747
/// Returns title and body, without separator
4848
pub fn message(input: &[u8]) -> (&BStr, Option<&BStr>) {
49-
terminated(subject_and_body::<()>, eof)(input).expect("cannot fail").1
49+
terminated(subject_and_body::<()>, eof)
50+
.parse_next(input)
51+
.expect("cannot fail")
52+
.1
5053
}

gix-object/src/commit/ref_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'a> CommitRefIter<'a> {
236236
}
237237
}
238238
Message => {
239-
let (i, message) = terminated(decode::message, eof)(i)?;
239+
let (i, message) = terminated(decode::message, eof).parse_next(i)?;
240240
debug_assert!(
241241
i.is_empty(),
242242
"we should have consumed all data - otherwise iter may go forever"

gix-object/src/parse.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use winnow::{
66
multi::many1,
77
prelude::*,
88
sequence::{preceded, terminated},
9+
Parser,
910
};
1011

1112
use crate::ByteSlice;
@@ -46,16 +47,16 @@ pub(crate) fn any_header_field_multi_line<'a, E: ParseError<&'a [u8]> + ContextE
4647
pub(crate) fn header_field<'a, T, E: ParseError<&'a [u8]>>(
4748
i: &'a [u8],
4849
name: &'static [u8],
49-
parse_value: impl FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
50+
parse_value: impl Parser<&'a [u8], T, E>,
5051
) -> IResult<&'a [u8], T, E> {
51-
terminated(preceded(terminated(name, SPACE), parse_value), NL)(i)
52+
terminated(preceded(terminated(name, SPACE), parse_value), NL).parse_next(i)
5253
}
5354

5455
pub(crate) fn any_header_field<'a, T, E: ParseError<&'a [u8]>>(
5556
i: &'a [u8],
56-
parse_value: impl FnMut(&'a [u8]) -> IResult<&'a [u8], T, E>,
57+
parse_value: impl Parser<&'a [u8], T, E>,
5758
) -> IResult<&'a [u8], (&'a [u8], T), E> {
58-
terminated((terminated(take_till1(SPACE_OR_NL), SPACE), parse_value), NL)(i)
59+
terminated((terminated(take_till1(SPACE_OR_NL), SPACE), parse_value), NL).parse_next(i)
5960
}
6061

6162
fn is_hex_digit_lc(b: u8) -> bool {
@@ -67,7 +68,8 @@ pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &
6768
gix_hash::Kind::shortest().len_in_hex(),
6869
gix_hash::Kind::longest().len_in_hex(),
6970
is_hex_digit_lc,
70-
)(i)
71+
)
72+
.parse_next(i)
7173
.map(|(i, hex)| (i, hex.as_bstr()))
7274
}
7375

gix-object/src/tag/decode.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn git_tag<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]
2828
let (i, signature) = opt(|i| parse::header_field(i, b"tagger", parse::signature))
2929
.context("tagger <signature>")
3030
.parse_next(i)?;
31-
let (i, (message, pgp_signature)) = terminated(message, eof)(i)?;
31+
let (i, (message, pgp_signature)) = terminated(message, eof).parse_next(i)?;
3232
Ok((
3333
i,
3434
TagRef {
@@ -49,7 +49,7 @@ pub fn message<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&
4949
if i.is_empty() {
5050
return Ok((i, (i.as_bstr(), None)));
5151
}
52-
let (i, _) = tag(NL)(i)?;
52+
let (i, _) = tag(NL).parse_next(i)?;
5353
fn all_to_end<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a [u8], &'a [u8]), E> {
5454
if i.is_empty() {
5555
// Empty message. That's OK.
@@ -74,8 +74,9 @@ pub fn message<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&
7474
),
7575
),
7676
all_to_end,
77-
))(i)?;
78-
let (i, _) = opt(NL)(i)?;
77+
))
78+
.parse_next(i)?;
79+
let (i, _) = opt(NL).parse_next(i)?;
7980
Ok((
8081
i,
8182
(

gix-object/src/tag/ref_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'a> TagRefIter<'a> {
9898
(i, Token::Tagger(signature))
9999
}
100100
Message => {
101-
let (i, (message, pgp_signature)) = terminated(decode::message, eof)(i)?;
101+
let (i, (message, pgp_signature)) = terminated(decode::message, eof).parse_next(i)?;
102102
debug_assert!(
103103
i.is_empty(),
104104
"we should have consumed all data - otherwise iter may go forever"

gix-object/src/tree/ref_iter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ mod decode {
122122
combinator::eof,
123123
error::ParseError,
124124
multi::many0,
125+
prelude::*,
125126
sequence::terminated,
126127
stream::AsChar,
127-
IResult,
128128
};
129129

130130
use crate::{parse::SPACE, tree, tree::EntryRef, TreeRef};
@@ -161,11 +161,11 @@ mod decode {
161161
}
162162

163163
pub fn entry<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], EntryRef<'_>, E> {
164-
let (i, mode) = terminated(take_while_m_n(5, 6, AsChar::is_dec_digit), SPACE)(i)?;
164+
let (i, mode) = terminated(take_while_m_n(5, 6, AsChar::is_dec_digit), SPACE).parse_next(i)?;
165165
let mode = tree::EntryMode::try_from(mode)
166166
.map_err(|invalid| winnow::error::ErrMode::from_error_kind(invalid, winnow::error::ErrorKind::MapRes))?;
167-
let (i, filename) = terminated(take_while1(|b| b != NULL[0]), NULL)(i)?;
168-
let (i, oid) = take(20u8)(i)?; // TODO: make this compatible with other hash lengths
167+
let (i, filename) = terminated(take_while1(|b| b != NULL[0]), NULL).parse_next(i)?;
168+
let (i, oid) = take(20u8).parse_next(i)?; // TODO: make this compatible with other hash lengths
169169

170170
Ok((
171171
i,
@@ -178,7 +178,7 @@ mod decode {
178178
}
179179

180180
pub fn tree<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], TreeRef<'a>, E> {
181-
let (i, entries) = terminated(many0(entry), eof)(i)?;
181+
let (i, entries) = terminated(many0(entry), eof).parse_next(i)?;
182182
Ok((i, TreeRef { entries }))
183183
}
184184
}

gix-ref/src/parse.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use gix_object::bstr::{BStr, ByteSlice};
2-
use winnow::{branch::alt, bytes::take_while_m_n, error::ParseError, IResult};
2+
use winnow::{branch::alt, bytes::take_while_m_n, error::ParseError, prelude::*};
33

44
fn is_hex_digit_lc(b: u8) -> bool {
55
matches!(b, b'0'..=b'9' | b'a'..=b'f')
@@ -13,10 +13,11 @@ pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &
1313
gix_hash::Kind::shortest().len_in_hex(),
1414
gix_hash::Kind::longest().len_in_hex(),
1515
is_hex_digit_lc,
16-
)(i)
16+
)
17+
.parse_next(i)
1718
.map(|(i, hex)| (i, hex.as_bstr()))
1819
}
1920

2021
pub fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
21-
alt((b"\r\n", b"\n"))(i)
22+
alt((b"\r\n", b"\n")).parse_next(i)
2223
}

0 commit comments

Comments
 (0)