Skip to content

Commit df226dd

Browse files
committed
Switch errors to StrContext
This is prep for switching from `VerboseError` to `ContextError`
1 parent 266864f commit df226dd

File tree

12 files changed

+95
-63
lines changed

12 files changed

+95
-63
lines changed

gix-actor/src/identity.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bstr::ByteSlice;
2+
use winnow::error::StrContext;
23
use winnow::prelude::*;
34

45
use crate::{signature::decode, Identity, IdentityRef};
@@ -7,7 +8,7 @@ impl<'a> IdentityRef<'a> {
78
/// Deserialize an identity from the given `data`.
89
pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<Self, winnow::error::ErrMode<E>>
910
where
10-
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8]>,
11+
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8], StrContext>,
1112
{
1213
decode::identity.parse_next(&mut data)
1314
}

gix-actor/src/signature/decode.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub(crate) mod function {
77
combinator::repeat,
88
combinator::separated_pair,
99
combinator::terminated,
10-
error::{AddContext, ParserError},
10+
error::{AddContext, ParserError, StrContext},
1111
prelude::*,
1212
stream::AsChar,
1313
token::{take, take_until0, take_while},
@@ -18,7 +18,7 @@ pub(crate) mod function {
1818
const SPACE: &[u8] = b" ";
1919

2020
/// Parse a signature from the bytes input `i` using `nom`.
21-
pub fn decode<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(
21+
pub fn decode<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
2222
i: &mut &'a [u8],
2323
) -> PResult<SignatureRef<'a>, E> {
2424
separated_pair(
@@ -27,18 +27,18 @@ pub(crate) mod function {
2727
(
2828
terminated(take_until0(SPACE), take(1usize))
2929
.verify_map(|v| btoi::<SecondsSinceUnixEpoch>(v).ok())
30-
.context("<timestamp>"),
30+
.context(StrContext::Expected("<timestamp>".into())),
3131
alt((
3232
repeat(1.., b"-").map(|_: ()| Sign::Minus),
3333
repeat(1.., b"+").map(|_: ()| Sign::Plus),
3434
))
35-
.context("+|-"),
35+
.context(StrContext::Expected("+|-".into())),
3636
take_while(2, AsChar::is_dec_digit)
3737
.verify_map(|v| btoi::<OffsetInSeconds>(v).ok())
38-
.context("HH"),
38+
.context(StrContext::Expected("HH".into())),
3939
take_while(1..=2, AsChar::is_dec_digit)
4040
.verify_map(|v| btoi::<OffsetInSeconds>(v).ok())
41-
.context("MM"),
41+
.context(StrContext::Expected("MM".into())),
4242
)
4343
.map(|(time, sign, hours, minutes)| {
4444
let offset = (hours * 3600 + minutes * 60) * if sign == Sign::Minus { -1 } else { 1 };
@@ -49,7 +49,7 @@ pub(crate) mod function {
4949
}
5050
}),
5151
)
52-
.context("<name> <<email>> <timestamp> <+|-><HHMM>")
52+
.context(StrContext::Expected("<name> <<email>> <timestamp> <+|-><HHMM>".into()))
5353
.map(|(identity, time)| SignatureRef {
5454
name: identity.name,
5555
email: identity.email,
@@ -59,18 +59,18 @@ pub(crate) mod function {
5959
}
6060

6161
/// Parse an identity from the bytes input `i` (like `name <email>`) using `nom`.
62-
pub fn identity<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(
62+
pub fn identity<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
6363
i: &mut &'a [u8],
6464
) -> PResult<IdentityRef<'a>, E> {
6565
(
66-
terminated(take_until0(&b" <"[..]), take(2usize)).context("<name>"),
67-
terminated(take_until0(&b">"[..]), take(1usize)).context("<email>"),
66+
terminated(take_until0(&b" <"[..]), take(2usize)).context(StrContext::Expected("<name>".into())),
67+
terminated(take_until0(&b">"[..]), take(1usize)).context(StrContext::Expected("<email>".into())),
6868
)
6969
.map(|(name, email): (&[u8], &[u8])| IdentityRef {
7070
name: name.as_bstr(),
7171
email: email.as_bstr(),
7272
})
73-
.context("<name> <<email>>")
73+
.context(StrContext::Expected("<name> <<email>>".into()))
7474
.parse_next(i)
7575
}
7676
}
@@ -86,7 +86,9 @@ mod tests {
8686

8787
use crate::{signature, SignatureRef, Time};
8888

89-
fn decode<'i>(i: &mut &'i [u8]) -> PResult<SignatureRef<'i>, winnow::error::TreeError<&'i [u8], &'static str>> {
89+
fn decode<'i>(
90+
i: &mut &'i [u8],
91+
) -> PResult<SignatureRef<'i>, winnow::error::TreeError<&'i [u8], winnow::error::StrContext>> {
9092
signature::decode.parse_next(i)
9193
}
9294

@@ -163,7 +165,7 @@ mod tests {
163165
.map_err(to_bstr_err)
164166
.expect_err("parse fails as > is missing")
165167
.to_string(),
166-
"in slice at ' 12345 -1215'\n 0: <email> at ' 12345 -1215'\n 1: <name> <<email>> at ' 12345 -1215'\n 2: <name> <<email>> <timestamp> <+|-><HHMM> at ' 12345 -1215'\n"
168+
"in slice at ' 12345 -1215'\n 0: expected `<email>` at ' 12345 -1215'\n 1: expected `<name> <<email>>` at ' 12345 -1215'\n 2: expected `<name> <<email>> <timestamp> <+|-><HHMM>` at ' 12345 -1215'\n"
167169
);
168170
}
169171

@@ -174,7 +176,7 @@ mod tests {
174176
.map_err(to_bstr_err)
175177
.expect_err("parse fails as > is missing")
176178
.to_string(),
177-
"in predicate verification at 'abc -1215'\n 0: <timestamp> at 'abc -1215'\n 1: <name> <<email>> <timestamp> <+|-><HHMM> at 'abc -1215'\n"
179+
"in predicate verification at 'abc -1215'\n 0: expected `<timestamp>` at 'abc -1215'\n 1: expected `<name> <<email>> <timestamp> <+|-><HHMM>` at 'abc -1215'\n"
178180
);
179181
}
180182
}

