From ca69fdc4fa532aef074b62fb9a765e38d1db298b Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 2 Dec 2022 15:06:37 +0100 Subject: [PATCH] cob: Make json serialization consistent Signed-off-by: Alexis Sellier --- radicle/src/cob/issue.rs | 6 +++--- radicle/src/cob/op.rs | 5 ++--- radicle/src/cob/patch.rs | 20 ++++++++++++++++++-- radicle/src/cob/thread.rs | 1 + 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/radicle/src/cob/issue.rs b/radicle/src/cob/issue.rs index e5a25c3c..665b29a0 100644 --- a/radicle/src/cob/issue.rs +++ b/radicle/src/cob/issue.rs @@ -36,7 +36,7 @@ pub enum Error { /// Reason why an issue was closed. #[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "camelCase")] pub enum CloseReason { Other, Solved, @@ -44,7 +44,7 @@ pub enum CloseReason { /// Issue state. #[derive(Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] -#[serde(rename_all = "lowercase", tag = "status")] +#[serde(rename_all = "camelCase", tag = "status")] pub enum Status { /// The issue is closed. Closed { reason: CloseReason }, @@ -74,7 +74,6 @@ impl Status { /// Issue state. Accumulates [`Action`]. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Issue { - // TODO(cloudhead): Title should bias towards shorter strings. title: LWWReg, clock::Lamport>, status: LWWReg, clock::Lamport>, tags: LWWSet, @@ -382,6 +381,7 @@ impl<'a> Issues<'a> { /// Issue operation. #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(tag = "type", rename_all = "camelCase")] pub enum Action { Title { title: String }, Lifecycle { status: Status }, diff --git a/radicle/src/cob/op.rs b/radicle/src/cob/op.rs index ce247123..00e90177 100644 --- a/radicle/src/cob/op.rs +++ b/radicle/src/cob/op.rs @@ -1,6 +1,5 @@ use std::collections::BTreeMap; -use serde::{Deserialize, Serialize}; use thiserror::Error; use radicle_cob::history::EntryWithClock; @@ -24,7 +23,7 @@ pub enum OpDecodeError { /// /// Everything that can be done in the system is represented by an `Op`. /// Operations are applied to an accumulator to yield a final state. -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Op { /// The action carried out by this operation. pub action: A, @@ -67,7 +66,7 @@ pub struct Actor { pub ops: BTreeMap<(Lamport, PublicKey), Op>, } -impl Actor { +impl Actor { pub fn new(signer: G) -> Self { Self { signer, diff --git a/radicle/src/cob/patch.rs b/radicle/src/cob/patch.rs index 92cdac44..a44f3c26 100644 --- a/radicle/src/cob/patch.rs +++ b/radicle/src/cob/patch.rs @@ -66,6 +66,7 @@ pub enum Error { /// Patch operation. #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(tag = "type", rename_all = "camelCase")] pub enum Action { Edit { title: String, @@ -379,7 +380,7 @@ impl Revision { } #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "camelCase")] pub enum Status { #[default] Proposed, @@ -389,6 +390,7 @@ pub enum Status { /// A merged patch revision. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +#[serde(rename_all = "camelCase")] pub struct Merge { /// Owner of repository that this patch was merged into. pub node: NodeId, @@ -400,7 +402,7 @@ pub struct Merge { /// A patch review verdict. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "camelCase")] pub enum Verdict { /// Accept patch. Accept, @@ -427,6 +429,7 @@ impl fmt::Display for Verdict { /// Code location, used for attaching comments. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct CodeLocation { /// File being commented on. pub blob: git::Oid, @@ -455,6 +458,7 @@ impl Ord for CodeLocation { /// Comment on code. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct CodeComment { /// Code location of the comment. pub location: CodeLocation, @@ -933,6 +937,18 @@ mod test { .quickcheck(property as fn(Changes<3>) -> TestResult); } + #[test] + fn test_json_serialization() { + let edit = Action::Tag { + add: vec![], + remove: vec![], + }; + assert_eq!( + serde_json::to_string(&edit).unwrap(), + String::from(r#"{"type":"tag","add":[],"remove":[]}"#) + ); + } + #[test] fn test_patch_create_and_get() { let tmp = tempfile::tempdir().unwrap(); diff --git a/radicle/src/cob/thread.rs b/radicle/src/cob/thread.rs index 53f63588..c91be571 100644 --- a/radicle/src/cob/thread.rs +++ b/radicle/src/cob/thread.rs @@ -60,6 +60,7 @@ impl PartialOrd for Comment { /// An action that can be carried out in a change. #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(tag = "type", rename_all = "camelCase")] pub enum Action { /// Comment on a thread. Comment {