Skip to content

Commit

Permalink
Merge pull request #670 from epage/span
Browse files Browse the repository at this point in the history
fix(serde): Improve spans for empty tables
  • Loading branch information
epage authored Jan 31, 2024
2 parents 9db97b3 + 7e23971 commit d00d616
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
29 changes: 29 additions & 0 deletions crates/toml/tests/testsuite/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,3 +1346,32 @@ fn serialize_array_with_enum_of_optional_struct_field() {
let raw = toml::to_string(&input).unwrap();
snapbox::assert_eq(expected, raw);
}

#[test]
fn span_for_sequence_as_map() {
#[allow(dead_code)]
#[derive(Deserialize)]
struct Manifest {
package: Package,
bench: Vec<Bench>,
}

#[derive(Deserialize)]
struct Package {}

#[derive(Deserialize)]
struct Bench {}

let raw = r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[[bench.foo]]
"#;
let err = match toml::from_str::<Manifest>(raw) {
Ok(_) => panic!("should fail"),
Err(err) => err,
};
assert_eq!(err.span(), Some(61..66));
}
8 changes: 4 additions & 4 deletions crates/toml_edit/src/de/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl crate::InlineTable {
pub(crate) struct TableMapAccess {
iter: indexmap::map::IntoIter<crate::InternalString, crate::table::TableKeyValue>,
span: Option<std::ops::Range<usize>>,
value: Option<(crate::InternalString, crate::Item)>,
value: Option<(crate::Key, crate::Item)>,
}

impl TableMapAccess {
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'de> serde::de::MapAccess<'de> for TableMapAccess {
}
e
});
self.value = Some((v.key.into(), v.value));
self.value = Some((v.key, v.value));
ret
}
None => Ok(None),
Expand All @@ -162,13 +162,13 @@ impl<'de> serde::de::MapAccess<'de> for TableMapAccess {
{
match self.value.take() {
Some((k, v)) => {
let span = v.span();
let span = v.span().or_else(|| k.span());
seed.deserialize(crate::de::ValueDeserializer::new(v))
.map_err(|mut e: Self::Error| {
if e.span().is_none() {
e.set_span(span);
}
e.add_key(k.as_str().to_owned());
e.add_key(k.get().to_owned());
e
})
}
Expand Down

0 comments on commit d00d616

Please sign in to comment.