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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ color = ["dep:anstyle"]
[dependencies]
anstyle = { version = "1.0.13", optional = true }

[dev-dependencies]
snapbox = "0.6.24"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used snapbox before, but have used tools like cargo-insta before. I wonder if it would make sense to eventually migrate more of these tests to be snapshot based vs trying to encode these things in strs.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something that needs to be tackled as a part of this PR, i'm more just considering things we could do in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose it because

  • I am more familiar with snapbox
  • It doesn't require any other CLI tool. Just env SNAPSHOTS=overwrite cargo test. Though that also means you don't have fancy snapshot reviews. Can cut over if needed :)


[[example]]
name = "patch_formatter"
required-features = ["color"]
24 changes: 22 additions & 2 deletions src/patch/error.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
//! Error types for patch parsing.

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

/// An error returned when parsing a `Patch` using [`Patch::from_str`] fails.
///
/// [`Patch::from_str`]: struct.Patch.html#method.from_str
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsePatchError {
pub(crate) kind: ParsePatchErrorKind,
span: Option<Range<usize>>,
}

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod format;
mod parse;
#[cfg(feature = "color")]
mod style;
#[cfg(test)]
mod tests;

pub use error::ParsePatchError;
pub use format::PatchFormatter;
Expand Down
Loading
Loading