Skip to content

Commit 5825250

Browse files
committed
Refactor Errors to track remaining Input
1 parent 53309be commit 5825250

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

gix-object/src/commit/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ mod write;
6161
impl<'a> CommitRef<'a> {
6262
/// Deserialize a commit from the given `data` bytes while avoiding most allocations.
6363
pub fn from_bytes(mut data: &'a [u8]) -> Result<CommitRef<'a>, crate::decode::Error> {
64-
decode::commit
65-
.parse_next(&mut data)
66-
.map_err(crate::decode::Error::with_err)
64+
let input = &mut data;
65+
match decode::commit.parse_next(input) {
66+
Ok(tag) => Ok(tag),
67+
Err(err) => Err(crate::decode::Error::with_err(err, input)),
68+
}
6769
}
6870
}
6971

gix-object/src/commit/ref_iter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ impl<'a> CommitRefIter<'a> {
156156
#[inline]
157157
fn next_inner(mut i: &'a [u8], state: &mut State) -> Result<(&'a [u8], Token<'a>), crate::decode::Error> {
158158
let input = &mut i;
159-
let token = Self::next_inner_(input, state).map_err(crate::decode::Error::with_err)?;
160-
Ok((input, token))
159+
match Self::next_inner_(input, state) {
160+
Ok(token) => Ok((*input, token)),
161+
Err(err) => Err(crate::decode::Error::with_err(err, input)),
162+
}
161163
}
162164

163165
fn next_inner_(

gix-object/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub mod decode {
267267
pub(crate) fn empty_error() -> Error {
268268
Error {
269269
inner: winnow::error::ContextError::new(),
270+
remaining: Default::default(),
270271
}
271272
}
272273

@@ -275,12 +276,15 @@ pub mod decode {
275276
pub struct Error {
276277
/// The actual error
277278
pub inner: ParseError,
279+
/// Where the error occurred
280+
pub remaining: Vec<u8>,
278281
}
279282

280283
impl Error {
281-
pub(crate) fn with_err(err: winnow::error::ErrMode<ParseError>) -> Self {
284+
pub(crate) fn with_err(err: winnow::error::ErrMode<ParseError>, remaining: &[u8]) -> Self {
282285
Self {
283286
inner: err.into_inner().expect("we don't have streaming parsers"),
287+
remaining: remaining.to_owned(),
284288
}
285289
}
286290
}
@@ -316,7 +320,7 @@ pub mod decode {
316320
}
317321

318322
impl Error {
319-
pub(crate) fn with_err(err: winnow::error::ErrMode<ParseError>) -> Self {
323+
pub(crate) fn with_err(err: winnow::error::ErrMode<ParseError>, _remaining: &[u8]) -> Self {
320324
Self {
321325
inner: err.into_inner().expect("we don't have streaming parsers"),
322326
}

gix-object/src/tag/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ pub mod ref_iter;
1313
impl<'a> TagRef<'a> {
1414
/// Deserialize a tag from `data`.
1515
pub fn from_bytes(mut data: &'a [u8]) -> Result<TagRef<'a>, crate::decode::Error> {
16-
decode::git_tag
17-
.parse_next(&mut data)
18-
.map_err(crate::decode::Error::with_err)
16+
let input = &mut data;
17+
match decode::git_tag.parse_next(input) {
18+
Ok(tag) => Ok(tag),
19+
Err(err) => Err(crate::decode::Error::with_err(err, input)),
20+
}
1921
}
2022
/// The object this tag points to as `Id`.
2123
pub fn target(&self) -> gix_hash::ObjectId {

gix-object/src/tag/ref_iter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ impl<'a> TagRefIter<'a> {
6161
#[inline]
6262
fn next_inner(mut i: &'a [u8], state: &mut State) -> Result<(&'a [u8], Token<'a>), crate::decode::Error> {
6363
let input = &mut i;
64-
let token = Self::next_inner_(input, state).map_err(crate::decode::Error::with_err)?;
65-
Ok((input, token))
64+
match Self::next_inner_(input, state) {
65+
Ok(token) => Ok((*input, token)),
66+
Err(err) => Err(crate::decode::Error::with_err(err, input)),
67+
}
6668
}
6769

6870
fn next_inner_(

gix-object/src/tree/ref_iter.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ impl<'a> TreeRefIter<'a> {
1616
impl<'a> TreeRef<'a> {
1717
/// Deserialize a Tree from `data`.
1818
pub fn from_bytes(mut data: &'a [u8]) -> Result<TreeRef<'a>, crate::decode::Error> {
19-
decode::tree
20-
.parse_next(&mut data)
21-
.map_err(crate::decode::Error::with_err)
19+
let input = &mut data;
20+
match decode::tree.parse_next(input) {
21+
Ok(tag) => Ok(tag),
22+
Err(err) => Err(crate::decode::Error::with_err(err, input)),
23+
}
2224
}
2325

2426
/// Find an entry named `name` knowing if the entry is a directory or not, using a binary search.
@@ -75,6 +77,7 @@ impl<'a> Iterator for TreeRefIter<'a> {
7577
#[allow(clippy::unit_arg)]
7678
Some(Err(crate::decode::Error::with_err(
7779
winnow::error::ErrMode::from_error_kind(&failing, winnow::error::ErrorKind::Verify),
80+
failing,
7881
)))
7982
}
8083
}

0 commit comments

Comments
 (0)