From ce42ae1629804b65b75ce9176645d2397590aee4 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Thu, 5 Mar 2026 01:20:30 +0000 Subject: [PATCH] workspace.active: ActiveMutation type with Mutant + Mutation --- crates/bough-core/SPEC.md | 2 +- crates/bough-core/src/mutant/mod.rs | 10 +++--- crates/bough-core/src/mutant/mutation.rs | 2 +- crates/bough-core/src/workspace.rs | 45 ++++++++++++++++++++---- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/crates/bough-core/SPEC.md b/crates/bough-core/SPEC.md index 4d116470..1c90d8bf 100644 --- a/crates/bough-core/SPEC.md +++ b/crates/bough-core/SPEC.md @@ -91,7 +91,7 @@ core[workspace.bind.validate-unchanged] Workspace::validate_unchanged() is called after bind to ensure it has not changed core[workspace.bind.validate-unchanged.set_active] -if Workspace::validate_unchanged() finds 1 single active Mutation, it should be set as the Workspace's active Mutation +if Workspace::validate_unchanged() finds 1 single active Mutation, bind should set it as the Workspace's active Mutation core[workspace.validate-unchanged] Workspace::validate_unchanged() checks that list and file contents of Base::files and Workspace::files are identicall diff --git a/crates/bough-core/src/mutant/mod.rs b/crates/bough-core/src/mutant/mod.rs index 5850e8d3..34e6f463 100644 --- a/crates/bough-core/src/mutant/mod.rs +++ b/crates/bough-core/src/mutant/mod.rs @@ -23,7 +23,7 @@ pub struct MutantHash([u8; 32]); // core[impl mutant.twig] // core[impl mutant.kind] // core[impl mutant.span] -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Mutant<'a> { lang: LanguageId, base: &'a Base, @@ -81,7 +81,7 @@ impl TypedHashable for Mutant<'_> { } // core[impl span.point] -#[derive(Clone, bough_typed_hash::HashInto)] +#[derive(Debug, Clone, PartialEq, bough_typed_hash::HashInto)] pub struct Span { start: Point, end: Point, @@ -104,7 +104,7 @@ impl Span { // core[impl point.line] // core[impl point.col] // core[impl point.byte] -#[derive(Clone, bough_typed_hash::HashInto)] +#[derive(Debug, Clone, PartialEq, bough_typed_hash::HashInto)] pub struct Point { line: usize, col: usize, @@ -129,13 +129,13 @@ impl Point { } } -#[derive(Clone, bough_typed_hash::HashInto)] +#[derive(Debug, Clone, PartialEq, bough_typed_hash::HashInto)] pub enum BinaryOpMutationKind { Add, Sub, } -#[derive(Clone, bough_typed_hash::HashInto)] +#[derive(Debug, Clone, PartialEq, bough_typed_hash::HashInto)] pub enum MutantKind { StatementBlock, Condition, diff --git a/crates/bough-core/src/mutant/mutation.rs b/crates/bough-core/src/mutant/mutation.rs index 637e03e3..4adb62a3 100644 --- a/crates/bough-core/src/mutant/mutation.rs +++ b/crates/bough-core/src/mutant/mutation.rs @@ -37,7 +37,7 @@ impl<'a> Iterator for MutationIter<'a> { // core[impl mutation.mutant] // core[impl mutation.subst] -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Mutation<'a> { pub(crate) mutant: &'a Mutant<'a>, pub(crate) subst: String, diff --git a/crates/bough-core/src/workspace.rs b/crates/bough-core/src/workspace.rs index 637fccbc..e316850d 100644 --- a/crates/bough-core/src/workspace.rs +++ b/crates/bough-core/src/workspace.rs @@ -1,8 +1,26 @@ use crate::base::Base; -use crate::file::{File, FilesIter, Root, Twig}; +use crate::file::{File, FilesIter, Root}; +use crate::mutant::Mutant; use crate::mutant::mutation::Mutation; use std::path::{Path, PathBuf}; +// core[impl workspace.active] +#[derive(Debug, Clone, PartialEq)] +pub struct ActiveMutation<'a> { + mutant: Mutant<'a>, + mutation: Mutation<'a>, +} + +impl<'a> ActiveMutation<'a> { + pub fn mutant(&self) -> &Mutant<'a> { + &self.mutant + } + + pub fn mutation(&self) -> &Mutation<'a> { + &self.mutation + } +} + #[derive(Debug)] pub enum Error { File(crate::file::Error), @@ -85,7 +103,7 @@ pub struct Workspace<'a> { id: WorkspaceId, root: PathBuf, base: &'a Base, - active: Option, + active: Option>, } impl<'a> Workspace<'a> { @@ -141,7 +159,7 @@ impl<'a> Workspace<'a> { // core[impl workspace.write_mutant] // core[impl workspace.write_mutant.set-active] // core[impl workspace.write_mutant.set-active.only-one] - pub fn write_mutant(&mut self, mutation: &Mutation<'_>) -> Result<(), Error> { + pub fn write_mutant(&mut self, mutation: &Mutation<'a>) -> Result<(), Error> { if self.active.is_some() { return Err(Error::AlreadyActive); } @@ -157,21 +175,25 @@ impl<'a> Workspace<'a> { mutated.extend_from_slice(&content[end..]); let ws_file = self.root.join(mutant.twig().path()); std::fs::write(&ws_file, &mutated)?; - self.active = Some(mutant.twig().clone()); + self.active = Some(ActiveMutation { + mutant: mutation.mutant().clone(), + mutation: mutation.clone(), + }); Ok(()) } // core[impl workspace.revert_mutant] // core[impl workspace.revert_mutant.active] pub fn revert_mutant(&mut self) -> Result<(), Error> { - let twig = self.active.take().ok_or(Error::NotActive)?; - let src = File::new(self.base, &twig).resolve(); + let active = self.active.take().ok_or(Error::NotActive)?; + let twig = active.mutant().twig(); + let src = File::new(self.base, twig).resolve(); let dst = self.root.join(twig.path()); std::fs::copy(&src, &dst)?; Ok(()) } - pub fn active(&self) -> Option<&Twig> { + pub fn active(&self) -> Option<&ActiveMutation<'a>> { self.active.as_ref() } @@ -481,6 +503,15 @@ mod tests { assert!(result.is_err()); } + // core[verify workspace.active] + #[test] + fn workspace_active_is_none_initially() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + assert!(ws.active().is_none()); + } + // core[verify workspace.write_mutant.set-active] #[test] fn write_mutant_sets_active() { -- knotmirror 0.0.1