From 7091654be75677ecc4fe17465b681cd100b029fd Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Fri, 6 Mar 2026 19:41:13 +0000 Subject: [PATCH] Introduce Session with FacetDiskStore spec and mutant type refactoring --- crates/bough-core/BASE_SPEC.md | 4 +- crates/bough-core/FACET_DISK_STORE_SPEC.md | 19 +++++ crates/bough-core/SESSION_SPEC.md | 4 ++ crates/bough-core/src/base.rs | 10 +-- crates/bough-core/src/lib.rs | 1 + crates/bough-core/src/mutant.rs | 4 ++ crates/bough-core/src/session.rs | 80 ++++++++++++++++++++++ crates/bough-core/src/twig.rs | 6 +- crates/bough-core/src/workspace.rs | 2 + 9 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 crates/bough-core/FACET_DISK_STORE_SPEC.md create mode 100644 crates/bough-core/SESSION_SPEC.md create mode 100644 crates/bough-core/src/session.rs diff --git a/crates/bough-core/BASE_SPEC.md b/crates/bough-core/BASE_SPEC.md index e3e6c11f..d987bf9e 100644 --- a/crates/bough-core/BASE_SPEC.md +++ b/crates/bough-core/BASE_SPEC.md @@ -3,8 +3,8 @@ core[base.root] Base impls Root -core[base.mutant_files] -`Base::mutant_files(language_id: &LanguageId) -> TwigsIter` + + diff --git a/crates/bough-core/FACET_DISK_STORE_SPEC.md b/crates/bough-core/FACET_DISK_STORE_SPEC.md new file mode 100644 index 00000000..ea331526 --- /dev/null +++ b/crates/bough-core/FACET_DISK_STORE_SPEC.md @@ -0,0 +1,19 @@ +## FacetDiskStore + +core[fds.type] +`FacetDiskStore` + +core[fds.keys] +`FacetDiskStore::keys` iterates all keys that have been inserted into it + +core[fds.get] +`FacetDiskStore::get(&key) -> Option<&val>` returns val by key + +core[fds.set] +`FacetDiskStore::get(key, val) -> Result<(), _>` inserts val by key + +core[fds.new] +`FacetDiskStore::new(dir: PathBuf) -> Self` points fds at a specified directory + +core[fds.files] +fds should store all vals on disk at `$dir/$key.json` in json format diff --git a/crates/bough-core/SESSION_SPEC.md b/crates/bough-core/SESSION_SPEC.md new file mode 100644 index 00000000..b69c6d97 --- /dev/null +++ b/crates/bough-core/SESSION_SPEC.md @@ -0,0 +1,4 @@ +## Session + +core[session.init] +`Session::::new(config: Config) -> Result` diff --git a/crates/bough-core/src/base.rs b/crates/bough-core/src/base.rs index f3b6d941..acb365a7 100644 --- a/crates/bough-core/src/base.rs +++ b/crates/bough-core/src/base.rs @@ -1,6 +1,6 @@ use crate::LanguageId; use crate::file::{Error, Root, Twig}; -use crate::mutant::{BasedMutant, TwigMutantsIter}; +use crate::mutant::{Mutant, TwigMutantsIter}; use crate::mutation::{Mutation, MutationIter}; use crate::twig::TwigsIterBuilder; use std::collections::HashMap; @@ -14,6 +14,8 @@ pub struct Base { mutant_files: HashMap, } +/// Base is meant for *scanning* the base directory, and to act as a handle for it +/// It's not for storing state or making decitions, that's [Session]'s job impl Base { pub fn new(root: PathBuf, files: TwigsIterBuilder) -> Result { crate::file::validate_root(&root)?; @@ -43,10 +45,10 @@ impl Base { }) } - pub fn mutants(&self) -> impl Iterator>> + '_ { + pub fn mutants(&self) -> impl Iterator> + '_ { self.mutant_twigs().flat_map(|(language_id, twig)| { match TwigMutantsIter::new(language_id, self, &twig) { - Ok(iter) => iter.map(Ok).collect::>(), + Ok(iter) => iter.map(|bm| Ok(bm.into_mutant())).collect::>(), Err(e) => vec![Err(e)], } }) @@ -54,7 +56,7 @@ impl Base { pub fn mutations(&self) -> impl Iterator> + '_ { self.mutants().flat_map(|r| match r { - Ok(bm) => MutationIter::new(bm.mutant()).map(Ok).collect::>(), + Ok(m) => MutationIter::new(&m).map(Ok).collect::>(), Err(e) => vec![Err(e)], }) } diff --git a/crates/bough-core/src/lib.rs b/crates/bough-core/src/lib.rs index c75c4a8b..7cafa6e3 100644 --- a/crates/bough-core/src/lib.rs +++ b/crates/bough-core/src/lib.rs @@ -7,6 +7,7 @@ mod mutant; mod mutation; mod outcome; mod phase; +mod session; mod test_id; mod twig; mod workspace; diff --git a/crates/bough-core/src/mutant.rs b/crates/bough-core/src/mutant.rs index 86faba3c..05c55d03 100644 --- a/crates/bough-core/src/mutant.rs +++ b/crates/bough-core/src/mutant.rs @@ -80,6 +80,10 @@ impl<'a> BasedMutant<'a> { &self.mutant } + pub fn into_mutant(self) -> Mutant { + self.mutant + } + pub fn base(&self) -> &Base { self.base } diff --git a/crates/bough-core/src/session.rs b/crates/bough-core/src/session.rs new file mode 100644 index 00000000..e3557626 --- /dev/null +++ b/crates/bough-core/src/session.rs @@ -0,0 +1,80 @@ +use std::path::PathBuf; + +use crate::{LanguageId, base::Base, file::Error, twig::TwigsIterBuilder}; + +trait Config { + fn get_base_root_path(&self) -> PathBuf; + fn get_base_include_globs(&self) -> impl Iterator; + fn get_base_exclude_globs(&self) -> impl Iterator; + + fn get_langs(&self) -> impl Iterator; + fn get_lang_include_globs(&self, language_id: LanguageId) -> impl Iterator; + fn get_lang_exclude_globs(&self, language_id: LanguageId) -> impl Iterator; +} + +pub struct TestConfig {} + +pub struct Session { + config: C, + base: Base, + workspaces: Vec, +} + +impl Session { + pub fn new(config: C) -> Result { + let mut base_twigs_iter_builder = TwigsIterBuilder::new(); + for include in config.get_base_include_globs() { + base_twigs_iter_builder = base_twigs_iter_builder.with_include_glob(include); + } + for exclude in config.get_base_exclude_globs() { + base_twigs_iter_builder = base_twigs_iter_builder.with_exclude_glob(exclude); + } + + let mut base = Base::new(config.get_base_root_path(), base_twigs_iter_builder)?; + + for lang in config.get_langs() { + let mut lang_twigs_iter_builder = TwigsIterBuilder::new(); + for include in config.get_lang_include_globs(lang) { + lang_twigs_iter_builder = lang_twigs_iter_builder.with_include_glob(include); + } + for exclude in config.get_lang_exclude_globs(lang) { + lang_twigs_iter_builder = lang_twigs_iter_builder.with_exclude_glob(exclude); + } + base.add_mutator(lang, lang_twigs_iter_builder); + } + + let workspaces = vec![]; + + Ok(Self { + config, + base, + workspaces, + }) + } +} + +impl Config for TestConfig { + fn get_base_root_path(&self) -> PathBuf { + todo!() + } + + fn get_base_include_globs(&self) -> impl Iterator { + vec![].into_iter() + } + + fn get_base_exclude_globs(&self) -> impl Iterator { + vec![].into_iter() + } + + fn get_lang_include_globs(&self, language_id: LanguageId) -> impl Iterator { + vec![].into_iter() + } + + fn get_lang_exclude_globs(&self, language_id: LanguageId) -> impl Iterator { + vec![].into_iter() + } + + fn get_langs(&self) -> impl Iterator { + vec![].into_iter() + } +} diff --git a/crates/bough-core/src/twig.rs b/crates/bough-core/src/twig.rs index 33a1312d..b3e7bbf6 100644 --- a/crates/bough-core/src/twig.rs +++ b/crates/bough-core/src/twig.rs @@ -59,7 +59,9 @@ impl TwigsIterBuilder { .iter() .filter_map(|p| glob::Pattern::new(&root_path.join(p).to_string_lossy()).ok()) .collect(); - let walker = walkdir::WalkDir::new(root_path).sort_by_file_name().into_iter(); + let walker = walkdir::WalkDir::new(root_path) + .sort_by_file_name() + .into_iter(); TwigsIter { root, include, @@ -157,7 +159,7 @@ mod tests { } } - fn sorted_twigs(iter: TwigsIter) -> Vec { + fn sorted_twigs<'a, R: Root>(iter: TwigsIter<'a, R>) -> Vec { let mut twigs: Vec = iter.map(|t| t.path().to_path_buf()).collect(); twigs.sort(); twigs diff --git a/crates/bough-core/src/workspace.rs b/crates/bough-core/src/workspace.rs index 2016f533..d128ebd9 100644 --- a/crates/bough-core/src/workspace.rs +++ b/crates/bough-core/src/workspace.rs @@ -108,6 +108,8 @@ pub struct Workspace<'a> { active: Option, } +/// Workspace is meant for actiing in a workspace directory, and to act as a handle for it +/// It's not for storing state or making decitions, that's [Session]'s job impl<'a> Workspace<'a> { // core[impl workspace.new] // core[impl workspace.new.dir] -- knotmirror 0.0.1