Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Use string format for `OpId` JSON
Alexis Sellier committed 3 years ago
commit 88ffc02c5e84225f19424f8f59b108d8cf459fd0
parent 1cb89868305760a2c8ee536a5e34efeedcbe2c7b
1 file changed +29 -7
modified radicle/src/cob/op.rs
@@ -14,6 +14,7 @@ use radicle_crypto::{PublicKey, Signer};

/// Identifies an [`Op`] internally and within the change graph.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
+
#[serde(into = "String", try_from = "String")]
pub struct OpId(Lamport, ActorId);

impl OpId {
@@ -43,6 +44,22 @@ impl fmt::Display for OpId {
    }
}

+
// Used by `serde::Serialize`.
+
impl From<OpId> for String {
+
    fn from(value: OpId) -> Self {
+
        value.to_string()
+
    }
+
}
+

+
// Used by `serde::Deserialize`.
+
impl TryFrom<String> for OpId {
+
    type Error = OpIdError;
+

+
    fn try_from(value: String) -> Result<Self, Self::Error> {
+
        value.as_str().try_into()
+
    }
+
}
+

/// Error decoding an operation from an entry.
#[derive(Error, Debug)]
pub enum OpIdError {
@@ -233,14 +250,19 @@ mod test {

    #[test]
    fn test_opid_try_from_str() {
-
        let str = "z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT/12";
-
        let id = OpId::try_from(str).expect("Op ID parses string");
-
        assert_eq!(str, id.to_string(), "string conversion is consistent");
+
        let s = "z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT/12";
+
        let id = OpId::try_from(s).expect("Op ID parses string");
+
        assert_eq!(s, id.to_string(), "string conversion is consistent");
+

+
        let s = "";
+
        assert!(OpId::try_from(s).is_err(), "empty strings are invalid");

-
        let str = "";
-
        assert!(OpId::try_from(str).is_err(), "empty strings are invalid");
+
        let s = "jlkjfksgi";
+
        assert!(OpId::try_from(s).is_err(), "badly formatted string");

-
        let str = "jlkjfksgi";
-
        assert!(OpId::try_from(str).is_err(), "badly formatted string");
+
        assert_eq!(
+
            serde_json::from_str::<OpId>(serde_json::to_string(&id).unwrap().as_str()).unwrap(),
+
            id
+
        );
    }
}