From 109a679e756d3a5b2dd95e88a32c856743543e16 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sat, 7 Mar 2026 22:13:30 +0000 Subject: [PATCH] Show all mutations (show mutations) --- Cargo.lock | 1 + crates/bough-cli/Cargo.toml | 1 + crates/bough-cli/src/main.rs | 18 +++++++--- crates/bough-cli/src/render.rs | 62 +++++++++++++++++++++++++++++++++- crates/bough-core/src/lib.rs | 1 + 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b5b5ce8..f9e32fcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,6 +113,7 @@ name = "bough-cli" version = "0.0.0" dependencies = [ "bough-core", + "bough-typed-hash", "facet", "facet-json", "facet-toml", diff --git a/crates/bough-cli/Cargo.toml b/crates/bough-cli/Cargo.toml index 42991c83..0b26f109 100644 --- a/crates/bough-cli/Cargo.toml +++ b/crates/bough-cli/Cargo.toml @@ -6,6 +6,7 @@ license.workspace = true [dependencies] bough-core = { path = "../bough-core" } +bough-typed-hash = { path = "../bough-typed-hash" } facet = "0.44" facet-json = "0.44" miette = { version = "7", features = ["fancy"] } diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 1114934d..1e3dedf7 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -6,7 +6,7 @@ use config::{Command, Show, parse}; use render::{Noop, Render}; use tracing::{Level, debug, info}; -use crate::render::{BaseFiles, MutantFiles}; +use crate::render::{AllMutations, BaseFiles, MutantFiles}; fn main() { let cli = parse(); @@ -59,16 +59,24 @@ fn main() { Show::Mutations { lang: None, file: _, - } => todo!(), + } => { + let session = Session::new(cli.config.clone()).expect("session creation"); + let base = session.base(); + let mutations: Vec<_> = base + .mutations() + .collect::, _>>() + .expect("mutation scan"); + Box::new(AllMutations(mutations)) + } Show::Mutations { - lang: Some(lang), + lang: Some(_lang), file: None, } => todo!(), Show::Mutations { - lang: Some(lang), - file: Some(file), + lang: Some(_lang), + file: Some(_file), } => todo!(), Show::Mutation { hash } => todo!(), diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index dc636ff5..5cb7f934 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; -use bough_core::LanguageId; +use bough_core::{LanguageId, Mutation}; +use bough_typed_hash::{MemoryHashStore, TypedHashable}; use facet::Facet; use crate::config::{Config, Format}; @@ -107,6 +108,65 @@ impl Render for MutantFiles { } } +fn mutation_hash(m: &Mutation) -> String { + let mut store = MemoryHashStore::new(); + let hash = m.hash(&mut store).expect("hashing should not fail"); + format!("{hash}") +} + +fn fmt_mutation_terse(m: &Mutation) -> String { + let mutant = m.mutant(); + format!( + "{} {}:{}:{} {:?} → {}", + mutation_hash(m), + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + ) +} + +fn fmt_mutation_verbose(m: &Mutation) -> String { + let mutant = m.mutant(); + format!( + "{} {} [{:?}] {:?} @ {}:{}-{}:{} → \"{}\"", + mutation_hash(m), + mutant.twig().path().display(), + mutant.lang(), + mutant.kind(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.span().end().line() + 1, + mutant.span().end().col() + 1, + m.subst(), + ) +} + +#[derive(Facet)] +pub struct AllMutations(pub Vec); +impl Render for AllMutations { + fn markdown(&self) -> String { + format!( + "# All Mutations\n\n{} total\n\n{}", + self.0.len(), + self.0.iter().map(|m| format!("- {}", fmt_mutation_verbose(m))).collect::>().join("\n") + ) + } + + fn terse(&self) -> String { + self.0.iter().map(fmt_mutation_terse).collect::>().join("\n") + } + + fn verbose(&self) -> String { + self.0.iter().map(fmt_mutation_verbose).collect::>().join("\n") + } + + fn json(&self) -> String { + facet_json::to_string(self).unwrap() + } +} + 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 b8b3e72c..9559795a 100644 --- a/crates/bough-core/src/lib.rs +++ b/crates/bough-core/src/lib.rs @@ -35,6 +35,7 @@ pub enum LanguageId { } pub use file::File; +pub use mutation::Mutation; pub use mutation::MutationHash; pub use session::Config; pub use session::Session; -- knotmirror 0.0.1