From 6dc8be361ac6fb10d17a02e3ee041ff93a5fa64d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl?= Date: Mon, 5 Feb 2024 13:59:34 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20avoid=20panic=20when=20patch=20i?= =?UTF-8?q?s=20unparseable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- patch-rs/src/parser.rs | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/patch-rs/src/parser.rs b/patch-rs/src/parser.rs index ebe1679..0c4e3b0 100644 --- a/patch-rs/src/parser.rs +++ b/patch-rs/src/parser.rs @@ -69,25 +69,31 @@ fn consume_content_line(input: Input<'_>) -> IResult, &str> { pub(crate) fn parse_single_patch(s: &str) -> Result> { 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, 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, Vec> {