Skip to content

Commit

Permalink
Merge pull request #713 from epage/emoji
Browse files Browse the repository at this point in the history
fix(parser): Return correct span for multi-byte characters
  • Loading branch information
epage authored Apr 18, 2024
2 parents 48f968b + 1a6ed6e commit 2ca30f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
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);
}

0 comments on commit 2ca30f6

Please sign in to comment.