Skip to content
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
Cargo.lock
199 changes: 199 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ mod apply;
mod diff;
mod merge;
mod patch;
pub mod patch_set;
mod range;
mod utils;

Expand Down
10 changes: 9 additions & 1 deletion src/patch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod error;
mod format;
mod parse;
pub(crate) mod parse;
#[cfg(feature = "color")]
mod style;
#[cfg(test)]
Expand Down Expand Up @@ -69,6 +69,14 @@ impl<'a, T: ToOwned + ?Sized> Patch<'a, T> {
}
}

pub(crate) fn original_path(&self) -> Option<&Cow<'a, T>> {
self.original.as_ref().map(|f| &f.0)
}

pub(crate) fn modified_path(&self) -> Option<&Cow<'a, T>> {
self.modified.as_ref().map(|f| &f.0)
}

/// Return the name of the old file
pub fn original(&self) -> Option<&T> {
self.original.as_ref().map(AsRef::as_ref)
Expand Down
25 changes: 21 additions & 4 deletions src/patch/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,32 @@ impl<'a, T: Text + ?Sized> Parser<'a, T> {
}

pub fn parse(input: &str) -> Result<Patch<'_, str>> {
let (result, _consumed) = parse_one(input);
result
}

/// Parses one patch from input.
///
/// Always returns consumed bytes alongside the result
/// so callers can advance past the parsed or partially parsed content.
pub(crate) fn parse_one(input: &str) -> (Result<Patch<'_, str>>, usize) {
let mut parser = Parser::new(input);
let header = patch_header(&mut parser)?;
let hunks = hunks(&mut parser)?;

Ok(Patch::new(
let header = match patch_header(&mut parser) {
Ok(h) => h,
Err(e) => return (Err(e), parser.offset()),
};
let hunks = match hunks(&mut parser) {
Ok(h) => h,
Err(e) => return (Err(e), parser.offset()),
};

let patch = Patch::new(
header.0.map(convert_cow_to_str),
header.1.map(convert_cow_to_str),
hunks,
))
);
(Ok(patch), parser.offset())
}

pub fn parse_strict(input: &str) -> Result<Patch<'_, str>> {
Expand Down
92 changes: 92 additions & 0 deletions src/patch_set/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! Error types for patches parsing.

use std::fmt;
use std::ops::Range;

use crate::patch::ParsePatchError;

/// An error returned when parsing patches fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchSetParseError {
pub(crate) kind: PatchSetParseErrorKind,
span: Option<Range<usize>>,
}

impl PatchSetParseError {
/// Creates a new error with the given kind and span.
pub(crate) fn new(kind: PatchSetParseErrorKind, span: Range<usize>) -> Self {
Self {
kind,
span: Some(span),
}
}

/// Sets the byte range span for this error.
pub(crate) fn set_span(&mut self, span: Range<usize>) {
self.span = Some(span);
}
}

impl fmt::Display for PatchSetParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(span) = &self.span {
write!(
f,
"error parsing patches at byte {}: {}",
span.start, self.kind
)
} else {
write!(f, "error parsing patches: {}", self.kind)
}
}
}

impl std::error::Error for PatchSetParseError {}

impl From<PatchSetParseErrorKind> for PatchSetParseError {
fn from(kind: PatchSetParseErrorKind) -> Self {
Self { kind, span: None }
}
}

/// The kind of error that occurred when parsing patches.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub(crate) enum PatchSetParseErrorKind {
/// Single patch parsing failed.
Patch(ParsePatchError),

/// No valid patches found in input.
NoPatchesFound,

/// Patch has no file path.
NoFilePath,

/// Patch has both original and modified as /dev/null.
BothDevNull,

/// Delete patch missing original path.
DeleteMissingOriginalPath,

/// Create patch missing modified path.
CreateMissingModifiedPath,
}

impl fmt::Display for PatchSetParseErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Patch(e) => write!(f, "{e}"),
Self::NoPatchesFound => write!(f, "no valid patches found"),
Self::NoFilePath => write!(f, "patch has no file path"),
Self::BothDevNull => write!(f, "patch has both original and modified as /dev/null"),
Self::DeleteMissingOriginalPath => write!(f, "delete patch has no original path"),
Self::CreateMissingModifiedPath => write!(f, "create patch has no modified path"),
}
}
}

impl From<ParsePatchError> for PatchSetParseError {
fn from(e: ParsePatchError) -> Self {
PatchSetParseErrorKind::Patch(e).into()
}
}
Loading
Loading