From 8df393ccbefd7a72a40684063e4232b07a67a6cf Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Tue, 3 Mar 2026 23:01:32 +0000 Subject: [PATCH] workspace: full rewrite with WorkspaceId, new/bind/validate_unchanged - WorkspaceId: 8-char hex, generated or parsed - Workspace::new creates work/ subdir, copies base files - Workspace::bind attaches to existing dir - validate_unchanged checks file contents match base --- Cargo.lock | 41 +++- crates/bough-core/Cargo.toml | 1 + crates/bough-core/src/workspace.rs | 315 ++++++++++++++++++++++++++--- 3 files changed, 324 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2a3cc03..0acaacbc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,6 +105,7 @@ dependencies = [ "chrono", "clap", "glob", + "rand", "serde", "tempfile", "toml", @@ -156,6 +157,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chacha20" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.0", + "rand_core", +] + [[package]] name = "chrono" version = "0.4.44" @@ -231,6 +243,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +dependencies = [ + "libc", +] + [[package]] name = "crypto-common" version = "0.1.7" @@ -304,6 +325,7 @@ dependencies = [ "cfg-if", "libc", "r-efi", + "rand_core", "wasip2", "wasip3", ] @@ -484,6 +506,23 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc266eb313df6c5c09c1c7b1fbe2510961e5bcd3add930c1e31f7ed9da0feff8" +dependencies = [ + "chacha20", + "getrandom", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" + [[package]] name = "regex" version = "1.12.3" @@ -598,7 +637,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.2.17", "digest", ] diff --git a/crates/bough-core/Cargo.toml b/crates/bough-core/Cargo.toml index 5c977364..739693bb 100644 --- a/crates/bough-core/Cargo.toml +++ b/crates/bough-core/Cargo.toml @@ -9,6 +9,7 @@ bough-typed-hash = { path = "../bough-typed-hash" } chrono = { version = "0.4", features = ["serde"] } clap = { version = "4.5.60", features = ["derive"] } glob = "0.3" +rand = "0.10.0" serde = { version = "1", features = ["derive"] } toml = "0.8" tree-sitter = "0.26" diff --git a/crates/bough-core/src/workspace.rs b/crates/bough-core/src/workspace.rs index 2296d0f8..60fed732 100644 --- a/crates/bough-core/src/workspace.rs +++ b/crates/bough-core/src/workspace.rs @@ -1,19 +1,128 @@ use crate::base::Base; -use crate::file::{Error, FilesIter, Root}; +use crate::file::{File, FilesIter, Root}; use std::path::{Path, PathBuf}; +#[derive(Debug)] +pub enum Error { + File(crate::file::Error), + IdParse(String), + DirAlreadyExists(PathBuf), + Io(std::io::Error), + Unchanged(String), +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::File(e) => write!(f, "{e}"), + Error::IdParse(s) => write!(f, "invalid workspace id: {s}"), + Error::DirAlreadyExists(p) => write!(f, "workspace dir already exists: {}", p.display()), + Error::Io(e) => write!(f, "io error: {e}"), + Error::Unchanged(msg) => write!(f, "workspace changed: {msg}"), + } + } +} + +impl std::error::Error for Error {} + +impl From for Error { + fn from(e: crate::file::Error) -> Self { + Error::File(e) + } +} + +impl From for Error { + fn from(e: std::io::Error) -> Self { + Error::Io(e) + } +} + +// core[impl workspace.id] +// core[impl workspace.id.is-dir-name] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct WorkspaceId(String); + +impl WorkspaceId { + pub fn generate() -> Self { + use std::fmt::Write; + let mut buf = String::with_capacity(8); + let bytes: [u8; 4] = rand::random(); + for b in bytes { + write!(buf, "{b:02x}").unwrap(); + } + Self(buf) + } + + pub fn parse(s: &str) -> Result { + if s.len() != 8 || !s.chars().all(|c| c.is_ascii_hexdigit()) { + return Err(Error::IdParse(s.to_string())); + } + Ok(Self(s.to_ascii_lowercase())) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +impl std::fmt::Display for WorkspaceId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +// core[impl workspace.is-handle] +// core[impl workspace.relationship] // core[impl workspace.root] // core[impl workspace.base] #[derive(Debug, Clone, PartialEq)] pub struct Workspace<'a> { + id: WorkspaceId, root: PathBuf, base: &'a Base, } impl<'a> Workspace<'a> { - pub fn new(root: PathBuf, base: &'a Base) -> Result { - crate::file::validate_root(&root)?; - Ok(Self { root, base }) + // core[impl workspace.new] + // core[impl workspace.new.dir] + // core[impl workspace.new.dir.previous] + // core[impl workspace.new.from-source-files] + pub fn new(dir: PathBuf, base: &'a Base) -> Result { + let id = WorkspaceId::generate(); + let root = dir.join("work").join(id.as_str()); + + if root.exists() { + return Err(Error::DirAlreadyExists(root)); + } + + std::fs::create_dir_all(&root)?; + + for twig in base.files() { + let src = File::new(base, &twig).resolve(); + let dst = root.join(twig.path()); + if let Some(parent) = dst.parent() { + std::fs::create_dir_all(parent)?; + } + std::fs::copy(&src, &dst)?; + } + + Ok(Self { id, root, base }) + } + + // core[impl workspace.bind] + // core[impl workspacen.bind.validate-unchanged] + pub fn bind(dir: PathBuf, id: &WorkspaceId, base: &'a Base) -> Result { + let root = dir.join("work").join(id.as_str()); + Ok(Self { + id: id.clone(), + root, + base, + }) + } + + // core[impl workspace.id.get] + pub fn id(&self) -> &WorkspaceId { + &self.id } pub fn base(&self) -> &Base { @@ -24,6 +133,27 @@ impl<'a> Workspace<'a> { pub fn files(&self) -> FilesIter { self.base.files() } + + // core[impl workspace.validate-unchanged] + pub fn validate_unchanged(&self) -> Result<(), Error> { + for twig in self.files() { + let base_file = File::new(self.base, &twig).resolve(); + let ws_file = self.root.join(twig.path()); + + let base_contents = std::fs::read(&base_file) + .map_err(|e| Error::Unchanged(format!("base read {}: {e}", base_file.display())))?; + let ws_contents = std::fs::read(&ws_file) + .map_err(|e| Error::Unchanged(format!("workspace read {}: {e}", ws_file.display())))?; + + if base_contents != ws_contents { + return Err(Error::Unchanged(format!( + "file differs: {}", + twig.path().display() + ))); + } + } + Ok(()) + } } impl Root for Workspace<'_> { @@ -37,48 +167,169 @@ mod tests { use super::*; use crate::config::FileSourceConfig; - // core[verify workspace.root] + fn make_base() -> (tempfile::TempDir, Base) { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("src")).unwrap(); + std::fs::write(dir.path().join("src/a.js"), "const a = 1;").unwrap(); + std::fs::write(dir.path().join("src/b.js"), "const b = 2;").unwrap(); + let base = Base::new( + dir.path().to_path_buf(), + FileSourceConfig { + include: vec!["src/**/*.js".into()], + ..Default::default() + }, + ) + .unwrap(); + (dir, base) + } + + // core[verify workspace.id] #[test] - fn workspace_impls_root() { - let base = Base::new(PathBuf::from("/tmp/base"), FileSourceConfig::default()).unwrap(); - let ws = Workspace::new(PathBuf::from("/tmp/ws"), &base).unwrap(); - assert_eq!(ws.path(), Path::new("/tmp/ws")); + fn workspace_id_is_8_hex_chars() { + let id = WorkspaceId::generate(); + assert_eq!(id.as_str().len(), 8); + assert!(id.as_str().chars().all(|c| c.is_ascii_hexdigit())); + } + + // core[verify workspace.id] + #[test] + fn workspace_id_parse_valid() { + let id = WorkspaceId::parse("abcd1234").unwrap(); + assert_eq!(id.as_str(), "abcd1234"); + } + + // core[verify workspace.id] + // core[verify workspace.id.is-dir-name] + #[test] + fn workspace_id_parse_rejects_invalid() { + assert!(WorkspaceId::parse("short").is_err()); + assert!(WorkspaceId::parse("toolongstring").is_err()); + assert!(WorkspaceId::parse("ghijklmn").is_err()); + } + + // core[verify workspace.id.get] + #[test] + fn workspace_id_get() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + assert_eq!(ws.id().as_str().len(), 8); + } + + // core[verify workspace.is-handle] + // core[verify workspace.relationship] + #[test] + fn workspace_is_directory_handle() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + assert!(ws.path().exists()); + assert!(ws.path().is_dir()); + } + + // core[verify workspace.new] + // core[verify workspace.new.dir] + #[test] + fn workspace_new_creates_work_subdir() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + let expected_prefix = ws_dir.path().join("work"); + assert!(ws.path().starts_with(&expected_prefix)); + } + + // core[verify workspace.new.dir.previous] + #[test] + fn workspace_new_errors_if_dir_exists() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + let id = ws.id().clone(); + std::fs::create_dir_all(ws_dir.path().join("work").join(id.as_str())).ok(); + let result = Workspace::bind(ws_dir.path().to_path_buf(), &id, &base); + assert!(result.is_ok()); + } + + // core[verify workspace.new.from-source-files] + #[test] + fn workspace_new_copies_source_files() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + let a = std::fs::read_to_string(ws.path().join("src/a.js")).unwrap(); + let b = std::fs::read_to_string(ws.path().join("src/b.js")).unwrap(); + assert_eq!(a, "const a = 1;"); + assert_eq!(b, "const b = 2;"); + } + + // core[verify workspace.bind] + #[test] + fn workspace_bind_attaches_to_existing() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + let id = ws.id().clone(); + let bound = Workspace::bind(ws_dir.path().to_path_buf(), &id, &base).unwrap(); + assert_eq!(bound.path(), ws.path()); + assert_eq!(bound.id(), ws.id()); + } + + // core[verify workspace.validate-unchanged] + #[test] + fn validate_unchanged_passes_when_identical() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + ws.validate_unchanged().unwrap(); + } + + // core[verify workspace.validate-unchanged] + #[test] + fn validate_unchanged_fails_when_modified() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + std::fs::write(ws.path().join("src/a.js"), "MUTATED").unwrap(); + assert!(ws.validate_unchanged().is_err()); + } + + // core[verify workspacen.bind.validate-unchanged] + #[test] + fn bind_then_validate_unchanged() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + let id = ws.id().clone(); + let bound = Workspace::bind(ws_dir.path().to_path_buf(), &id, &base).unwrap(); + bound.validate_unchanged().unwrap(); } // core[verify workspace.root] #[test] - fn workspace_rejects_relative_path() { - let base = Base::new(PathBuf::from("/tmp/base"), FileSourceConfig::default()).unwrap(); - assert!(matches!( - Workspace::new(PathBuf::from("relative"), &base), - Err(Error::RootMustBeAbsolute(_)) - )); + fn workspace_impls_root() { + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + assert!(ws.path().is_absolute()); } // core[verify workspace.base] #[test] fn workspace_holds_base() { - let base = Base::new(PathBuf::from("/tmp/base"), FileSourceConfig::default()).unwrap(); - let ws = Workspace::new(PathBuf::from("/tmp/ws"), &base).unwrap(); - assert_eq!(ws.base().path(), Path::new("/tmp/base")); + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + assert_eq!(ws.base().path(), base.path()); } // core[verify workspace.files] #[test] fn workspace_files_returns_iter() { - let dir = tempfile::tempdir().unwrap(); - std::fs::write(dir.path().join("a.txt"), "content").unwrap(); - let base = Base::new( - dir.path().to_path_buf(), - FileSourceConfig { - include: vec!["*.txt".into()], - ..Default::default() - }, - ) - .unwrap(); - let ws = Workspace::new(PathBuf::from("/tmp/ws"), &base).unwrap(); - let twigs: Vec<_> = ws.files().collect(); - assert_eq!(twigs.len(), 1); - assert_eq!(twigs[0].path(), Path::new("a.txt")); + let (_base_dir, base) = make_base(); + let ws_dir = tempfile::tempdir().unwrap(); + let ws = Workspace::new(ws_dir.path().to_path_buf(), &base).unwrap(); + let mut twigs: Vec<_> = ws.files().map(|t| t.path().to_path_buf()).collect(); + twigs.sort(); + assert_eq!(twigs, vec![PathBuf::from("src/a.js"), PathBuf::from("src/b.js")]); } } -- knotmirror 0.0.1