From 79c01e8786157d17088497fc4ee141c80868cfbb Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sat, 14 Mar 2026 23:59:27 +0000 Subject: [PATCH] refactor: extract FindBestMutations into find_best_mutations module --- crates/bough-cli/src/find_best_mutations.rs | 119 ++++++++++++++++++++ crates/bough-cli/src/main.rs | 21 +--- 2 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 crates/bough-cli/src/find_best_mutations.rs diff --git a/crates/bough-cli/src/find_best_mutations.rs b/crates/bough-cli/src/find_best_mutations.rs new file mode 100644 index 00000000..1da05464 --- /dev/null +++ b/crates/bough-cli/src/find_best_mutations.rs @@ -0,0 +1,119 @@ +use std::path::PathBuf; + +use bough_core::{LanguageId, MutationHash, Session, State}; + +use crate::config::Config; +use crate::render::{ + HASH, NOT_RUN, PATH, RESET, STRING, TITLE, Render, fmt_status_colored, mutation_hash, +}; + +pub struct FindBestMutations(pub Vec<(MutationHash, State, f64)>); + +impl FindBestMutations { + pub fn run(config: Config, lang: Option, file: Option) -> Box { + let session = Session::new(config).expect("session creation"); + let results = session.find_best_mutations().expect("find best mutations"); + let filtered: Vec<_> = results + .into_iter() + .filter(|(_, state, _)| { + if let Some(l) = lang { + if state.mutation().mutant().lang() != l { + return false; + } + } + if let Some(ref f) = file { + if state.mutation().mutant().twig().path() != f.as_path() { + return false; + } + } + true + }) + .collect(); + Box::new(Self(filtered)) + } +} + +impl Render for FindBestMutations { + fn markdown(&self) -> String { + let mut out = format!("# Find Best Mutations\n\n{} selected\n\n", self.0.len()); + out.push_str("| # | Score | Hash | File | Location | Kind | Subst |\n"); + out.push_str("|---|-------|------|------|----------|------|-------|\n"); + for (i, (hash, state, score)) in self.0.iter().enumerate() { + let m = state.mutation(); + let mutant = m.mutant(); + out.push_str(&format!( + "| {} | {:.2} | `{}` | {} | {}:{} | {:?} | `{}` |\n", + i + 1, + score, + hash, + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + )); + } + out + } + + fn terse(&self) -> String { + self.0 + .iter() + .map(|(_, state, score)| { + let m = state.mutation(); + let mutant = m.mutant(); + format!( + "{HASH}{}{RESET} {:.2} {} {PATH}{}:{}:{}{RESET} {:?} → {STRING}{}{RESET}", + mutation_hash(m), + score, + fmt_status_colored(state), + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + ) + }) + .collect::>() + .join("\n") + } + + fn verbose(&self) -> String { + let mut out = format!("{TITLE}Find Best Mutations{RESET} ({} selected)\n", self.0.len()); + for (i, (hash, state, score)) in self.0.iter().enumerate() { + let m = state.mutation(); + let mutant = m.mutant(); + out.push_str(&format!( + "\n {HASH}#{}{RESET} score={:.2} {HASH}{}{RESET} {} {PATH}{}:{}:{}{RESET} {:?} → {STRING}{}{RESET}", + i + 1, + score, + hash, + fmt_status_colored(state), + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + )); + } + out + } + + fn json(&self) -> String { + let items: Vec = self.0.iter().map(|(hash, state, score)| { + let m = state.mutation(); + let mutant = m.mutant(); + format!( + r#"{{"hash":"{}","score":{},"file":"{}","line":{},"col":{},"kind":"{}","subst":"{}"}}"#, + hash, + score, + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + format!("{:?}", mutant.kind()), + m.subst(), + ) + }).collect(); + format!("[{}]", items.join(",")) + } +} diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 443c46a9..d9dd7a74 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -1,4 +1,5 @@ mod config; +mod find_best_mutations; mod render; mod show_all_files; mod show_all_mutations; @@ -362,25 +363,7 @@ fn main() { } Command::Find { ref lang, ref file } => { - let session = Session::new(cli.config.clone()).expect("session creation"); - let results = session.find_best_mutations().expect("find best mutations"); - let filtered: Vec<_> = results - .into_iter() - .filter(|(_, state, _)| { - if let Some(l) = lang { - if state.mutation().mutant().lang() != *l { - return false; - } - } - if let Some(f) = file { - if state.mutation().mutant().twig().path() != f.as_path() { - return false; - } - } - true - }) - .collect(); - Box::new(render::FindBestMutations(filtered)) + find_best_mutations::FindBestMutations::run(cli.config.clone(), *lang, file.clone()) } Command::Noop => { -- knotmirror 0.0.1