Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
fix(dashcore): make OutPoint serde tolerant of ContentDeserializer's HR-quirk#708
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec54f45
fix(dashcore): make `OutPoint` serde survive ContentDeserializer replays
shumkov 4dc2918
fix(test): drop bincode round-trip from OutPoint serde regression test
shumkov aef57a4
test(dashcore): restore bincode side of OutPoint serde tagged-enum test
shumkov 622484d
test(dashcore): use plain OutPoint bincode round-trip instead of tagged
shumkov 863159c
fix(test): drop needless borrow flagged by clippy
shumkov be00e51
review: add map-form regression test and reject duplicate fields
shumkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -358,143 +358,155 @@ pub(crate) use {serde_string_deserialize_impl, serde_string_impl, serde_string_s | ||
| /// A combination macro where the human-readable serialization is done like | ||
| /// serde_string_impl and the non-human-readable impl is done as a struct. | ||
| /// | ||
| /// The deserialize impl uses a *single* visitor that handles all three shapes | ||
| /// (`visit_str`, `visit_seq`, `visit_map`) — required to interoperate with | ||
| /// serde's `ContentDeserializer`, the format-agnostic intermediate buffer | ||
| /// serde uses to dispatch internally-tagged enums (`#[serde(tag = "...")]`), | ||
| /// `flatten`, and untagged enums. `ContentDeserializer` always reports | ||
| /// `is_human_readable() == true` regardless of the upstream format (this is | ||
| /// intentional in serde's source: see long-standing upstream issues — the | ||
| /// recommended pattern is "don't branch on `is_human_readable()` for shape | ||
| /// dispatch — accept any shape"). A value originally written by a | ||
| /// non-human-readable encoder can therefore be replayed into the | ||
| /// human-readable branch as a map and must be accepted there. See the | ||
| /// regression test | ||
| /// `outpoint::tests::serde_round_trip_through_internally_tagged_enum`. | ||
| macro_rules! serde_struct_human_string_impl { | ||
| ($name:ident, $expecting:literal, $($fe:ident),*) => ( | ||
| impl<'de> $crate::serde::Deserialize<'de> for $name { | ||
| fn deserialize<D>(deserializer: D) -> Result<$name, D::Error> | ||
| where | ||
| D: $crate::serde::de::Deserializer<'de>, | ||
| { | ||
| if deserializer.is_human_readable() { | ||
| use core::fmt::{self, Formatter}; | ||
| use core::str::FromStr; | ||
| struct Visitor; | ||
| impl<'de> $crate::serde::de::Visitor<'de> for Visitor { | ||
| type Value = $name; | ||
| use core::fmt::{self, Formatter}; | ||
| use core::str::FromStr; | ||
| use $crate::serde::de::IgnoredAny; | ||
| fn expecting(&self, f: &mut Formatter) -> fmt::Result { | ||
| f.write_str($expecting) | ||
| } | ||
| #[allow(non_camel_case_types)] | ||
| enum Enum { Unknown__Field, $($fe),* } | ||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: $crate::serde::de::Error, | ||
| { | ||
| $name::from_str(v).map_err(E::custom) | ||
| } | ||
| struct EnumVisitor; | ||
| impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor { | ||
| type Value = Enum; | ||
| fn expecting(&self, f: &mut Formatter) -> fmt::Result { | ||
| f.write_str("a field name") | ||
| } | ||
| deserializer.deserialize_str(Visitor) | ||
| } else { | ||
| use core::fmt::{self, Formatter}; | ||
| use $crate::serde::de::IgnoredAny; | ||
| #[allow(non_camel_case_types)] | ||
| enum Enum { Unknown__Field, $($fe),* } | ||
| struct EnumVisitor; | ||
| impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor { | ||
| type Value = Enum; | ||
| fn expecting(&self, f: &mut Formatter) -> fmt::Result { | ||
| f.write_str("a field name") | ||
| } | ||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: $crate::serde::de::Error, | ||
| { | ||
| match v { | ||
| $( | ||
| stringify!($fe) => Ok(Enum::$fe) | ||
| ),*, | ||
| _ => Ok(Enum::Unknown__Field) | ||
| } | ||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: $crate::serde::de::Error, | ||
| { | ||
| match v { | ||
| $( | ||
| stringify!($fe) => Ok(Enum::$fe) | ||
| ),*, | ||
| _ => Ok(Enum::Unknown__Field) | ||
| } | ||
| } | ||
| } | ||
| impl<'de> $crate::serde::Deserialize<'de> for Enum { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: $crate::serde::de::Deserializer<'de>, | ||
| { | ||
| deserializer.deserialize_str(EnumVisitor) | ||
| } | ||
| impl<'de> $crate::serde::Deserialize<'de> for Enum { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: $crate::serde::de::Deserializer<'de>, | ||
| { | ||
| deserializer.deserialize_str(EnumVisitor) | ||
| } | ||
| } | ||
| struct Visitor; | ||
| impl<'de> $crate::serde::de::Visitor<'de> for Visitor { | ||
| type Value = $name; | ||
| struct Visitor; | ||
| fn expecting(&self, f: &mut Formatter) -> fmt::Result { | ||
| f.write_str("a struct") | ||
| } | ||
| impl<'de> $crate::serde::de::Visitor<'de> for Visitor { | ||
| type Value = $name; | ||
| fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error> | ||
| where | ||
| V: $crate::serde::de::SeqAccess<'de>, | ||
| { | ||
| use $crate::serde::de::Error; | ||
| fn expecting(&self, f: &mut Formatter) -> fmt::Result { | ||
| f.write_str($expecting) | ||
| } | ||
| let length = 0; | ||
| $( | ||
| let $fe = seq.next_element()?.ok_or_else(|| { | ||
| Error::invalid_length(length, &self) | ||
| })?; | ||
| #[allow(unused_variables)] | ||
| let length = length + 1; | ||
| )* | ||
| let ret = $name { | ||
| $($fe),* | ||
| }; | ||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: $crate::serde::de::Error, | ||
| { | ||
| $name::from_str(v).map_err(E::custom) | ||
| } | ||
| Ok(ret) | ||
| } | ||
| fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error> | ||
| where | ||
| V: $crate::serde::de::SeqAccess<'de>, | ||
| { | ||
| use $crate::serde::de::Error; | ||
| let length = 0; | ||
| $( | ||
| let $fe = seq.next_element()?.ok_or_else(|| { | ||
| Error::invalid_length(length, &self) | ||
| })?; | ||
| #[allow(unused_variables)] | ||
| let length = length + 1; | ||
| )* | ||
| let ret = $name { | ||
| $($fe),* | ||
| }; | ||
| Ok(ret) | ||
| } | ||
| fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> | ||
| where | ||
| A: $crate::serde::de::MapAccess<'de>, | ||
| { | ||
| use $crate::serde::de::Error; | ||
| fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> | ||
| where | ||
| A: $crate::serde::de::MapAccess<'de>, | ||
| { | ||
| use $crate::serde::de::Error; | ||
| $(let mut $fe = None;)* | ||
| $(let mut $fe = None;)* | ||
| loop { | ||
| match map.next_key::<Enum>()? { | ||
| Some(Enum::Unknown__Field) => { | ||
| map.next_value::<IgnoredAny>()?; | ||
| } | ||
| $( | ||
| Some(Enum::$fe) => { | ||
| $fe = Some(map.next_value()?); | ||
| } | ||
| )* | ||
| None => { break; } | ||
| loop { | ||
| match map.next_key::<Enum>()? { | ||
| Some(Enum::Unknown__Field) => { | ||
| map.next_value::<IgnoredAny>()?; | ||
| } | ||
| $( | ||
| Some(Enum::$fe) => { | ||
| if $fe.is_some() { | ||
| return Err(A::Error::duplicate_field(stringify!($fe))); | ||
| } | ||
| $fe = Some(map.next_value()?); | ||
| } | ||
| )* | ||
| None => { break; } | ||
| } | ||
| } | ||
| $( | ||
| let $fe = match $fe { | ||
| Some(x) => x, | ||
| None => return Err(A::Error::missing_field(stringify!($fe))), | ||
| }; | ||
| )* | ||
| let ret = $name { | ||
| $($fe),* | ||
| $( | ||
| let $fe = match $fe { | ||
| Some(x) => x, | ||
| None => return Err(A::Error::missing_field(stringify!($fe))), | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }; | ||
| )* | ||
| Ok(ret) | ||
| } | ||
| let ret = $name { | ||
| $($fe),* | ||
| }; | ||
| Ok(ret) | ||
| } | ||
| // end type defs | ||
| } | ||
| // end type defs | ||
| static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*]; | ||
| static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*]; | ||
| if deserializer.is_human_readable() { | ||
| // Self-describing format (raw JSON, or a `ContentDeserializer` | ||
| // replaying buffered content for an internally-tagged enum). | ||
| // `deserialize_any` lets the deserializer dispatch to whichever | ||
| // visit_* method matches the actual data shape — so we accept | ||
| // the canonical "txid:vout" string form, plus the struct/seq | ||
| // forms that show up when a non-human-readable struct is | ||
| // re-deserialized via `ContentDeserializer`. | ||
| deserializer.deserialize_any(Visitor) | ||
| } else { | ||
| deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor) | ||
| } | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.