diff --git a/crates/toml_edit/src/error.rs b/crates/toml_edit/src/error.rs index d9630fca..6a278024 100644 --- a/crates/toml_edit/src/error.rs +++ b/crates/toml_edit/src/error.rs @@ -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), } diff --git a/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr b/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr index 7ada2f21..10592cfb 100644 --- a/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr +++ b/crates/toml_edit/tests/fixtures/invalid/key/special-character.stderr @@ -1,5 +1,5 @@ TOML parse error at line 1, column 1 | 1 | μ = "greek small letter mu" - | ^ + | ^^ invalid key diff --git a/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr b/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr index a7adf382..26fc7875 100644 --- a/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr +++ b/crates/toml_edit/tests/fixtures/invalid/table/no-close-2.stderr @@ -1,6 +1,6 @@ TOML parse error at line 1, column 25 | 1 | [closing-bracket.missingö - | ^ + | ^^ invalid table header expected `.`, `]` diff --git a/crates/toml_edit/tests/testsuite/invalid.rs b/crates/toml_edit/tests/testsuite/invalid.rs index 5e7acca0..79724163 100644 --- a/crates/toml_edit/tests/testsuite/invalid.rs +++ b/crates/toml_edit/tests/testsuite/invalid.rs @@ -209,3 +209,12 @@ dotted key `k1` attempted to extend non-table type (integer) let err = toml_input.parse::().unwrap_err(); snapbox::assert_eq(expected_err, err.to_string()); } + +#[test] +fn emoji_error_span() { + let input = "😀"; + let err = input.parse::().unwrap_err(); + dbg!(err.span()); + let actual = &input[err.span().unwrap()]; + assert_eq!(actual, input); +}