From 70f0cc354dee34fc5efaa593a4166872f4089097 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Thu, 28 Nov 2024 15:19:56 +0100 Subject: [PATCH] cob: Fix `serde` instances for `ObjectId` The type now serializes the same as `Oid`. --- radicle-cob/src/object.rs | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/radicle-cob/src/object.rs b/radicle-cob/src/object.rs index 69991338..f368ed9d 100644 --- a/radicle-cob/src/object.rs +++ b/radicle-cob/src/object.rs @@ -23,7 +23,8 @@ pub enum ParseObjectId { } /// The id of an object -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[serde(transparent)] pub struct ObjectId(Oid); impl FromStr for ObjectId { @@ -73,26 +74,6 @@ impl fmt::Display for ObjectId { } } -impl Serialize for ObjectId { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_bytes(self.0.as_bytes()) - } -} - -impl<'de> Deserialize<'de> for ObjectId { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let raw = <&[u8]>::deserialize(deserializer)?; - let oid = Oid::try_from(raw).map_err(serde::de::Error::custom)?; - Ok(ObjectId(oid)) - } -} - impl From<&ObjectId> for Component<'_> { fn from(id: &ObjectId) -> Self { let refstr = RefString::from(*id); @@ -108,3 +89,19 @@ impl From for RefString { .expect("collaborative object id's are valid ref strings") } } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn test_serde() { + let id = ObjectId::from_str("3ad84420bd882f983c2f9b605e7a68f5bdf95f5c").unwrap(); + + assert_eq!( + serde_json::to_string(&id).unwrap(), + serde_json::to_string(&id.0).unwrap() + ); + } +}