From 315c6e1125badae4b9b7446749d8cc7936a7929a Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Sun, 2 Jul 2023 00:55:00 +0200 Subject: [PATCH] Change uuid serialisation to compact (#2204) --- lib/src/sql/uuid.rs | 2 +- lib/src/sql/value/serde/ser/primitive/mod.rs | 1 + lib/src/sql/value/serde/ser/primitive/u8.rs | 25 +++++++++++++++ lib/src/sql/value/serde/ser/uuid/mod.rs | 33 ++++++++++++++++++-- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 lib/src/sql/value/serde/ser/primitive/u8.rs diff --git a/lib/src/sql/uuid.rs b/lib/src/sql/uuid.rs index 6a3901b1..45d5520a 100644 --- a/lib/src/sql/uuid.rs +++ b/lib/src/sql/uuid.rs @@ -18,7 +18,7 @@ pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Uuid"; #[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Hash)] #[serde(rename = "$surrealdb::private::sql::Uuid")] -pub struct Uuid(pub uuid::Uuid); +pub struct Uuid(#[serde(with = "uuid::serde::compact")] pub uuid::Uuid); impl From for Uuid { fn from(v: uuid::Uuid) -> Self { diff --git a/lib/src/sql/value/serde/ser/primitive/mod.rs b/lib/src/sql/value/serde/ser/primitive/mod.rs index ff29e8ee..72d36342 100644 --- a/lib/src/sql/value/serde/ser/primitive/mod.rs +++ b/lib/src/sql/value/serde/ser/primitive/mod.rs @@ -3,5 +3,6 @@ pub mod f64; pub mod i64; pub mod u32; pub mod u64; +pub mod u8; mod opt; diff --git a/lib/src/sql/value/serde/ser/primitive/u8.rs b/lib/src/sql/value/serde/ser/primitive/u8.rs new file mode 100644 index 00000000..1d424bce --- /dev/null +++ b/lib/src/sql/value/serde/ser/primitive/u8.rs @@ -0,0 +1,25 @@ +use crate::err::Error; +use crate::sql::value::serde::ser; +use serde::ser::Impossible; + +pub struct Serializer; + +impl ser::Serializer for Serializer { + type Ok = u8; + type Error = Error; + + type SerializeSeq = Impossible; + type SerializeTuple = Impossible; + type SerializeTupleStruct = Impossible; + type SerializeTupleVariant = Impossible; + type SerializeMap = Impossible; + type SerializeStruct = Impossible; + type SerializeStructVariant = Impossible; + + const EXPECTED: &'static str = "a u8"; + + #[inline] + fn serialize_u8(self, value: u8) -> Result { + Ok(value) + } +} diff --git a/lib/src/sql/value/serde/ser/uuid/mod.rs b/lib/src/sql/value/serde/ser/uuid/mod.rs index 04bdccfe..17740201 100644 --- a/lib/src/sql/value/serde/ser/uuid/mod.rs +++ b/lib/src/sql/value/serde/ser/uuid/mod.rs @@ -1,7 +1,9 @@ use crate::err::Error; use crate::sql::value::serde::ser; +use ser::Serializer as _; use serde::ser::Error as _; use serde::ser::Impossible; +use serde::Serialize; use uuid::Uuid; pub(super) struct Serializer; @@ -11,7 +13,7 @@ impl ser::Serializer for Serializer { type Error = Error; type SerializeSeq = Impossible; - type SerializeTuple = Impossible; + type SerializeTuple = SerializeCompactUuidTuple; type SerializeTupleStruct = Impossible; type SerializeTupleVariant = Impossible; type SerializeMap = Impossible; @@ -23,12 +25,39 @@ impl ser::Serializer for Serializer { fn serialize_bytes(self, value: &[u8]) -> Result { Uuid::from_slice(value).map_err(Error::custom) } + + fn serialize_tuple(self, _len: usize) -> Result { + Ok(SerializeCompactUuidTuple::default()) + } +} + +#[derive(Default)] +pub(super) struct SerializeCompactUuidTuple { + index: usize, + bytes: [u8; 16], +} + +impl serde::ser::SerializeTuple for SerializeCompactUuidTuple { + type Ok = Uuid; + type Error = Error; + + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + self.bytes[self.index] = value.serialize(ser::primitive::u8::Serializer.wrap())?; + self.index += 1; + Ok(()) + } + + fn end(self) -> Result { + Uuid::from_slice(&self.bytes).map_err(Error::custom) + } } #[cfg(test)] mod tests { use super::*; - use ser::Serializer as _; use serde::Serialize; #[test]