Skip to content

Commit

Permalink
🐛 avoid panic when patch is unparseable
Browse files Browse the repository at this point in the history
  • Loading branch information
mleduque committed Feb 5, 2024
1 parent b7807aa commit 6dc8be3
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions patch-rs/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,31 @@ fn consume_content_line(input: Input<'_>) -> IResult<Input<'_>, &str> {
pub(crate) fn parse_single_patch(s: &str) -> Result<Patch, ParseError<'_>> {
let (remaining_input, patch) = patch(Input::new(s))?;
// Parser should return an error instead of producing remaining input
assert!(
remaining_input.fragment().is_empty(),
"bug: failed to parse entire input. \
Remaining: '{}'",
remaining_input.fragment()
);
Ok(patch)
if remaining_input.fragment().is_empty() {
Ok(patch)
} else {
Err(ParseError{
line: remaining_input.location_line(),
offset: remaining_input.location_offset(),
fragment: remaining_input.fragment(),
kind: nom::error::ErrorKind::Eof,
})
}
}

pub(crate) fn parse_multiple_patches(s: &str) -> Result<Vec<Patch>, ParseError<'_>> {
let (remaining_input, patches) = multiple_patches(Input::new(s))?;
// Parser should return an error instead of producing remaining input
assert!(
remaining_input.fragment().is_empty(),
"bug: failed to parse entire input. \
Remaining: '{}'",
remaining_input.fragment()
);
Ok(patches)
if remaining_input.fragment().is_empty() {
Ok(patches)
} else {
Err(ParseError{
line: remaining_input.location_line(),
offset: remaining_input.location_offset(),
fragment: remaining_input.fragment(),
kind: nom::error::ErrorKind::Eof,
})
}
}

fn multiple_patches(input: Input<'_>) -> IResult<Input<'_>, Vec<Patch>> {
Expand Down

0 comments on commit 6dc8be3

Please sign in to comment.