diff --git a/crates/bough-cli/src/find_best_mutations.rs b/crates/bough-cli/src/find_best_mutations.rs index 1da05464..bca581a4 100644 --- a/crates/bough-cli/src/find_best_mutations.rs +++ b/crates/bough-cli/src/find_best_mutations.rs @@ -4,7 +4,7 @@ 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, + HASH, PATH, RESET, STRING, TITLE, Render, fmt_status_colored, mutation_hash, }; pub struct FindBestMutations(pub Vec<(MutationHash, State, f64)>); diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index d9dd7a74..eb2de764 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -19,7 +19,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; 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}; diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index 3cc73dcf..44627295 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -1,7 +1,5 @@ -use std::path::PathBuf; - -use bough_core::{LanguageId, Mutation, MutationHash, State, Status}; -use bough_typed_hash::{TypedHash, TypedHashable, UnvalidatedHash}; +use bough_core::{Mutation, State, Status}; +use bough_typed_hash::TypedHashable; use facet::Facet; use crate::config::{Cli, Config, Format}; @@ -164,198 +162,6 @@ pub(crate) fn fmt_outcome_at_from_state(state: &State) -> String { } } -#[derive(Facet)] -pub struct AllMutations(pub Vec); -impl Render for AllMutations { - fn markdown(&self) -> String { - let mutations: Vec<_> = self.0.iter().map(|s| s.mutation().clone()).collect(); - format!( - "# All Mutations\n\n{} total\n\n{}", - self.0.len(), - fmt_mutation_markdown_table(&mutations), - ) - } - - 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() - } -} - -#[derive(Facet)] -pub struct LangMutations(pub LanguageId, pub Vec); -impl Render for LangMutations { - 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() - } -} - -#[derive(Facet)] -pub struct FileMutations(pub LanguageId, pub PathBuf, pub Vec); -impl Render for FileMutations { - 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() - } -} - -pub fn find_mutation_by_hash(hash: &str, mutations: Vec) -> Mutation { - let unvalidated = UnvalidatedHash::new(hash.to_string()); - let hashes: Vec<_> = mutations - .iter() - .map(|m| m.hash().expect("hashing should not fail")) - .collect(); - let matched = unvalidated - .validate(&hashes) - .expect("hash resolution failed"); - let matched_bytes = matched.as_bytes(); - mutations - .into_iter() - .find(|m| m.hash().unwrap().as_bytes() == matched_bytes) - .unwrap() -} - -pub struct SingleMutation { - pub state: State, - pub before: String, - pub after: String, - pub lang: LanguageId, -} - -impl SingleMutation { - fn lang_tag(&self) -> &'static str { - match self.lang { - LanguageId::Javascript => "javascript", - LanguageId::Typescript => "typescript", - } - } -} - -impl Render for SingleMutation { - fn markdown(&self) -> String { - let m = self.state.mutation(); - let mutant = m.mutant(); - let status = fmt_status_from_state(&self.state); - let at = fmt_outcome_at_from_state(&self.state); - let tag = self.lang_tag(); - format!( - "{TITLE}# Mutation{RESET}\n\n\ - - {BOLD}Hash:{RESET} {HASH}{}{RESET}\n\ - - {BOLD}File:{RESET} {PATH}{}{RESET}\n\ - - {BOLD}Lang:{RESET} {LANG}{:?}{RESET}\n\ - - {BOLD}Kind:{RESET} {:?}\n\ - - {BOLD}Span:{RESET} {}:{}-{}:{}\n\ - - {BOLD}Subst:{RESET} {STRING}{}{RESET}\n\ - - {BOLD}Status:{RESET} {}\n\ - - {BOLD}At:{RESET} {}\n\n\ - {TITLE}## Before{RESET}\n\n```{}\n{}\n```\n\n\ - {TITLE}## After{RESET}\n\n```{}\n{}\n```", - 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(), - status, - at, - tag, - self.before, - tag, - self.after, - ) - } - - fn terse(&self) -> String { - fmt_mutation_terse(&self.state) - } - - fn verbose(&self) -> String { - let status = fmt_status_from_state(&self.state); - let at = fmt_outcome_at_from_state(&self.state); - format!( - "{}\nStatus: {} ({})\n\n--- before ---\n{}\n\n--- after ---\n{}", - fmt_mutation_verbose(&self.state), - status, - at, - self.before, - self.after, - ) - } - - fn json(&self) -> String { - facet_json::to_string(&self.state).unwrap() - } -} - pub struct TendState { pub added: Vec, pub removed: Vec, @@ -712,163 +518,6 @@ impl Render for BenchmarkTimesInBase { } } -pub struct ApplyMutation { - pub workspace_id: bough_core::WorkspaceId, - pub mutation_hash: String, -} - -impl Render for ApplyMutation { - fn markdown(&self) -> String { - format!( - "# Apply Mutation\n\n- Workspace: `{}`\n- Mutation: `{}`", - self.workspace_id, self.mutation_hash, - ) - } - - fn terse(&self) -> String { - format!( - "{HASH}{}{RESET} {HASH}{}{RESET}", - self.workspace_id, self.mutation_hash, - ) - } - - fn verbose(&self) -> String { - format!( - "{TITLE}Apply Mutation{RESET} {HASH}{}{RESET} to {HASH}{}{RESET}", - self.mutation_hash, self.workspace_id, - ) - } - - fn json(&self) -> String { - format!( - r#"{{"workspace_id":"{}","mutation_hash":"{}"}}"#, - self.workspace_id, self.mutation_hash, - ) - } -} - -pub struct UnapplyMutation { - pub workspace_id: bough_core::WorkspaceId, - pub mutation_hash: String, -} - -impl Render for UnapplyMutation { - fn markdown(&self) -> String { - format!( - "# Unapply Mutation\n\n- Workspace: `{}`\n- Mutation: `{}`", - self.workspace_id, self.mutation_hash, - ) - } - - fn terse(&self) -> String { - format!( - "{HASH}{}{RESET} {HASH}{}{RESET}", - self.workspace_id, self.mutation_hash, - ) - } - - fn verbose(&self) -> String { - format!( - "{TITLE}Unapply Mutation{RESET} {HASH}{}{RESET} from {HASH}{}{RESET}", - self.mutation_hash, self.workspace_id, - ) - } - - fn json(&self) -> String { - format!( - r#"{{"workspace_id":"{}","mutation_hash":"{}"}}"#, - self.workspace_id, self.mutation_hash, - ) - } -} - -pub struct FindBestMutations(pub Vec<(MutationHash, State, f64)>); - -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(",")) - } -} - impl Render for Config { fn markdown(&self) -> String { format!(