Use string format for `OpId` JSON
This commit is contained in:
parent
1cb8986830
commit
88ffc02c5e
|
|
@ -14,6 +14,7 @@ use radicle_crypto::{PublicKey, Signer};
|
||||||
|
|
||||||
/// Identifies an [`Op`] internally and within the change graph.
|
/// Identifies an [`Op`] internally and within the change graph.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||||
|
#[serde(into = "String", try_from = "String")]
|
||||||
pub struct OpId(Lamport, ActorId);
|
pub struct OpId(Lamport, ActorId);
|
||||||
|
|
||||||
impl OpId {
|
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.
|
/// Error decoding an operation from an entry.
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum OpIdError {
|
pub enum OpIdError {
|
||||||
|
|
@ -233,14 +250,19 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opid_try_from_str() {
|
fn test_opid_try_from_str() {
|
||||||
let str = "z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT/12";
|
let s = "z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT/12";
|
||||||
let id = OpId::try_from(str).expect("Op ID parses string");
|
let id = OpId::try_from(s).expect("Op ID parses string");
|
||||||
assert_eq!(str, id.to_string(), "string conversion is consistent");
|
assert_eq!(s, id.to_string(), "string conversion is consistent");
|
||||||
|
|
||||||
let str = "";
|
let s = "";
|
||||||
assert!(OpId::try_from(str).is_err(), "empty strings are invalid");
|
assert!(OpId::try_from(s).is_err(), "empty strings are invalid");
|
||||||
|
|
||||||
let str = "jlkjfksgi";
|
let s = "jlkjfksgi";
|
||||||
assert!(OpId::try_from(str).is_err(), "badly formatted string");
|
assert!(OpId::try_from(s).is_err(), "badly formatted string");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::from_str::<OpId>(serde_json::to_string(&id).unwrap().as_str()).unwrap(),
|
||||||
|
id
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue