From 3419b62194932cce901c71b1c7c0475b251d004b Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 23 Jan 2025 20:02:34 +0100 Subject: [PATCH] Strongly type patch review verdict Instead of typing a review verdict as `string | null` we should do `"accept" | "reject" | null` --- .../bindings/cob/patch/Action.ts | 5 +- .../bindings/cob/patch/Review.ts | 3 +- .../bindings/cob/patch/ReviewEdit.ts | 3 +- .../bindings/cob/patch/Verdict.ts | 3 ++ crates/radicle-types/src/cobs/patch.rs | 48 +++++++++++++++---- crates/radicle-types/src/traits/patch.rs | 12 +++-- src/components/PatchTimeline.svelte | 2 +- 7 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 crates/radicle-types/bindings/cob/patch/Verdict.ts diff --git a/crates/radicle-types/bindings/cob/patch/Action.ts b/crates/radicle-types/bindings/cob/patch/Action.ts index 2a4014253..f2d77dff9 100644 --- a/crates/radicle-types/bindings/cob/patch/Action.ts +++ b/crates/radicle-types/bindings/cob/patch/Action.ts @@ -1,6 +1,7 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. import type { CodeLocation } from "../thread/CodeLocation"; import type { Embed } from "../thread/Embed"; +import type { Verdict } from "./Verdict"; export type Action = | { "type": "edit"; title: string; target: string } @@ -12,14 +13,14 @@ export type Action = "type": "review"; revision: string; summary?: string; - verdict?: string; + verdict?: Verdict; labels?: Array; } | { "type": "review.edit"; review: string; summary?: string; - verdict?: string; + verdict?: Verdict; labels?: Array; } | { "type": "review.redact"; review: string } diff --git a/crates/radicle-types/bindings/cob/patch/Review.ts b/crates/radicle-types/bindings/cob/patch/Review.ts index 8c04dac91..6cd1d13a9 100644 --- a/crates/radicle-types/bindings/cob/patch/Review.ts +++ b/crates/radicle-types/bindings/cob/patch/Review.ts @@ -2,11 +2,12 @@ import type { Author } from "../Author"; import type { CodeLocation } from "../thread/CodeLocation"; import type { Comment } from "../thread/Comment"; +import type { Verdict } from "./Verdict"; export type Review = { id: string; author: Author; - verdict?: "accept" | "reject"; + verdict?: Verdict; summary?: string; comments?: Array>; timestamp: number; diff --git a/crates/radicle-types/bindings/cob/patch/ReviewEdit.ts b/crates/radicle-types/bindings/cob/patch/ReviewEdit.ts index 9c9f63e72..35b5f7ef3 100644 --- a/crates/radicle-types/bindings/cob/patch/ReviewEdit.ts +++ b/crates/radicle-types/bindings/cob/patch/ReviewEdit.ts @@ -1,8 +1,9 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Verdict } from "./Verdict"; export type ReviewEdit = { reviewId: string; - verdict?: string; + verdict?: Verdict; summary?: string; labels?: Array; }; diff --git a/crates/radicle-types/bindings/cob/patch/Verdict.ts b/crates/radicle-types/bindings/cob/patch/Verdict.ts new file mode 100644 index 000000000..f88e6d0b4 --- /dev/null +++ b/crates/radicle-types/bindings/cob/patch/Verdict.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type Verdict = "accept" | "reject"; diff --git a/crates/radicle-types/src/cobs/patch.rs b/crates/radicle-types/src/cobs/patch.rs index b76092d6a..129d94460 100644 --- a/crates/radicle-types/src/cobs/patch.rs +++ b/crates/radicle-types/src/cobs/patch.rs @@ -107,8 +107,10 @@ impl From for State { pub struct ReviewEdit { #[ts(as = "String")] pub review_id: cob::patch::ReviewId, - #[ts(as = "Option", optional)] - pub verdict: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub verdict: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] pub summary: Option, #[ts(as = "Option>", optional)] @@ -232,9 +234,10 @@ pub struct Review { #[ts(as = "String")] id: identity::PublicKey, author: cobs::Author, - #[ts(type = "'accept' | 'reject'")] + #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] - verdict: Option, + verdict: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] summary: Option, #[ts(as = "Option<_>", optional)] @@ -252,7 +255,7 @@ impl Review { Self { id, author: cobs::Author::new(&review.author().id, aliases), - verdict: review.verdict(), + verdict: review.verdict().map(|v| v.into()), summary: review.summary().map(|s| s.to_string()), comments: review .comments() @@ -269,6 +272,33 @@ impl Review { } } +#[derive(Debug, Clone, Serialize, Deserialize, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export)] +#[ts(export_to = "cob/patch/")] +pub enum Verdict { + Accept, + Reject, +} + +impl From for Verdict { + fn from(value: cob::patch::Verdict) -> Self { + match value { + cob::patch::Verdict::Accept => Self::Accept, + cob::patch::Verdict::Reject => Self::Reject, + } + } +} + +impl From for cob::patch::Verdict { + fn from(value: Verdict) -> Self { + match value { + Verdict::Accept => Self::Accept, + Verdict::Reject => Self::Reject, + } + } +} + #[derive(Serialize, Deserialize, TS)] #[serde(tag = "type", rename_all = "camelCase")] #[ts(export)] @@ -311,8 +341,8 @@ pub enum Action { #[ts(optional)] summary: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - #[ts(as = "Option", optional)] - verdict: Option, + #[ts(optional)] + verdict: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] #[ts(as = "Option>", optional)] labels: Vec, @@ -325,8 +355,8 @@ pub enum Action { #[ts(optional)] summary: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - #[ts(as = "Option", optional)] - verdict: Option, + #[ts(optional)] + verdict: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] #[ts(as = "Option>", optional)] labels: Vec, diff --git a/crates/radicle-types/src/traits/patch.rs b/crates/radicle-types/src/traits/patch.rs index 6fc6dfe67..0c2b59948 100644 --- a/crates/radicle-types/src/traits/patch.rs +++ b/crates/radicle-types/src/traits/patch.rs @@ -161,7 +161,7 @@ pub trait PatchesMut: Profile { verdict, labels, } => { - patch.review_edit(review, verdict, summary, labels, &signer)?; + patch.review_edit(review, verdict.map(|v| v.into()), summary, labels, &signer)?; } cobs::patch::Action::Review { revision, @@ -169,7 +169,13 @@ pub trait PatchesMut: Profile { verdict, labels, } => { - patch.review(revision, verdict, summary, labels, &signer)?; + patch.review( + revision, + verdict.map(|v| v.into()), + summary, + labels, + &signer, + )?; } cobs::patch::Action::ReviewRedact { review } => { patch.redact_review(review, &signer)?; @@ -329,7 +335,7 @@ pub trait PatchesMut: Profile { let mut patch = patches.get_mut(&cob_id.into())?; patch.review_edit( edit.review_id, - edit.verdict, + edit.verdict.map(|v| v.into()), edit.summary, edit.labels, &signer, diff --git a/src/components/PatchTimeline.svelte b/src/components/PatchTimeline.svelte index 0dfedc9fe..2efadca26 100644 --- a/src/components/PatchTimeline.svelte +++ b/src/components/PatchTimeline.svelte @@ -258,7 +258,7 @@ {formatTimestamp(op.timestamp)} - {:else} + {:else if op.verdict === "reject"}