diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index 52192e1e..deb8cd71 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -3,6 +3,7 @@ mod render; mod show_all_files; mod show_all_mutations; mod show_language_files; +mod show_language_mutations; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; @@ -14,7 +15,7 @@ use render::{Noop, Render}; use tracing::{Level, debug, error, info, warn}; use crate::render::{ - FileMutations, LangMutations, SingleMutation, + FileMutations, SingleMutation, find_mutation_by_hash, }; @@ -69,18 +70,7 @@ fn main() { lang: Some(lang), file: None, } => { - 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) - .collect(); - let states = collect_states(&session, mutations); - Box::new(LangMutations(*lang, states)) + show_language_mutations::ShowLanguageMutations::run(cli.config.clone(), *lang) } Show::Mutations { diff --git a/crates/bough-cli/src/show_language_mutations.rs b/crates/bough-cli/src/show_language_mutations.rs new file mode 100644 index 00000000..c683315a --- /dev/null +++ b/crates/bough-cli/src/show_language_mutations.rs @@ -0,0 +1,67 @@ +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 ShowLanguageMutations(pub LanguageId, pub Vec); + +impl ShowLanguageMutations { + pub fn run(config: Config, lang: LanguageId) -> 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) + .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, states)) + } +} + +impl Render for ShowLanguageMutations { + fn markdown(&self) -> String { + let mutations: Vec<_> = self.1.iter().map(|s| s.mutation().clone()).collect(); + format!( + "# {:?} Mutations\n\n{} total\n\n{}", + self.0, + self.1.len(), + fmt_mutation_markdown_table(&mutations), + ) + } + + fn terse(&self) -> String { + self.1 + .iter() + .map(fmt_mutation_terse) + .collect::>() + .join("\n") + } + + fn verbose(&self) -> String { + self.1 + .iter() + .map(fmt_mutation_verbose) + .collect::>() + .join("\n") + } + + fn json(&self) -> String { + facet_json::to_string(self).unwrap() + } +}