Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::borrow::Cow;
use std::fmt;

use chrono::{DateTime, FixedOffset};
use nom_locate::LocatedSpan;

use crate::parser::{parse_multiple_patches, parse_single_patch, ParseError};
use crate::parser::{
parse_multiple_patches, parse_single_patch, parse_single_patch_with_remaining, ParseError,
};

/// A complete patch summarizing the differences between two files
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -82,6 +85,12 @@ impl<'a> Patch<'a> {
parse_single_patch(s)
}

pub fn from_single_with_remaining(
s: &'a str,
) -> Result<(Self, Option<LocatedSpan<&'a str>>), ParseError<'a>> {
parse_single_patch_with_remaining(s)
}

/// Attempt to parse as many patches as possible from the given string. This is useful for when
/// you have a complete diff of many files.
///
Expand Down
26 changes: 19 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use nom::{
multi::{many0, many1},
sequence::{delimited, preceded, terminated, tuple},
};
use nom_locate::LocatedSpan;

use crate::ast::*;

Expand Down Expand Up @@ -75,16 +76,27 @@ fn consume_content_line(input: Input<'_>) -> IResult<Input<'_>, (&str, bool)> {

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()
);
if !remaining_input.fragment().is_empty() {
return Err(ParseError {
line: remaining_input.location_line(),
offset: 0,
fragment: remaining_input.fragment(),
kind: nom::error::ErrorKind::Fail,
});
}
Ok(patch)
}

pub(crate) fn parse_single_patch_with_remaining(
s: &str,
) -> Result<(Patch<'_>, Option<LocatedSpan<&str>>), ParseError<'_>> {
let (remaining_input, patch) = patch(Input::new(s))?;
if remaining_input.fragment().is_empty() {
return Ok((patch, None));
}
Ok((patch, Some(remaining_input)))
}

pub(crate) fn parse_multiple_patches(s: &str) -> Result<Vec<Patch<'_>>, ParseError<'_>> {
let (remaining_input, patches) = multiple_patches(Input::new(s))?;
debug_assert!(
Expand Down