Skip to content

Commit b37a909

Browse files
committed
Prefer Parser inherent parsers
1 parent ac0e81c commit b37a909

File tree

9 files changed

+155
-169
lines changed

9 files changed

+155
-169
lines changed

gix-actor/src/signature/decode.rs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub(crate) mod function {
77
branch::alt,
88
bytes::complete::{take, take_until, take_while_m_n},
99
character::is_digit,
10-
error::{context, ContextError, ParseError},
10+
error::{ContextError, ParseError},
1111
multi::many1_count,
1212
prelude::*,
1313
sequence::{terminated, tuple},
@@ -22,41 +22,41 @@ pub(crate) mod function {
2222
i: &'a [u8],
2323
) -> IResult<&'a [u8], SignatureRef<'a>, E> {
2424
let tzsign = RefCell::new(b'-'); // TODO: there should be no need for this.
25-
let (i, (identity, _, time, _tzsign_count, hours, minutes)) = context(
26-
"<name> <<email>> <timestamp> <+|-><HHMM>",
27-
(
28-
identity,
29-
b" ",
30-
context("<timestamp>", |i| {
31-
terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| {
32-
btoi::<SecondsSinceUnixEpoch>(v)
33-
.map(|v| (i, v))
34-
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
35-
})
36-
}),
37-
context(
38-
"+|-",
39-
alt((
40-
many1_count(b"-").map(|_| *tzsign.borrow_mut() = b'-'), // TODO: this should be a non-allocating consumer of consecutive tags
41-
many1_count(b"+").map(|_| *tzsign.borrow_mut() = b'+'),
42-
)),
43-
),
44-
context("HH", |i| {
45-
take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| {
46-
btoi::<OffsetInSeconds>(v)
47-
.map(|v| (i, v))
48-
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
49-
})
50-
}),
51-
context("MM", |i| {
52-
take_while_m_n(1usize, 2, is_digit)(i).and_then(|(i, v)| {
53-
btoi::<OffsetInSeconds>(v)
54-
.map(|v| (i, v))
55-
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
56-
})
57-
}),
58-
),
59-
)(i)?;
25+
let (i, (identity, _, time, _tzsign_count, hours, minutes)) = (
26+
identity,
27+
b" ",
28+
(|i| {
29+
terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| {
30+
btoi::<SecondsSinceUnixEpoch>(v)
31+
.map(|v| (i, v))
32+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
33+
})
34+
})
35+
.context("<timestamp>"),
36+
alt((
37+
many1_count(b"-").map(|_| *tzsign.borrow_mut() = b'-'), // TODO: this should be a non-allocating consumer of consecutive tags
38+
many1_count(b"+").map(|_| *tzsign.borrow_mut() = b'+'),
39+
))
40+
.context("+|-"),
41+
(|i| {
42+
take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| {
43+
btoi::<OffsetInSeconds>(v)
44+
.map(|v| (i, v))
45+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
46+
})
47+
})
48+
.context("HH"),
49+
(|i| {
50+
take_while_m_n(1usize, 2, is_digit)(i).and_then(|(i, v)| {
51+
btoi::<OffsetInSeconds>(v)
52+
.map(|v| (i, v))
53+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
54+
})
55+
})
56+
.context("MM"),
57+
)
58+
.context("<name> <<email>> <timestamp> <+|-><HHMM>")
59+
.parse_next(i)?;
6060