gix-actor/src/signature/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod _ref {
22
use bstr::ByteSlice;
3+
use winnow::error::StrContext;
34
use winnow::prelude::*;
45

56
use crate::{signature::decode, IdentityRef, Signature, SignatureRef};
@@ -8,7 +9,7 @@ mod _ref {
89
/// Deserialize a signature from the given `data`.
910
pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::error::ErrMode<E>>
1011
where
11-
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8]>,
12+
E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8], StrContext>,
1213
{
1314
decode.parse_next(&mut data)
1415
}

gix-object/src/commit/decode.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,47 @@ use winnow::{
77
combinator::repeat,
88
combinator::terminated,
99
combinator::{eof, opt, rest},
10-
error::{AddContext, ParserError},
10+
error::{AddContext, ParserError, StrContext},
1111
prelude::*,
1212
token::take_till1,
1313
};
1414

1515
use crate::{parse, parse::NL, BStr, ByteSlice, CommitRef};
1616

17-
pub fn message<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<&'a BStr, E> {
17+
pub fn message<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
18+
i: &mut &'a [u8],
19+
) -> PResult<&'a BStr, E> {
1820
if i.is_empty() {
1921
// newline + [message]
2022
return Err(
2123
winnow::error::ErrMode::from_error_kind(i, winnow::error::ErrorKind::Eof)
22-
.add_context(i, "newline + <message>"),
24+
.add_context(i, StrContext::Expected("newline + <message>".into())),
2325
);
2426
}
2527
preceded(NL, rest.map(ByteSlice::as_bstr))
26-
.context("a newline separates headers from the message")
28+
.context(StrContext::Expected(
29+
"a newline separates headers from the message".into(),
30+
))
2731
.parse_next(i)
2832
}
2933

