From 08f6f5d06ba984437a2974cd3245a0674b8ec9f5 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Fri, 20 Dec 2024 17:20:39 +0100 Subject: [PATCH] refactor(patch): Introduce review mode (create / resume) --- bin/commands/patch.rs | 9 ++++ bin/commands/patch/review.rs | 79 ++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/bin/commands/patch.rs b/bin/commands/patch.rs index 0fc0b233..d480f6e6 100644 --- a/bin/commands/patch.rs +++ b/bin/commands/patch.rs @@ -326,6 +326,7 @@ mod interface { use crate::tui_patch::list; use crate::tui_patch::review::builder::CommentBuilder; use crate::tui_patch::review::ReviewAction; + use crate::tui_patch::review::ReviewMode; use super::review; use super::review::builder::ReviewBuilder; @@ -368,6 +369,7 @@ mod interface { let drafts = DraftStore::new(&repo, *signer.public_key()); let mut patches = cob::patch::Cache::no_cache(&drafts)?; let mut patch = patches.get_mut(&patch_id)?; + let mut resume = false; if let Some(review) = revision.review_by(signer.public_key()) { // Review already finalized. Do nothing and warn. @@ -381,6 +383,7 @@ mod interface { if let Some((id, _)) = patch::find_review(&patch, revision, &signer) { // Review already started, resume. + resume = true; log::info!("Resuming review {id}.."); } else { // No review to resume, start a new one. @@ -404,7 +407,13 @@ mod interface { let (review_id, review) = patch::find_review(&patch, revision, &signer) .ok_or_else(|| anyhow!("Could not find review."))?; + let mode = if resume { + ReviewMode::Resume + } else { + ReviewMode::Create + }; let selection = review::Tui::new( + mode, profile.storage.clone(), rid, patch_id, diff --git a/bin/commands/patch/review.rs b/bin/commands/patch/review.rs index 6ed7edc9..c7e855a5 100644 --- a/bin/commands/patch/review.rs +++ b/bin/commands/patch/review.rs @@ -5,6 +5,7 @@ use std::fmt::Debug; use std::sync::Arc; use std::sync::Mutex; +use anyhow::bail; use anyhow::Result; use termion::event::Key; @@ -52,7 +53,13 @@ pub struct Selection { pub args: Option, } +pub enum ReviewMode { + Create, + Resume, +} + pub struct Tui { + pub mode: ReviewMode, pub storage: Storage, pub rid: RepoId, pub patch: PatchId, @@ -63,7 +70,9 @@ pub struct Tui { } impl Tui { + #[allow(clippy::too_many_arguments)] pub fn new( + mode: ReviewMode, storage: Storage, rid: RepoId, patch: PatchId, @@ -73,6 +82,7 @@ impl Tui { hunks: Hunks, ) -> Self { Self { + mode, storage, rid, patch, @@ -88,6 +98,7 @@ impl Tui { let channel = Channel::default(); let state = App::new( + self.mode, self.storage, self.rid, self.patch, @@ -132,6 +143,8 @@ pub struct AppState { _rid: RepoId, /// Patch this review belongs to. patch: PatchId, + /// Patch title. + title: String, /// Revision this review belongs to. _revision: Revision, /// Current app page. @@ -151,6 +164,7 @@ impl AppState { pub fn new( rid: RepoId, patch: PatchId, + title: String, revision: Revision, page: AppPage, panes: PanesState, @@ -161,6 +175,7 @@ impl AppState { Self { _rid: rid, patch, + title, _revision: revision, page, panes, @@ -207,33 +222,16 @@ impl AppState { #[derive(Clone)] pub struct App<'a> { - /// Patch title/ - title: String, /// All hunks. hunks: Arc>>>, /// The app state. state: AppState, } -impl<'a> TryFrom for App<'a> { - type Error = anyhow::Error; - - fn try_from(tui: Tui) -> Result { - App::new( - tui.storage, - tui.rid, - tui.patch, - tui.title, - tui.revision, - tui.review, - tui.hunks, - ) - } -} - impl<'a> App<'a> { #[allow(clippy::too_many_arguments)] pub fn new( + mode: ReviewMode, storage: Storage, rid: RepoId, patch: PatchId, @@ -253,24 +251,34 @@ impl<'a> App<'a> { .iter() .map(|item| HunkItem::from((&repo, &review, item))) .collect::>(); - let states = hunks - .iter() - .map(|_| HunkState::Rejected) - .collect::>(); + + let state = match mode { + ReviewMode::Create => { + let states = hunks + .iter() + .map(|_| HunkState::Rejected) + .collect::>(); + + AppState::new( + rid, + patch, + title, + revision, + AppPage::Main, + PanesState::new(2, Some(0)), + (TableState::new(Some(0)), states), + views, + TextViewState::new(Position::default()), + ) + } + ReviewMode::Resume => { + bail!("Resuming a review is not yet implemented."); + } + }; Ok(Self { - title, hunks: Arc::new(Mutex::new(hunks)), - state: AppState::new( - rid, - patch, - revision, - AppPage::Main, - PanesState::new(2, Some(0)), - (TableState::new(Some(0)), states), - views, - TextViewState::new(Position::default()), - ), + state, }) } @@ -367,7 +375,7 @@ impl<'a> App<'a> { let hunks = &self.hunks.lock().unwrap(); let id = format!(" {} ", format::cob(&self.state.patch)); - let title = &self.title; + let title = &self.state.title; let hunks_total = hunks.len(); let hunks_accepted = self @@ -629,7 +637,7 @@ mod test { use crate::test::setup::NodeWithRepo; use super::builder::ReviewBuilder; - use super::App; + use super::{App, ReviewMode}; pub fn app<'a>( node: &NodeWithRepo, @@ -645,6 +653,7 @@ mod test { let hunks = ReviewBuilder::new(&node.repo).hunks(revision)?; App::new( + ReviewMode::Create, node.storage.clone(), node.repo.id, *patch.id(),