6161
let tzsign = tzsign.into_inner();
6262
debug_assert!(tzsign == b'-' || tzsign == b'+', "parser assure it's +|- only");
@@ -81,13 +81,12 @@ pub(crate) mod function {
8181
pub fn identity<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
8282
i: &'a [u8],
8383
) -> IResult<&'a [u8], IdentityRef<'a>, E> {
84-
let (i, (name, email)) = context(
85-
"<name> <<email>>",
86-
(
87-
context("<name>", terminated(take_until(&b" <"[..]), take(2usize))),
88-
context("<email>", terminated(take_until(&b">"[..]), take(1usize))),
89-
),
90-
)(i)?;
84+
let (i, (name, email)) = (
85+
terminated(take_until(&b" <"[..]), take(2usize)).context("<name>"),
86+
terminated(take_until(&b">"[..]), take(1usize)).context("<email>"),
87+
)
88+
.context("<name> <<email>>")
89+
.parse_next(i)?;
9190

9291
Ok((
9392
i,

gix-object/src/commit/decode.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use std::borrow::Cow;
33
use smallvec::SmallVec;
44
use winnow::{
55
branch::alt,
6-
bytes::complete::is_not,
6+
bytes::complete::{is_not, tag},
77
combinator::{all_consuming, opt},
8-
error::{context, ContextError, ParseError},
8+
error::{ContextError, ParseError},
99
multi::many0,
10-
IResult, Parser,
10+
prelude::*,
1111
};
1212

1313
use crate::{parse, parse::NL, BStr, ByteSlice, CommitRef};
@@ -18,39 +18,36 @@ pub fn message<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]
1818
return Err(winnow::Err::from_error_kind(i, winnow::error::ErrorKind::Eof)
1919
.map(|err: E| err.add_context(i, "newline + <message>")));
2020
}
21-
let (i, _) = context("a newline separates headers from the message", NL)(i)?;
21+
let (i, _) = tag(NL)
22+
.context("a newline separates headers from the message")
23+
.parse_next(i)?;
2224
Ok((&[], i.as_bstr()))
2325
}
2426

2527
pub fn commit<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
2628
i: &'a [u8],
2729
) -> IResult<&'a [u8], CommitRef<'_>, E> {
28-
let (i, tree) = context("tree <40 lowercase hex char>", |i| {
29-
parse::header_field(i, b"tree", parse::hex_hash)
30-
})(i)?;
31-
let (i, parents): (_, Vec<_>) = context(
32-
"zero or more 'parent <40 lowercase hex char>'",
33-
many0(|i| parse::header_field(i, b"parent", parse::hex_hash)),
34-
)(i)?;
35-
let (i, author) = context("author <signature>", |i| {
36-
parse::header_field(i, b"author", parse::signature)
37-
})(i)?;
38-
let (i, committer) = context("committer <signature>", |i| {
39-
parse::header_field(i, b"committer", parse::signature)
40-
})(i)?;
41-
let (i, encoding) = context(
42-
"encoding <encoding>",
43-
opt(|i| parse::header_field(i, b"encoding", is_not(NL))),
44-
)(i)?;
45-
let (i, extra_headers) = context(
46-
"<field> <single-line|multi-line>",
47-
many0(alt((
48-
parse::any_header_field_multi_line.map(|(k, o)| (k.as_bstr(), Cow::Owned(o))),
49-
|i| {
50-
parse::any_header_field(i, is_not(NL)).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr()))))
51-
},
52-
))),
53-
)(i)?;
30+
let (i, tree) = (|i| parse::header_field(i, b"tree", parse::hex_hash))
31+
.context("tree <40 lowercase hex char>")
32+
.parse_next(i)?;
33+
let (i, parents): (_, Vec<_>) = many0(|i| parse::header_field(i, b"parent", parse::hex_hash))
34+
.context("zero or more 'parent <40 lowercase hex char>'")
35+
.parse_next(i)?;
36+
let (i, author) = (|i| parse::header_field(i, b"author", parse::signature))
37+
.context("author <signature>")
38+
.parse_next(i)?;
39+
let (i, committer) = (|i| parse::header_field(i, b"committer", parse::signature))
40+
.context("committer <signature>")
41+
.parse_next(i)?;
42+
let (i, encoding) = opt(|i| parse::header_field(i, b"encoding", is_not(NL)))
43+
.context("encoding <encoding>")
44+
.parse_next(i)?;
45+
let (i, extra_headers) = many0(alt((
46+
parse::any_header_field_multi_line.map(|(k, o)| (k.as_bstr(), Cow::Owned(o))),
47+
|i| parse::any_header_field(i, is_not(NL)).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr())))),
48+
)))
49+
.context("<field> <single-line|multi-line>")
50+
.parse_next(i)?;
5451
let (i, message) = all_consuming(message)(i)?;
5552

