Skip to content

Commit

Permalink
fix(parser): Return correct span for multi-byte characters
Browse files Browse the repository at this point in the history
This is a step towards addressing rust-lang/cargo#13772
  • Loading branch information
epage committed Apr 18, 2024
1 parent c30d31e commit b17c1b6
Show file tree
Hide file tree
Showing 2 changed files with 13 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
4 changes: 2 additions & 2 deletions crates/toml_edit/tests/testsuite/invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ dotted key `k1` attempted to extend non-table type (integer)
}

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

0 comments on commit b17c1b6

Please sign in to comment.