From dfd11dcdcca1bacf6bfdb1f8e7301aacc177320f Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Sat, 14 Mar 2026 23:52:51 +0000 Subject: [PATCH] refactor: extract ShowFileMutations into show_file_mutations module --- crates/bough-cli/src/main.rs | 33 ++-------- crates/bough-cli/src/show_file_mutations.rs | 71 +++++++++++++++++++++ 2 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 crates/bough-cli/src/show_file_mutations.rs diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index deb8cd71..1f8d03f1 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -2,36 +2,24 @@ mod config; mod render; mod show_all_files; mod show_all_mutations; +mod show_file_mutations; mod show_language_files; mod show_language_mutations; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; -use bough_core::{Mutation, Session, State}; +use bough_core::Session; use bough_typed_hash::TypedHashable; use config::{Command, Show, parse}; use render::{Noop, Render}; use tracing::{Level, debug, error, info, warn}; use crate::render::{ - FileMutations, SingleMutation, + SingleMutation, find_mutation_by_hash, }; -fn collect_states(session: &Session, mutations: Vec) -> Vec { - mutations - .into_iter() - .map(|m| { - let hash = m.hash().expect("hash"); - session - .get_state() - .get(&hash) - .expect("state not found for mutation") - }) - .collect() -} - fn main() { let cli = parse(); @@ -77,20 +65,7 @@ fn main() { lang: Some(lang), file: Some(file), } => { - let mut session = Session::new(cli.config.clone()).expect("session creation"); - session.tend_add_missing_states().expect("tend states"); - let base = session.base(); - let mutations: Vec<_> = base - .mutations() - .collect::, _>>() - .expect("mutation scan") - .into_iter() - .filter(|m| { - m.mutant().lang() == *lang && m.mutant().twig().path() == file.as_path() - }) - .collect(); - let states = collect_states(&session, mutations); - Box::new(FileMutations(*lang, file.clone(), states)) + show_file_mutations::ShowFileMutations::run(cli.config.clone(), *lang, file.clone()) } Show::Mutation { hash } => { diff --git a/crates/bough-cli/src/show_file_mutations.rs b/crates/bough-cli/src/show_file_mutations.rs new file mode 100644 index 00000000..35350b6e --- /dev/null +++ b/crates/bough-cli/src/show_file_mutations.rs @@ -0,0 +1,71 @@ +use std::path::PathBuf; + +use bough_core::{LanguageId, Session, State}; +use bough_typed_hash::TypedHashable; +use facet::Facet; + +use crate::config::Config; +use crate::render::{Render, fmt_mutation_markdown_table, fmt_mutation_terse, fmt_mutation_verbose}; + +#[derive(Facet)] +pub struct ShowFileMutations(pub LanguageId, pub PathBuf, pub Vec); + +impl ShowFileMutations { + pub fn run(config: Config, lang: LanguageId, file: PathBuf) -> Box { + let mut session = Session::new(config).expect("session creation"); + session.tend_add_missing_states().expect("tend states"); + let base = session.base(); + let mutations: Vec<_> = base + .mutations() + .collect::, _>>() + .expect("mutation scan") + .into_iter() + .filter(|m| { + m.mutant().lang() == lang && m.mutant().twig().path() == file.as_path() + }) + .collect(); + let states = mutations + .into_iter() + .map(|m| { + let hash = m.hash().expect("hash"); + session + .get_state() + .get(&hash) + .expect("state not found for mutation") + }) + .collect(); + Box::new(Self(lang, file, states)) + } +} + +impl Render for ShowFileMutations { + fn markdown(&self) -> String { + let mutations: Vec<_> = self.2.iter().map(|s| s.mutation().clone()).collect(); + format!( + "# Mutations in {}\n\n{} total\n\n{}", + self.1.display(), + self.2.len(), + fmt_mutation_markdown_table(&mutations), + ) + } + + fn terse(&self) -> String { + self.2 + .iter() + .map(fmt_mutation_terse) + .collect::>() + .join("\n") + } + + fn verbose(&self) -> String { + self.2 + .iter() + .map(fmt_mutation_verbose) + .collect::>() + .join("\n") + } + + fn json(&self) -> String { + facet_json::to_string(self).unwrap() + } +} -- knotmirror 0.0.1