Skip to content

Commit

Permalink
Merge pull request #2653 from emilbonnek/fix/range-to-from-de-mixup
Browse files Browse the repository at this point in the history
Fix Range{From,To} deserialize mixup
  • Loading branch information
dtolnay authored Nov 21, 2023
2 parents 2083f43 + c91c334 commit 24a78f0
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2509,15 +2509,15 @@ where
where
D: Deserializer<'de>,
{
let start = tri!(deserializer.deserialize_struct(
let end = tri!(deserializer.deserialize_struct(
"RangeFrom",
range_from::FIELDS,
range_from::RangeFromVisitor {
expecting: "struct RangeFrom",
phantom: PhantomData,
},
));
Ok(start..)
Ok(end..)
}
}

Expand All @@ -2526,14 +2526,14 @@ mod range_from {

use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};

pub const FIELDS: &[&str] = &["end"];
pub const FIELDS: &[&str] = &["start"];

// If this were outside of the serde crate, it would just use:
//
// #[derive(Deserialize)]
// #[serde(field_identifier, rename_all = "lowercase")]
enum Field {
End,
Start,
}

impl<'de> Deserialize<'de> for Field {
Expand All @@ -2547,15 +2547,15 @@ mod range_from {
type Value = Field;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`end`")
formatter.write_str("`start`")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
match value {
"end" => Ok(Field::End),
"start" => Ok(Field::Start),
_ => Err(Error::unknown_field(value, FIELDS)),
}
}
Expand All @@ -2565,7 +2565,7 @@ mod range_from {
E: Error,
{
match value {
b"end" => Ok(Field::End),
b"start" => Ok(Field::Start),
_ => {
let value = crate::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&*value, FIELDS))
Expand Down Expand Up @@ -2597,35 +2597,35 @@ mod range_from {
where
A: SeqAccess<'de>,
{
let end: Idx = match tri!(seq.next_element()) {
let start: Idx = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
Ok(end)
Ok(start)
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut end: Option<Idx> = None;
let mut start: Option<Idx> = None;
while let Some(key) = tri!(map.next_key()) {
match key {
Field::End => {
if end.is_some() {
return Err(<A::Error as Error>::duplicate_field("end"));
Field::Start => {
if start.is_some() {
return Err(<A::Error as Error>::duplicate_field("start"));
}
end = Some(tri!(map.next_value()));
start = Some(tri!(map.next_value()));
}
}
}
let end = match end {
Some(end) => end,
None => return Err(<A::Error as Error>::missing_field("end")),
let start = match start {
Some(start) => start,
None => return Err(<A::Error as Error>::missing_field("start")),
};
Ok(end)
Ok(start)
}
}
}
Expand All @@ -2647,15 +2647,15 @@ where
where
D: Deserializer<'de>,
{
let end = tri!(deserializer.deserialize_struct(
let start = tri!(deserializer.deserialize_struct(
"RangeTo",
range_to::FIELDS,
range_to::RangeToVisitor {
expecting: "struct RangeTo",
phantom: PhantomData,
},
));
Ok(..end)
Ok(..start)
}
}

Expand All @@ -2664,14 +2664,14 @@ mod range_to {

use crate::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};

pub const FIELDS: &[&str] = &["start"];
pub const FIELDS: &[&str] = &["end"];

// If this were outside of the serde crate, it would just use:
//
// #[derive(Deserialize)]
// #[serde(field_identifier, rename_all = "lowercase")]
enum Field {
Start,
End,
}

impl<'de> Deserialize<'de> for Field {
Expand All @@ -2685,15 +2685,15 @@ mod range_to {
type Value = Field;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("`start`")
formatter.write_str("`end`")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
match value {
"start" => Ok(Field::Start),
"end" => Ok(Field::End),
_ => Err(Error::unknown_field(value, FIELDS)),
}
}
Expand All @@ -2703,7 +2703,7 @@ mod range_to {
E: Error,
{
match value {
b"start" => Ok(Field::Start),
b"end" => Ok(Field::End),
_ => {
let value = crate::__private::from_utf8_lossy(value);
Err(Error::unknown_field(&*value, FIELDS))
Expand Down Expand Up @@ -2735,35 +2735,35 @@ mod range_to {
where
A: SeqAccess<'de>,
{
let start: Idx = match tri!(seq.next_element()) {
let end: Idx = match tri!(seq.next_element()) {
Some(value) => value,
None => {
return Err(Error::invalid_length(0, &self));
}
};
Ok(start)
Ok(end)
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut start: Option<Idx> = None;
let mut end: Option<Idx> = None;
while let Some(key) = tri!(map.next_key()) {
match key {
Field::Start => {
if start.is_some() {
return Err(<A::Error as Error>::duplicate_field("start"));
Field::End => {
if end.is_some() {
return Err(<A::Error as Error>::duplicate_field("end"));
}
start = Some(tri!(map.next_value()));
end = Some(tri!(map.next_value()));
}
}
}
let start = match start {
Some(start) => start,
None => return Err(<A::Error as Error>::missing_field("start")),
let end = match end {
Some(end) => end,
None => return Err(<A::Error as Error>::missing_field("end")),
};
Ok(start)
Ok(end)
}
}
}
Expand Down

0 comments on commit 24a78f0

Please sign in to comment.