From 9ab927480290609bfade0198b8a860f285ded251 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Thu, 19 Mar 2026 15:29:53 +0000 Subject: [PATCH] Add LanguageId/MutantKind helpers and mutation reference generation --- Cargo.lock | 1 + crates/bough-core/src/lib.rs | 28 ++++++++++ crates/bough-core/src/mutant.rs | 89 ++++++++++++++++++++++++++++++ crates/bough-docs-cli/Cargo.toml | 1 + crates/bough-docs-cli/src/build.rs | 29 +++++++++- 5 files changed, 147 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8715bc31..3f88a598 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -344,6 +344,7 @@ version = "0.0.2" dependencies = [ "arborium", "bough-cli", + "bough-core", "comrak", "facet", "facet-core", diff --git a/crates/bough-core/src/lib.rs b/crates/bough-core/src/lib.rs index e7bbcef4..7217d3a1 100644 --- a/crates/bough-core/src/lib.rs +++ b/crates/bough-core/src/lib.rs @@ -37,6 +37,34 @@ pub enum LanguageId { Python, } +impl LanguageId { + pub const ALL: &[LanguageId] = &[ + LanguageId::Javascript, + LanguageId::Typescript, + LanguageId::Python, + ]; + + pub fn substitutions(&self, kind: &MutantKind) -> Vec { + language::driver_for_lang(*self).substitutions(kind) + } + + pub fn slug(&self) -> &'static str { + match self { + LanguageId::Javascript => "js", + LanguageId::Typescript => "ts", + LanguageId::Python => "py", + } + } + + pub fn display_name(&self) -> &'static str { + match self { + LanguageId::Javascript => "JavaScript", + LanguageId::Typescript => "TypeScript", + LanguageId::Python => "Python", + } + } +} + pub use base::Base; pub use file::File; pub use file::Twig; diff --git a/crates/bough-core/src/mutant.rs b/crates/bough-core/src/mutant.rs index 7911eed8..57266548 100644 --- a/crates/bough-core/src/mutant.rs +++ b/crates/bough-core/src/mutant.rs @@ -400,6 +400,95 @@ pub enum MutantKind { SwitchCase, } +impl MutantKind { + pub fn all_variants() -> Vec { + use BinaryOpMutationKind::*; + use AssignMutationKind::*; + use ArrayDeclKind::*; + use LiteralKind::*; + use OptionalChainKind::*; + + vec![ + MutantKind::StatementBlock, + MutantKind::Condition, + MutantKind::BinaryOp(Add), + MutantKind::BinaryOp(And), + MutantKind::BinaryOp(BitAnd), + MutantKind::BinaryOp(BitOr), + MutantKind::BinaryOp(BitXor), + MutantKind::BinaryOp(Div), + MutantKind::BinaryOp(Eq), + MutantKind::BinaryOp(Exp), + MutantKind::BinaryOp(FloorDiv), + MutantKind::BinaryOp(Gt), + MutantKind::BinaryOp(In), + MutantKind::BinaryOp(Is), + MutantKind::BinaryOp(IsNot), + MutantKind::BinaryOp(NotIn), + MutantKind::BinaryOp(Gte), + MutantKind::BinaryOp(Lt), + MutantKind::BinaryOp(Lte), + MutantKind::BinaryOp(Mul), + MutantKind::BinaryOp(Or), + MutantKind::BinaryOp(Rem), + MutantKind::BinaryOp(Shl), + MutantKind::BinaryOp(Shr), + MutantKind::BinaryOp(StrictEq), + MutantKind::BinaryOp(Ne), + MutantKind::BinaryOp(StrictNe), + MutantKind::BinaryOp(Sub), + MutantKind::BinaryOp(Xor), + MutantKind::Assign(AddAssign), + MutantKind::Assign(AndAssign), + MutantKind::Assign(BitAndAssign), + MutantKind::Assign(BitOrAssign), + MutantKind::Assign(BitXorAssign), + MutantKind::Assign(DivAssign), + MutantKind::Assign(ExpAssign), + MutantKind::Assign(FloorDivAssign), + MutantKind::Assign(MulAssign), + MutantKind::Assign(NormalAssign), + MutantKind::Assign(OrAssign), + MutantKind::Assign(RemAssign), + MutantKind::Assign(ShlAssign), + MutantKind::Assign(ShrAssign), + MutantKind::Assign(SubAssign), + MutantKind::ArrayDecl(Inline), + MutantKind::ArrayDecl(Instance), + MutantKind::Literal(BoolTrue), + MutantKind::Literal(BoolFalse), + MutantKind::Literal(String), + MutantKind::Literal(EmptyString), + MutantKind::Literal(Number), + MutantKind::DictDecl, + MutantKind::TupleDecl, + MutantKind::Assert, + MutantKind::UnaryNot, + MutantKind::OptionalChain(Literal), + MutantKind::OptionalChain(Indexed), + MutantKind::OptionalChain(FnCall), + MutantKind::SwitchCase, + ] + } + + pub fn heading(&self) -> std::string::String { + match self { + MutantKind::StatementBlock => "StatementBlock".into(), + MutantKind::Condition => "Condition".into(), + MutantKind::BinaryOp(k) => format!("BinaryOp - {k:?}"), + MutantKind::Assign(k) => format!("Assign - {k:?}"), + MutantKind::ArrayDecl(k) => format!("ArrayDecl - {k:?}"), + MutantKind::Literal(k) => format!("Literal - {k:?}"), + MutantKind::DictDecl => "DictDecl".into(), + MutantKind::TupleDecl => "TupleDecl".into(), + MutantKind::Assert => "Assert".into(), + MutantKind::UnaryNot => "UnaryNot".into(), + MutantKind::OptionalChain(k) => format!("OptionalChain - {k:?}"), + MutantKind::SwitchCase => "SwitchCase".into(), + } + } +} + impl<'a, 't> TwigMutantsIter<'a, 't> { pub fn new(lang: LanguageId, base: &'a Base, twig: &'t Twig) -> std::io::Result { // bough[impl mutant.twig-iter.file] diff --git a/crates/bough-docs-cli/Cargo.toml b/crates/bough-docs-cli/Cargo.toml index fe384b21..6ecf58d3 100644 --- a/crates/bough-docs-cli/Cargo.toml +++ b/crates/bough-docs-cli/Cargo.toml @@ -21,5 +21,6 @@ tiny_http = "0.12" markdown-frontmatter = { version = "0.5", default-features = false, features = ["toml"] } serde = { version = "1.0.228", features = ["derive"] } bough-cli = { path = "../bough-cli" } +bough-core = { path = "../bough-core" } notify-debouncer-mini = "0.7" notify = "8" diff --git a/crates/bough-docs-cli/src/build.rs b/crates/bough-docs-cli/src/build.rs index f6079d54..b672a57c 100644 --- a/crates/bough-docs-cli/src/build.rs +++ b/crates/bough-docs-cli/src/build.rs @@ -281,6 +281,25 @@ fn collect_md_files(dir: &Path) -> Vec { files } +fn make_mutation_reference(lang: &bough_core::LanguageId) -> String { + use std::fmt::Write; + let mut out = format!("+++\ntitle = \"{}\"\n+++\n\n", lang.display_name()); + for kind in bough_core::MutantKind::all_variants() { + let subs = lang.substitutions(&kind); + if subs.is_empty() { + continue; + } + let _ = writeln!(out, "## {}\n", kind.heading()); + let _ = writeln!(out, "| Substitution |"); + let _ = writeln!(out, "|---|"); + for sub in &subs { + let _ = writeln!(out, "| `{}` |", sub); + } + let _ = writeln!(out); + } + out +} + pub fn build() { let docs_dir = Path::new(DOCS_DIR); let out_dir = Path::new(OUT_DIR); @@ -299,12 +318,20 @@ pub fn build() { let reference_dir = generated_dir.join("config"); fs::create_dir_all(&reference_dir).expect("failed to create generated config dir"); let reference_md = format!( - "---\ntitle: Configuration Reference\n---\n\n{}", + "+++\ntitle = \"Configuration Reference\"\n+++\n\n{}", crate::facet_reference::make_facet_reference::() ); fs::write(reference_dir.join("reference.md"), reference_md) .expect("failed to write config reference"); + let mutations_dir = generated_dir.join("mutations"); + fs::create_dir_all(&mutations_dir).expect("failed to create generated mutations dir"); + for lang in bough_core::LanguageId::ALL { + let md = make_mutation_reference(lang); + fs::write(mutations_dir.join(format!("{}.md", lang.slug())), md) + .expect("failed to write mutation reference"); + } + let mut md_files = collect_md_files(docs_dir); md_files.extend(collect_md_files(generated_dir)); if md_files.is_empty() { -- knotmirror 0.0.1