Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Return correct span for multi-byte characters #713

Merged
merged 2 commits into from
Apr 18, 2024
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
18 changes: 11 additions & 7 deletions crates/toml_edit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ impl TomlError {
) -> Self {
use winnow::stream::Stream;

let message = error.inner().to_string();
let raw = raw.finish();
let raw = String::from_utf8(raw.to_owned()).expect("original document was utf8");

let offset = error.offset();
let span = if offset == raw.len() {
offset..offset
let mut indices = raw[offset..].char_indices();
indices.next();
let len = if let Some((index, _)) = indices.next() {
index
} else {
offset..(offset + 1)
raw.len()
};

let message = error.inner().to_string();
let raw = raw.finish();
let span = offset..(offset + len);

Self {
message,
raw: Some(String::from_utf8(raw.to_owned()).expect("original document was utf8")),
raw: Some(raw),
keys: Vec::new(),
span: Some(span),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TOML parse error at line 1, column 1
|
1 | μ = "greek small letter mu"
| ^
| ^^
invalid key
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TOML parse error at line 1, column 25
|
1 | [closing-bracket.missingö
| ^
| ^^
invalid table header
expected `.`, `]`
9 changes: 9 additions & 0 deletions crates/toml_edit/tests/testsuite/invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,12 @@ dotted key `k1` attempted to extend non-table type (integer)
let err = toml_input.parse::<toml_edit::DocumentMut>().unwrap_err();
snapbox::assert_eq(expected_err, err.to_string());
}

#[test]
fn emoji_error_span() {
let input = "😀";
let err = input.parse::<toml_edit::DocumentMut>().unwrap_err();
dbg!(err.span());
let actual = &input[err.span().unwrap()];
assert_eq!(actual, input);
}
Loading