5653
Ok((

gix-object/src/commit/ref_iter.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winnow::{
77
branch::alt,
88
bytes::complete::is_not,
99
combinator::{all_consuming, opt},
10-
error::context,
10+
prelude::*,
1111
};
1212

1313
use crate::commit::SignedData;
@@ -153,9 +153,9 @@ impl<'a> CommitRefIter<'a> {
153153
use State::*;
154154
Ok(match state {
155155
Tree => {
156-
let (i, tree) = context("tree <40 lowercase hex char>", |i| {
157-
parse::header_field(i, b"tree", parse::hex_hash)
158-
})(i)?;
156+
let (i, tree) = (|i| parse::header_field(i, b"tree", parse::hex_hash))
157+
.context("tree <40 lowercase hex char>")
158+
.parse_next(i)?;
159159
*state = State::Parents;
160160
(
161161
i,
@@ -165,10 +165,9 @@ impl<'a> CommitRefIter<'a> {
165165
)
166166
}
167167
Parents => {
168-
let (i, parent) = context(
169-
"commit <40 lowercase hex char>",
170-
opt(|i| parse::header_field(i, b"parent", parse::hex_hash)),
171-
)(i)?;
168+
let (i, parent) = opt(|i| parse::header_field(i, b"parent", parse::hex_hash))
169+
.context("commit <40 lowercase hex char>")
170+
.parse_next(i)?;
172171
match parent {
173172
Some(parent) => (
174173
i,
@@ -196,7 +195,9 @@ impl<'a> CommitRefIter<'a> {
196195
(&b"committer"[..], "committer <signature>")
197196
}
198197
};
199-
let (i, signature) = context(err_msg, |i| parse::header_field(i, field_name, parse::signature))(i)?;
198+
let (i, signature) = (|i| parse::header_field(i, field_name, parse::signature))
199+
.context(err_msg)
200+
.parse_next(i)?;
200201
(
201202
i,
202203
match who {
@@ -206,27 +207,25 @@ impl<'a> CommitRefIter<'a> {
206207
)
207208
}
208209
Encoding => {
209-
let (i, encoding) = context(
210-
"encoding <encoding>",
211-
opt(|i| parse::header_field(i, b"encoding", is_not(NL))),
212-
)(i)?;
210+
let (i, encoding) = opt(|i| parse::header_field(i, b"encoding", is_not(NL)))
211+
.context("encoding <encoding>")
212+
.parse_next(i)?;
213213
*state = State::ExtraHeaders;
214214
match encoding {
215215
Some(encoding) => (i, Token::Encoding(encoding.as_bstr())),
216216
None => return Self::next_inner(i, state),
217217
}
218218
}
219219
ExtraHeaders => {
220-
let (i, extra_header) = context(
221-
"<field> <single-line|multi-line>",
222-
opt(alt((
223-
|i| parse::any_header_field_multi_line(i).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Owned(o)))),
224-
|i| {
225-
parse::any_header_field(i, is_not(NL))
226-
.map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr()))))
227-
},
228-
))),
229-
)(i)?;
220+
let (i, extra_header) = opt(alt((
221+
|i| parse::any_header_field_multi_line(i).map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Owned(o)))),
222+
|i| {
223+
parse::any_header_field(i, is_not(NL))
224+
.map(|(i, (k, o))| (i, (k.as_bstr(), Cow::Borrowed(o.as_bstr()))))
225+
},
226+
)))
227+
.context("<field> <single-line|multi-line>")
228+
.parse_next(i)?;
230229
match extra_header {
231230
Some(extra_header) => (i, Token::ExtraHeader(extra_header)),
232231
None => {

gix-object/src/parse.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use bstr::{BStr, BString, ByteVec};
22
use winnow::{
33
bytes::complete::{is_not, take_until, take_while_m_n},
4-
combinator::{peek, recognize},
5-
error::{context, ContextError, ParseError},
4+
combinator::peek,
5+
error::{ContextError, ParseError},
66
multi::many1_count,
7+
prelude::*,
78
sequence::{preceded, terminated},
8-
IResult,
99
};
1010

1111
use crate::ByteSlice;
@@ -17,13 +17,12 @@ const SPACE_OR_NL: &[u8] = b" \n";
1717
pub(crate) fn any_header_field_multi_line<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
1818
i: &'a [u8],
1919
) -> IResult<&'a [u8], (&'a [u8], BString), E> {
20-
let (i, (k, o)) = context(
21-
"name <multi-line-value>",
22-
peek((
23-
terminated(is_not(SPACE_OR_NL), SPACE),
24-
recognize((is_not(NL), NL, many1_count(terminated((SPACE, take_until(NL)), NL)))),
25-
)),
26-
)(i)?;
20+
let (i, (k, o)) = peek((
21+
terminated(is_not(SPACE_OR_NL), SPACE),
22+
(is_not(NL), NL, many1_count(terminated((SPACE, take_until(NL)), NL))).recognize(),
23+
))
24+
.context("name <multi-line-value>")
25+
.parse_next(i)?;
2726
assert!(!o.is_empty(), "we have parsed more than one value here");
2827
let end = &o[o.len() - 1] as *const u8 as usize;
2928
let start_input = &i[0] as *const u8 as usize;

gix-object/src/tag/decode.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,32 @@ use winnow::{
22
branch::alt,
33
bytes::complete::{tag, take_until, take_while, take_while1},
44
character::is_alphabetic,
5-
combinator::{all_consuming, opt, recognize},
6-
error::{context, ContextError, ParseError},
5+
combinator::{all_consuming, opt},
6+
error::{ContextError, ParseError},
7+
prelude::*,
78
sequence::preceded,
8-
IResult,
99
};
1010

1111
use crate::{parse, parse::NL, BStr, ByteSlice, TagRef};
1212

1313
pub fn git_tag<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], TagRef<'a>, E> {
14-
let (i, target) = context("object <40 lowercase hex char>", |i| {
15-
parse::header_field(i, b"object", parse::hex_hash)
16-
})(i)?;
14+
let (i, target) = (|i| parse::header_field(i, b"object", parse::hex_hash))
15+
.context("object <40 lowercase hex char>")
16+
.parse_next(i)?;
1717

