Skip to content

feat(error): offset-based span tracking#50

Merged
bmwill merged 4 commits intobmwill:masterfrom
weihanglo:span-tracking
Apr 9, 2026
Merged

feat(error): offset-based span tracking#50
bmwill merged 4 commits intobmwill:masterfrom
weihanglo:span-tracking

Conversation

@weihanglo
Copy link
Copy Markdown
Contributor

This is pretty much weihanglo#38 but without richer display.

The richer display there was modeled after the toml crate, though I just found it didn't handle multi-bytes UTF-8 well (including CJK of course). I'd like to just drop it now and maybe that can behind a Cargo feature or an optional config in the future.

Use snapbox for snapshot testing for Display.
This would be useful for span-aware errors.
Add an `offset` field accumulating byte length of every line consumed.
This lays groundwork for attaching byte-offset spans to parse errors.

/// Creates an error with a specific offset as span.
fn error_at(&self, kind: ParsePatchErrorKind, offset: usize) -> ParsePatchError {
ParsePatchError::new(kind, offset..offset)
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.

While we track spans here for room for future, currently just use span.start for everything for simplicity, e.g., error parsing patch at byte 28: unable to parse hunk header.

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.

We could enhance and annotate the header when we have better infra for richer error display.

Comment on lines +25 to +27
pub fn span(&self) -> Option<Range<usize>> {
self.span.clone()
}
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.

Do we want this to be public? I suppose eventually we do want some sort of rich error data to be able to be pulled out.

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.

Just want your thoughts on this, and then i can get this PR merged.

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.

Removed. We should add it when we need it

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 :)

New methods `error()` and `error_at()` attach the current offset
so callers get location info in error messages.
@bmwill bmwill merged commit 398471c into bmwill:master Apr 9, 2026
8 checks passed
@weihanglo weihanglo deleted the span-tracking branch April 9, 2026 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants