From d0bdfa64e9380d063dbe01b5b04d69d4863ad3e8 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sun, 8 Mar 2026 22:44:54 +0000 Subject: [PATCH] Implement apply and unapply mutation CLI steps --- crates/bough-cli/src/main.rs | 42 ++++++++++++++++++- crates/bough-cli/src/render.rs | 70 ++++++++++++++++++++++++++++++++ crates/bough-core/src/lib.rs | 2 +- crates/bough-core/src/session.rs | 2 +- 4 files changed, 112 insertions(+), 4 deletions(-) diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 5b4a1a4d..b604eb19 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -204,12 +204,50 @@ fn main() { config::Step::ApplyMutation { workspace_id, mutation_hash, - } => todo!(), + } => { + let session = + Session::new(cli.config.clone()).expect("session creation"); + let wid = bough_core::WorkspaceId::parse(workspace_id) + .expect("invalid workspace id"); + let base = session.base(); + let mutations: Vec<_> = base + .mutations() + .collect::, _>>() + .expect("mutation scan"); + let mutation = + render::find_mutation_by_hash(mutation_hash, mutations); + let mut workspace = session + .bind_workspace(&wid) + .expect("bind workspace"); + workspace + .write_mutant(&mutation) + .expect("apply mutation"); + let hash_str = mutation.hash().expect("hash").to_string(); + Box::new(render::ApplyMutation { + workspace_id: wid, + mutation_hash: hash_str, + }) + } config::Step::UnapplyMutation { workspace_id, mutation_hash, - } => todo!(), + } => { + let session = + Session::new(cli.config.clone()).expect("session creation"); + let wid = bough_core::WorkspaceId::parse(workspace_id) + .expect("invalid workspace id"); + let mut workspace = session + .bind_workspace(&wid) + .expect("bind workspace"); + workspace + .revert_mutant() + .expect("unapply mutation"); + Box::new(render::UnapplyMutation { + workspace_id: wid, + mutation_hash: mutation_hash.clone(), + }) + } config::Step::TestMutation { workspace_id, diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index fe311eae..18a7a6fa 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -633,6 +633,76 @@ impl Render for TestMutation { } } +pub struct ApplyMutation { + pub workspace_id: bough_core::WorkspaceId, + pub mutation_hash: String, +} + +impl Render for ApplyMutation { + fn markdown(&self) -> String { + format!( + "# Apply Mutation\n\n- Workspace: `{}`\n- Mutation: `{}`", + self.workspace_id, self.mutation_hash, + ) + } + + fn terse(&self) -> String { + format!( + "{HASH}{}{RESET} {HASH}{}{RESET}", + self.workspace_id, self.mutation_hash, + ) + } + + fn verbose(&self) -> String { + format!( + "{TITLE}Apply Mutation{RESET} {HASH}{}{RESET} to {HASH}{}{RESET}", + self.mutation_hash, self.workspace_id, + ) + } + + fn json(&self) -> String { + format!( + r#"{{"workspace_id":"{}","mutation_hash":"{}"}}"#, + self.workspace_id, self.mutation_hash, + ) + } +} + +pub struct UnapplyMutation { + pub workspace_id: bough_core::WorkspaceId, + pub mutation_hash: String, +} + +impl Render for UnapplyMutation { + fn markdown(&self) -> String { + format!( + "# Unapply Mutation\n\n- Workspace: `{}`\n- Mutation: `{}`", + self.workspace_id, self.mutation_hash, + ) + } + + fn terse(&self) -> String { + format!( + "{HASH}{}{RESET} {HASH}{}{RESET}", + self.workspace_id, self.mutation_hash, + ) + } + + fn verbose(&self) -> String { + format!( + "{TITLE}Unapply Mutation{RESET} {HASH}{}{RESET} from {HASH}{}{RESET}", + self.mutation_hash, self.workspace_id, + ) + } + + fn json(&self) -> String { + format!( + r#"{{"workspace_id":"{}","mutation_hash":"{}"}}"#, + self.workspace_id, self.mutation_hash, + ) + } +} + impl Render for Config { fn markdown(&self) -> String { format!( diff --git a/crates/bough-core/src/lib.rs b/crates/bough-core/src/lib.rs index 2da73523..1c33a8cd 100644 --- a/crates/bough-core/src/lib.rs +++ b/crates/bough-core/src/lib.rs @@ -41,4 +41,4 @@ pub use session::Config; pub use session::Session; pub use state::State; pub use phase::PhaseOutcome; -pub use workspace::WorkspaceId; +pub use workspace::{Workspace, WorkspaceId}; diff --git a/crates/bough-core/src/session.rs b/crates/bough-core/src/session.rs index ebb169c5..a39a5a2d 100644 --- a/crates/bough-core/src/session.rs +++ b/crates/bough-core/src/session.rs @@ -259,7 +259,7 @@ impl Session { )) } - fn bind_workspace(&self, workspace_id: &WorkspaceId) -> Result, Error> { + pub fn bind_workspace(&self, workspace_id: &WorkspaceId) -> Result, Error> { let workspaces_dir = self.config.get_bough_state_dir().join("workspaces"); Ok(Workspace::bind(workspaces_dir, workspace_id, &self.base)?) } -- knotmirror 0.0.1