Skip to content

Commit

Permalink
Revert "Add a test case showing the new flexibility of Seq"
Browse files Browse the repository at this point in the history
This reverts commit 0e4fef6.
  • Loading branch information
jonasbb committed Mar 10, 2023
1 parent 2be6973 commit 262407a
Showing 1 changed file with 0 additions and 71 deletions.
71 changes: 0 additions & 71 deletions serde_with/tests/serde_as/map_tuple_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,74 +319,3 @@ fn test_tuple_array_as_map() {
}"#]],
);
}

#[test]
fn test_map_as_custom_struct() {
let ip = "1.2.3.4".parse().unwrap();
let ip2 = "255.255.255.255".parse().unwrap();

use serde_with::{de::DeserializeAsWrap, ser::SerializeAsWrap, DeserializeAs, SerializeAs};

#[derive(serde::Serialize, serde::Deserialize)]
struct Custom<K, V> {
custom_key: K,
v: V,
}

impl<K, KAs, V, VAs> SerializeAs<(K, V)> for Custom<KAs, VAs>
where
KAs: SerializeAs<K>,
VAs: SerializeAs<V>,
{
fn serialize_as<S>((k, v): &(K, V), serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
(Custom {
custom_key: SerializeAsWrap::<K, KAs>::new(k),
v: SerializeAsWrap::<V, VAs>::new(v),
})
.serialize(serializer)
}
}

impl<'de, K, KAs, V, VAs> DeserializeAs<'de, (K, V)> for Custom<KAs, VAs>
where
KAs: DeserializeAs<'de, K>,
VAs: DeserializeAs<'de, V>,
{
fn deserialize_as<D>(deserializer: D) -> Result<(K, V), D::Error>
where
D: serde::Deserializer<'de>,
{
let c = <Custom<DeserializeAsWrap<K, KAs>, DeserializeAsWrap<V, VAs>>>::deserialize(
deserializer,
)?;
Ok((c.custom_key.into_inner(), c.v.into_inner()))
}
}

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SM(#[serde_as(as = "Seq<Custom<_, _>>")] BTreeMap<u32, IpAddr>);

let map: BTreeMap<_, _> = vec![(1, ip), (10, ip), (200, ip2)].into_iter().collect();
is_equal(
SM(map),
expect![[r#"
[
{
"custom_key": 1,
"v": "1.2.3.4"
},
{
"custom_key": 10,
"v": "1.2.3.4"
},
{
"custom_key": 200,
"v": "255.255.255.255"
}
]"#]],
);
}

0 comments on commit 262407a

Please sign in to comment.