Skip to content
Open
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
51 changes: 30 additions & 21 deletions src/external/serde_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,20 @@ impl Serialize for NonNilUuid {
}
}

impl Serialize for Hyphenated {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}

impl Serialize for Simple {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}

impl Serialize for Urn {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
macro_rules! serialize_uuid_proxy {
($type:ty) => {
impl Serialize for $type {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}
};
}

impl Serialize for Braced {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}
serialize_uuid_proxy!(Hyphenated);
serialize_uuid_proxy!(Simple);
serialize_uuid_proxy!(Urn);
serialize_uuid_proxy!(Braced);

impl<'de> Deserialize<'de> for Uuid {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Expand Down Expand Up @@ -138,6 +129,24 @@ impl<'de> Deserialize<'de> for Uuid {
}
}

macro_rules! deserialize_uuid_proxy {
($type:ty) => {
impl<'de> Deserialize<'de> for $type {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Uuid::deserialize(deserializer)?.into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're trying to deserialize a UUID into one of these proxy types, I think we should only accept a UUID in its format. We should be able to make the visitor used in Uuid's deserialize impl generic and add appropriate FromStr implementations to the proxy types. Then the bound on that visitor would be any T: FromStr + From<Uuid>.

I'd be happy to sketch it out a bit further if that would be helpful 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand what your goal is. The proxy types are just formatting wrappers that each just simply hold a Uuid in a Unit struct. Anything that is a Uuid can be wrapped in these proxy types.

You mean, that if you wish to deserialise a Hyphenated, the input may only be a hyphenated?

If yes, I think I get what you mean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kek5chen That’s right; I think deserlializing a hyphenated UUID should only succeed on a hyphenated input.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I can change it to have that behavior

}
}
};
}

deserialize_uuid_proxy!(Hyphenated);
deserialize_uuid_proxy!(Simple);
deserialize_uuid_proxy!(Urn);
deserialize_uuid_proxy!(Braced);

impl<'de> Deserialize<'de> for NonNilUuid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down