Skip to content

Commit 86d7fd1

Browse files
committed
Simplify winnow ErrMode construction
Things resolved - Use traits on `winnow::ErrMode` instead of manually constructing (anytime) - Switch error trait functions to use umcs (anytime) Things to do later - Rely on `ErrMode::into_inner` (0.5) - Rely on `ErrMode::add_context` (0.5)
1 parent ee75de1 commit 86d7fd1

File tree

7 files changed

+24
-32
lines changed

7 files changed

+24
-32
lines changed

gix-actor/src/signature/decode.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ pub(crate) mod function {
2929
tag(b" "),
3030
context("<timestamp>", |i| {
3131
terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| {
32-
btoi::<SecondsSinceUnixEpoch>(v).map(|v| (i, v)).map_err(|_| {
33-
winnow::Err::Backtrack(E::from_error_kind(i, winnow::error::ErrorKind::MapRes))
34-
})
32+
btoi::<SecondsSinceUnixEpoch>(v)
33+
.map(|v| (i, v))
34+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
3535
})
3636
}),
3737
context(
@@ -43,16 +43,16 @@ pub(crate) mod function {
4343
),
4444
context("HH", |i| {
4545
take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| {
46-
btoi::<OffsetInSeconds>(v).map(|v| (i, v)).map_err(|_| {
47-
winnow::Err::Backtrack(E::from_error_kind(i, winnow::error::ErrorKind::MapRes))
48-
})
46+
btoi::<OffsetInSeconds>(v)
47+
.map(|v| (i, v))
48+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
4949
})
5050
}),
5151
context("MM", |i| {
5252
take_while_m_n(1usize, 2, is_digit)(i).and_then(|(i, v)| {
53-
btoi::<OffsetInSeconds>(v).map(|v| (i, v)).map_err(|_| {
54-
winnow::Err::Backtrack(E::from_error_kind(i, winnow::error::ErrorKind::MapRes))
55-
})
53+
btoi::<OffsetInSeconds>(v)
54+
.map(|v| (i, v))
55+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))
5656
})
5757
}),
5858
)),

gix-object/src/commit/decode.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ use crate::{parse, parse::NL, BStr, ByteSlice, CommitRef};
1515
pub fn message<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a BStr, E> {
1616
if i.is_empty() {
1717
// newline + [message]
18-
return Err(winnow::Err::Backtrack(E::add_context(
19-
E::from_error_kind(i, winnow::error::ErrorKind::Eof),
20-
i,
21-
"newline + <message>",
22-
)));
18+
return Err(winnow::Err::from_error_kind(i, winnow::error::ErrorKind::Eof)
19+
.map(|err: E| err.add_context(i, "newline + <message>")));
2320
}
2421
let (i, _) = context("a newline separates headers from the message", tag(NL))(i)?;
2522
Ok((&[], i.as_bstr()))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct TrailerRef<'a> {
3535
fn parse_single_line_trailer<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
3636
let (value, token) = terminated(take_until1(b":".as_ref()), tag(b": "))(i.trim_end())?;
3737
if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
38-
Err(winnow::Err::Cut(E::from_error_kind(i, ErrorKind::Fail)))
38+
Err(winnow::Err::from_error_kind(i, ErrorKind::Fail).cut())
3939
} else {
4040
Ok((&[], (token.as_bstr(), value.as_bstr())))
4141
}

gix-object/src/tag/decode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub fn git_tag<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]
1818
let (i, kind) = context("type <object kind>", |i| {
1919
parse::header_field(i, b"type", take_while1(is_alphabetic))
2020
})(i)?;
21-
let kind = crate::Kind::from_bytes(kind)
22-
.map_err(|_| winnow::Err::Backtrack(E::from_error_kind(i, winnow::error::ErrorKind::MapRes)))?;
21+
let kind =
22+
crate::Kind::from_bytes(kind).map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))?;
2323

2424
let (i, tag_version) = context("tag <version>", |i| {
2525
parse::header_field(i, b"tag", take_while1(|b| b != NL[0]))

gix-object/src/tag/ref_iter.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,8 @@ impl<'a> TagRefIter<'a> {
7676
let (i, kind) = context("type <object kind>", |i| {
7777
parse::header_field(i, b"type", take_while1(is_alphabetic))
7878
})(i)?;
79-
let kind = Kind::from_bytes(kind).map_err(|_| {
80-
#[allow(clippy::let_unit_value)]
81-
{
82-
let err = crate::decode::ParseError::from_error_kind(i, winnow::error::ErrorKind::MapRes);
83-
winnow::Err::Backtrack(err)
84-
}
85-
})?;
79+
let kind = Kind::from_bytes(kind)
80+
.map_err(|_| winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes))?;
8681
*state = Name;
8782
(i, Token::TargetKind(kind))
8883
}

gix-object/src/tree/ref_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ impl<'a> Iterator for TreeRefIter<'a> {
6969
None => {
7070
self.data = &[];
7171
#[allow(clippy::unit_arg)]
72-
Some(Err(winnow::Err::Backtrack(crate::decode::ParseError::from_error_kind(
72+
Some(Err(winnow::Err::from_error_kind(
7373
&[] as &[u8],
7474
winnow::error::ErrorKind::MapRes,
75-
))
75+
)
7676
.into()))
7777
}
7878
}
@@ -163,7 +163,7 @@ mod decode {
163163
pub fn entry<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], EntryRef<'_>, E> {
164164
let (i, mode) = terminated(take_while_m_n(5, 6, is_digit), tag(SPACE))(i)?;
165165
let mode = tree::EntryMode::try_from(mode)
166-
.map_err(|invalid| winnow::Err::Backtrack(E::from_error_kind(invalid, winnow::error::ErrorKind::MapRes)))?;
166+
.map_err(|invalid| winnow::Err::from_error_kind(invalid, winnow::error::ErrorKind::MapRes))?;
167167
let (i, filename) = terminated(take_while1(|b| b != NULL[0]), tag(NULL))(i)?;
168168
let (i, oid) = take(20u8)(i)?; // TODO: make this compatible with other hash lengths
169169

gix-ref/src/store/file/log/line.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ pub mod decode {
146146
if message_sep.is_none() {
147147
if let Some(first) = message.first() {
148148
if !first.is_ascii_whitespace() {
149-
return Err(winnow::Err::Backtrack(E::add_context(
150-
E::from_error_kind(i, winnow::error::ErrorKind::MapRes),
151-
i,
152-
"log message must be separated from signature with whitespace",
153-
)));
149+
return Err(
150+
winnow::Err::from_error_kind(i, winnow::error::ErrorKind::MapRes).map(|err: E| {
151+
err.add_context(i, "log message must be separated from signature with whitespace")
152+
}),
153+
);
154154
}
155155
}
156156
}

0 commit comments

Comments
 (0)