Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cob: Fix `serde` instances for `ObjectId`
cloudhead committed 1 year ago
commit 70f0cc354dee34fc5efaa593a4166872f4089097
parent f58af8fef4831d2096ce3a083e1d33f1988b2eb2
1 file changed +18 -21
modified 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
-
    where
-
        S: serde::Serializer,
-
    {
-
        serializer.serialize_bytes(self.0.as_bytes())
-
    }
-
}
-

-
impl<'de> Deserialize<'de> for ObjectId {
-
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
-
    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<ObjectId> 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()
+
        );
+
    }
+
}