30-
pub fn commit<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<CommitRef<'a>, E> {
34+
pub fn commit<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
35+
i: &mut &'a [u8],
36+
) -> PResult<CommitRef<'a>, E> {
3137
(
32-
(|i: &mut _| parse::header_field(i, b"tree", parse::hex_hash)).context("tree <40 lowercase hex char>"),
38+
(|i: &mut _| parse::header_field(i, b"tree", parse::hex_hash))
39+
.context(StrContext::Expected("tree <40 lowercase hex char>".into())),
3340
repeat(0.., |i: &mut _| parse::header_field(i, b"parent", parse::hex_hash))
3441
.map(|p: Vec<_>| p)
35-
.context("zero or more 'parent <40 lowercase hex char>'"),
36-
(|i: &mut _| parse::header_field(i, b"author", parse::signature)).context("author <signature>"),
37-
(|i: &mut _| parse::header_field(i, b"committer", parse::signature)).context("committer <signature>"),
38-
opt(|i: &mut _| parse::header_field(i, b"encoding", take_till1(NL))).context("encoding <encoding>"),
42+
.context(StrContext::Expected(
43+
"zero or more 'parent <40 lowercase hex char>'".into(),
44+
)),
45+
(|i: &mut _| parse::header_field(i, b"author", parse::signature))
46+
.context(StrContext::Expected("author <signature>".into())),
47+
(|i: &mut _| parse::header_field(i, b"committer", parse::signature))
48+
.context(StrContext::Expected("committer <signature>".into())),
49+
opt(|i: &mut _| parse::header_field(i, b"encoding", take_till1(NL)))
50+
.context(StrContext::Expected("encoding <encoding>".into())),
3951
repeat(
4052
0..,
4153
alt((
@@ -45,7 +57,7 @@ pub fn commit<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &mut &'a [
4557
},
4658
)),
4759
)
48-
.context("<field> <single-line|multi-line>"),
60+
.context(StrContext::Expected("<field> <single-line|multi-line>".into())),
4961
terminated(message, eof),
5062
)
5163
.map(

gix-object/src/commit/ref_iter.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use winnow::{
77
combinator::alt,
88
combinator::terminated,
99
combinator::{eof, opt},
10+
error::StrContext,
1011
prelude::*,
1112
token::take_till1,
1213
};
@@ -155,7 +156,7 @@ impl<'a> CommitRefIter<'a> {
155156
Ok(match state {
156157
Tree => {
157158
let tree = (|i: &mut _| parse::header_field(i, b"tree", parse::hex_hash))
158-
.context("tree <40 lowercase hex char>")
159+
.context(StrContext::Expected("tree <40 lowercase hex char>".into()))
159160
.parse_next(&mut i)?;
160161
*state = State::Parents;
161162
(
@@ -167,7 +168,7 @@ impl<'a> CommitRefIter<'a> {
167168
}
168169
Parents => {
169170
let parent = opt(|i: &mut _| parse::header_field(i, b"parent", parse::hex_hash))
170-
.context("commit <40 lowercase hex char>")
171+
.context(StrContext::Expected("commit <40 lowercase hex char>".into()))
171172
.parse_next(&mut i)?;
172173
match parent {
173174
Some(parent) => (
@@ -197,7 +198,7 @@ impl<'a> CommitRefIter<'a> {
197198
}
198199
};
199200
let signature = (|i: &mut _| parse::header_field(i, field_name, parse::signature))
200-
.context(err_msg)
201+
.context(StrContext::Expected(err_msg.into()))
201202
.parse_next(&mut i)?;
202203
(
203204
i,
@@ -209,7 +210,7 @@ impl<'a> CommitRefIter<'a> {
209210
}
210211
Encoding => {
211212
let encoding = opt(|i: &mut _| parse::header_field(i, b"encoding", take_till1(NL)))
212-
.context("encoding <encoding>")
213+
.context(StrContext::Expected("encoding <encoding>".into()))
213214
.parse_next(&mut i)?;
214215
*state = State::ExtraHeaders;
215216
match encoding {
@@ -225,7 +226,7 @@ impl<'a> CommitRefIter<'a> {
225226
.map(|(k, o)| (k.as_bstr(), Cow::Borrowed(o.as_bstr())))
226227
},
227228
)))
228-
.context("<field> <single-line|multi-line>")
229+
.context(StrContext::Expected("<field> <single-line|multi-line>".into()))
229230
.parse_next(&mut i)?;
230231
match extra_header {
231232
Some(extra_header) => (i, Token::ExtraHeader(extra_header)),

gix-object/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ pub mod decode {
261261
use crate::bstr::{BString, ByteSlice};
262262

263263
/// The type to be used for parse errors.
264-
pub type ParseError<'a> = winnow::error::VerboseError<&'a [u8]>;
264+
pub type ParseError<'a> = winnow::error::VerboseError<&'a [u8], winnow::error::StrContext>;
265265
/// The owned type to be used for parse errors.
266-
pub type ParseErrorOwned = winnow::error::VerboseError<BString>;
266+
pub type ParseErrorOwned = winnow::error::VerboseError<BString, winnow::error::StrContext>;
267267

268268
pub(crate) fn empty_error() -> Error {
269269
Error {
270-
inner: winnow::error::VerboseError::<BString> { errors: Vec::new() },
270+
inner: winnow::error::VerboseError::<_, _> { errors: Vec::new() },
271271
}
272272
}
273273

gix-object/src/parse.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bstr::{BStr, BString, ByteVec};
22
use winnow::{
33
combinator::repeat,
44
combinator::{preceded, terminated},
5-
error::{AddContext, ParserError},
5+
error::{AddContext, ParserError, StrContext},
66
prelude::*,
77
token::{take_till1, take_until0, take_while},
88
Parser,
@@ -14,7 +14,7 @@ pub(crate) const NL: &[u8] = b"\n";
1414
pub(crate) const SPACE: &[u8] = b" ";
1515
const SPACE_OR_NL: &[u8] = b" \n";
1616

17-
pub(crate) fn any_header_field_multi_line<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(
17+
pub(crate) fn any_header_field_multi_line<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
1818
i: &mut &'a [u8],
1919
) -> PResult<(&'a [u8], BString), E> {
2020
(
@@ -37,7 +37,7 @@ pub(crate) fn any_header_field_multi_line<'a, E: ParserError<&'a [u8]> + AddCont
3737
out
3838
}),
3939
)
40-
.context("name <multi-line-value>")
40+
.context(StrContext::Expected("name <multi-line-value>".into()))
4141
.parse_next(i)
4242
}
4343

@@ -69,7 +69,7 @@ pub fn hex_hash<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<&'a B
6969
.parse_next(i)
7070
}
7171

72-
pub(crate) fn signature<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(
72+
pub(crate) fn signature<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
7373
i: &mut &'a [u8],
7474
) -> PResult<gix_actor::SignatureRef<'a>, E> {
7575
gix_actor::signature::decode(i)

gix-object/src/tag/decode.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ use winnow::{
44
combinator::rest,
55
combinator::{eof, opt},
66
combinator::{preceded, terminated},
7-
error::{AddContext, ParserError},
7+
error::{AddContext, ParserError, StrContext},
88
prelude::*,
99
stream::AsChar,
1010
token::{take_until0, take_while},
1111
};
1212

1313
use crate::{parse, parse::NL, BStr, ByteSlice, TagRef};
1414

15-
pub fn git_tag<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<TagRef<'a>, E> {
15+
pub fn git_tag<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
16+
i: &mut &'a [u8],
17+
) -> PResult<TagRef<'a>, E> {
1618
(
17-
(|i: &mut _| parse::header_field(i, b"object", parse::hex_hash)).context("object <40 lowercase hex char>"),
19+
(|i: &mut _| parse::header_field(i, b"object", parse::hex_hash))
20+
.context(StrContext::Expected("object <40 lowercase hex char>".into())),
1821
(|i: &mut _| parse::header_field(i, b"type", take_while(1.., AsChar::is_alpha)))
1922
.verify_map(|kind| crate::Kind::from_bytes(kind).ok())
20-
.context("type <object kind>"),
21-
(|i: &mut _| parse::header_field(i, b"tag", take_while(1.., |b| b != NL[0]))).context("tag <version>"),
22-
opt(|i: &mut _| parse::header_field(i, b"tagger", parse::signature)).context("tagger <signature>"),
23+
.context(StrContext::Expected("type <object kind>".into())),
24+
(|i: &mut _| parse::header_field(i, b"tag", take_while(1.., |b| b != NL[0])))
25+
.context(StrContext::Expected("tag <version>".into())),
26+
opt(|i: &mut _| parse::header_field(i, b"tagger", parse::signature))
27+
.context(StrContext::Expected("tagger <signature>".into())),
2328
terminated(message, eof),
2429
)
2530
.map(

gix-object/src/tag/ref_iter.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use winnow::{
44
combinator::terminated,
55
combinator::{eof, opt},
66
error::ParserError,
7+
error::StrContext,
78
prelude::*,
89
stream::AsChar,
910
token::take_while,
@@ -64,7 +65,7 @@ impl<'a> TagRefIter<'a> {
6465
Ok(match state {
6566
Target => {
6667
let target = (|i: &mut _| parse::header_field(i, b"object", parse::hex_hash))
67-
.context("object <40 lowercase hex char>")
68+
.context(StrContext::Expected("object <40 lowercase hex char>".into()))
6869
.parse_next(&mut i)?;
6970
*state = TargetKind;
7071
(
@@ -76,7 +77,7 @@ impl<'a> TagRefIter<'a> {
7677
}
7778
TargetKind => {
7879
let kind = (|i: &mut _| parse::header_field(i, b"type", take_while(1.., AsChar::is_alpha)))
79-
.context("type <object kind>")
80+
.context(StrContext::Expected("type <object kind>".into()))
8081
.parse_next(&mut i)?;
8182
let kind = Kind::from_bytes(kind)
8283
.map_err(|_| winnow::error::ErrMode::from_error_kind(&i, winnow::error::ErrorKind::Verify))?;
@@ -85,14 +86,14 @@ impl<'a> TagRefIter<'a> {
8586
}
8687
Name => {
8788
let tag_version = (|i: &mut _| parse::header_field(i, b"tag", take_while(1.., |b| b != NL[0])))
88-
.context("tag <version>")
89+
.context(StrContext::Expected("tag <version>".into()))
8990
.parse_next(&mut i)?;
9091
*state = Tagger;
9192
(i, Token::Name(tag_version.as_bstr()))
9293
}
9394
Tagger => {
9495
let signature = opt(|i: &mut _| parse::header_field(i, b"tagger", parse::signature))
95-
.context("tagger <signature>")
96+
.context(StrContext::Expected("tagger <signature>".into()))
9697
.parse_next(&mut i)?;
9798
*state = Message;
9899
(i, Token::Tagger(signature))

0 commit comments

Comments
 (0)