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(spanned): Allow internal fields to be in any order #792

Merged
merged 1 commit into from
Sep 25, 2024
Merged
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
55 changes: 38 additions & 17 deletions crates/serde_spanned/src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,46 @@ where
where
V: serde::de::MapAccess<'de>,
{
if visitor.next_key()? != Some(START_FIELD) {
return Err(serde::de::Error::custom("spanned start key not found"));
let mut start: Option<usize> = None;
let mut end: Option<usize> = None;
let mut value: Option<T> = None;
while let Some(key) = visitor.next_key()? {
match key {
START_FIELD => {
if start.is_some() {
return Err(serde::de::Error::duplicate_field(START_FIELD));
}
start = Some(visitor.next_value()?);
}
END_FIELD => {
if end.is_some() {
return Err(serde::de::Error::duplicate_field(END_FIELD));
}
end = Some(visitor.next_value()?);
}
VALUE_FIELD => {
if value.is_some() {
return Err(serde::de::Error::duplicate_field(VALUE_FIELD));
}
value = Some(visitor.next_value()?);
}
field => {
return Err(serde::de::Error::unknown_field(
field,
&[START_FIELD, END_FIELD, VALUE_FIELD],
));
}
}
}
let start: usize = visitor.next_value()?;

if visitor.next_key()? != Some(END_FIELD) {
return Err(serde::de::Error::custom("spanned end key not found"));
}
let end: usize = visitor.next_value()?;

if visitor.next_key()? != Some(VALUE_FIELD) {
return Err(serde::de::Error::custom("spanned value key not found"));
match (start, end, value) {
(Some(start), Some(end), Some(value)) => Ok(Spanned {
span: start..end,
value,
}),
(None, _, _) => Err(serde::de::Error::missing_field(START_FIELD)),
(_, None, _) => Err(serde::de::Error::missing_field(END_FIELD)),
(_, _, None) => Err(serde::de::Error::missing_field(VALUE_FIELD)),
}
let value: T = visitor.next_value()?;

Ok(Spanned {
span: start..end,
value,
})
}
}

Expand Down
Loading