18-
let (i, kind) = context("type <object kind>", |i| {
19-
parse::header_field(i, b"type", take_while1(is_alphabetic))
20-
})(i)?;
18+
let (i, kind) = (|i| parse::header_field(i, b"type", take_while1(is_alphabetic)))
19+
.context("type <object kind>")
20+
.parse_next(i)?;
2121
let kind =
2222
crate::Kind::from_bytes(kind).map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))?;
2323

24-
let (i, tag_version) = context("tag <version>", |i| {
25-
parse::header_field(i, b"tag", take_while1(|b| b != NL[0]))
26-
})(i)?;
24+
let (i, tag_version) = (|i| parse::header_field(i, b"tag", take_while1(|b| b != NL[0])))
25+
.context("tag <version>")
26+
.parse_next(i)?;
2727

28-
let (i, signature) = context(
29-
"tagger <signature>",
30-
opt(|i| parse::header_field(i, b"tagger", parse::signature)),
31-
)(i)?;
28+
let (i, signature) = opt(|i| parse::header_field(i, b"tagger", parse::signature))
29+
.context("tagger <signature>")
30+
.parse_next(i)?;
3231
let (i, (message, pgp_signature)) = all_consuming(message)(i)?;
3332
Ok((
3433
i,
@@ -65,12 +64,13 @@ pub fn message<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&
6564
take_until(PGP_SIGNATURE_BEGIN),
6665
preceded(
6766
NL,
68-
recognize((
67+
(
6968
&PGP_SIGNATURE_BEGIN[1..],
7069
take_until(PGP_SIGNATURE_END),
7170
PGP_SIGNATURE_END,
7271
take_while(|_| true),
73-
)),
72+
)
73+
.recognize(),
7474
),
7575
),
7676
all_to_end,

0 commit comments

Comments
 (0)