cob: Make json serialization consistent

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-12-02 15:06:37 +01:00
parent f9c81ab382
commit ca69fdc4fa
No known key found for this signature in database
4 changed files with 24 additions and 8 deletions

View File

@ -36,7 +36,7 @@ pub enum Error {
/// Reason why an issue was closed. /// Reason why an issue was closed.
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "camelCase")]
pub enum CloseReason { pub enum CloseReason {
Other, Other,
Solved, Solved,
@ -44,7 +44,7 @@ pub enum CloseReason {
/// Issue state. /// Issue state.
#[derive(Debug, Default, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)] #[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 { pub enum Status {
/// The issue is closed. /// The issue is closed.
Closed { reason: CloseReason }, Closed { reason: CloseReason },
@ -74,7 +74,6 @@ impl Status {
/// Issue state. Accumulates [`Action`]. /// Issue state. Accumulates [`Action`].
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Issue { pub struct Issue {
// TODO(cloudhead): Title should bias towards shorter strings.
title: LWWReg<Max<String>, clock::Lamport>, title: LWWReg<Max<String>, clock::Lamport>,
status: LWWReg<Max<Status>, clock::Lamport>, status: LWWReg<Max<Status>, clock::Lamport>,
tags: LWWSet<Tag>, tags: LWWSet<Tag>,
@ -382,6 +381,7 @@ impl<'a> Issues<'a> {
/// Issue operation. /// Issue operation.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum Action { pub enum Action {
Title { title: String }, Title { title: String },
Lifecycle { status: Status }, Lifecycle { status: Status },

View File

@ -1,6 +1,5 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
use radicle_cob::history::EntryWithClock; 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`. /// Everything that can be done in the system is represented by an `Op`.
/// Operations are applied to an accumulator to yield a final state. /// 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<A> { pub struct Op<A> {
/// The action carried out by this operation. /// The action carried out by this operation.
pub action: A, pub action: A,
@ -67,7 +66,7 @@ pub struct Actor<G, A> {
pub ops: BTreeMap<(Lamport, PublicKey), Op<A>>, pub ops: BTreeMap<(Lamport, PublicKey), Op<A>>,
} }
impl<G: Signer, A: Clone + Serialize> Actor<G, A> { impl<G: Signer, A: Clone> Actor<G, A> {
pub fn new(signer: G) -> Self { pub fn new(signer: G) -> Self {
Self { Self {
signer, signer,

View File

@ -66,6 +66,7 @@ pub enum Error {
/// Patch operation. /// Patch operation.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum Action { pub enum Action {
Edit { Edit {
title: String, title: String,
@ -379,7 +380,7 @@ impl Revision {
} }
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "camelCase")]
pub enum Status { pub enum Status {
#[default] #[default]
Proposed, Proposed,
@ -389,6 +390,7 @@ pub enum Status {
/// A merged patch revision. /// A merged patch revision.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct Merge { pub struct Merge {
/// Owner of repository that this patch was merged into. /// Owner of repository that this patch was merged into.
pub node: NodeId, pub node: NodeId,
@ -400,7 +402,7 @@ pub struct Merge {
/// A patch review verdict. /// A patch review verdict.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "camelCase")]
pub enum Verdict { pub enum Verdict {
/// Accept patch. /// Accept patch.
Accept, Accept,
@ -427,6 +429,7 @@ impl fmt::Display for Verdict {
/// Code location, used for attaching comments. /// Code location, used for attaching comments.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeLocation { pub struct CodeLocation {
/// File being commented on. /// File being commented on.
pub blob: git::Oid, pub blob: git::Oid,
@ -455,6 +458,7 @@ impl Ord for CodeLocation {
/// Comment on code. /// Comment on code.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CodeComment { pub struct CodeComment {
/// Code location of the comment. /// Code location of the comment.
pub location: CodeLocation, pub location: CodeLocation,
@ -933,6 +937,18 @@ mod test {
.quickcheck(property as fn(Changes<3>) -> TestResult); .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] #[test]
fn test_patch_create_and_get() { fn test_patch_create_and_get() {
let tmp = tempfile::tempdir().unwrap(); let tmp = tempfile::tempdir().unwrap();

View File

@ -60,6 +60,7 @@ impl PartialOrd for Comment {
/// An action that can be carried out in a change. /// An action that can be carried out in a change.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum Action { pub enum Action {
/// Comment on a thread. /// Comment on a thread.
Comment { Comment {