From 05d57bc60c4fd3ce1734574632a1bc97a459322d Mon Sep 17 00:00:00 2001 From: Mark Dittmer Date: Thu, 14 May 2026 14:52:20 +0000 Subject: [PATCH] Start cleaning up vibe-coded 'Rename v2..Introduce standalone' changes gherrit-pr-id: Gujheulv5kc2p4w3mxqys7plrd56znrwb --- anneal/Cargo.lock | 36 ++-- .../examples/static-toml/src/main.rs | 3 +- .../examples/static-toml/toolchain.tar.zst | Bin 179 -> 181 bytes anneal/v2/toolchain-config/src/lib.rs | 156 ++++++++++-------- 4 files changed, 117 insertions(+), 78 deletions(-) diff --git a/anneal/Cargo.lock b/anneal/Cargo.lock index e36a991600..00f68d5437 100644 --- a/anneal/Cargo.lock +++ b/anneal/Cargo.lock @@ -2108,18 +2108,6 @@ dependencies = [ "serde", ] -[[package]] -name = "setup" -version = "0.1.0" -dependencies = [ - "log", - "reqwest", - "sha2", - "tar", - "tempfile", - "zstd", -] - [[package]] name = "sha2" version = "0.10.9" @@ -2509,6 +2497,30 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" +[[package]] +name = "toolchain-config" +version = "0.1.0" +dependencies = [ + "digest", + "log", + "reqwest", + "sha2", + "tar", + "tempfile", + "zstd", +] + +[[package]] +name = "toolchain-config-example-static-toml" +version = "0.1.0" +dependencies = [ + "clap", + "dirs", + "sha2", + "toml", + "toolchain-config", +] + [[package]] name = "tower" version = "0.5.3" diff --git a/anneal/v2/toolchain-config/examples/static-toml/src/main.rs b/anneal/v2/toolchain-config/examples/static-toml/src/main.rs index 5cf9ee755b..06d06e32b2 100644 --- a/anneal/v2/toolchain-config/examples/static-toml/src/main.rs +++ b/anneal/v2/toolchain-config/examples/static-toml/src/main.rs @@ -40,6 +40,7 @@ fn main() { match cli.command { Commands::Setup => { + // TODO: Probably use a flag, not an environment variable to activate override. let local_override = if std::env::var("__TOOLCHAIN_EXAMPLE_STATIC_TOML").is_ok() { println!("Local testing override active. Assembling mock toolchain archive..."); let manifest_dir = std::env::var("CARGO_MANIFEST_DIR") @@ -52,7 +53,7 @@ fn main() { assert!(status.success(), "build-toolchain.sh script failed"); let archive_path = std::path::Path::new(&manifest_dir).join("toolchain.tar.zst"); - Some(LocalOverride::Archive(archive_path)) + Some(LocalOverride::::archive(archive_path)) } else { None }; diff --git a/anneal/v2/toolchain-config/examples/static-toml/toolchain.tar.zst b/anneal/v2/toolchain-config/examples/static-toml/toolchain.tar.zst index 9ee1244aaa84105db36c5d2e47d55a78b9485d1e..fd046ad8e422dca9597f27472e7ba56efbd93643 100644 GIT binary patch delta 145 zcmV;C0B--Y0kr{tD77#B1Xx7{0Mf`E9$>Z30kFVUn1(~ES?GQg-~#^S_m*s%5NbgH zijx6^Q7o_{fC?gnZ(wL-2#;VQr|98%9ZjkTTytAPmLr6ZL0IFbQPh#p6uS`6&S@IT zevTwHi=my9s_mp`O4dyHY@fXLjqMk2vc(Cn=kj8UJpbeb|5F_Sk%2A*i5o6Ravwtc delta 143 zcmV;A0C4}c0kZ*rD77#B1Xwi%0MbYu9Z+R#4!~gv3ceE6y*{c!#^`?rZ4%@0NHhDK zN(O{cG;kw;3L=DWU}$s*k8mc(=plMLRY(!A=5i%jj}ShGHRaL6AV*47+(N)Q=vgWE xK@{~ghILS?uA`h)Q7u!w( { pub os: &'a str, pub arch: &'a str, pub url: &'a str, - pub sha256: &'a [u8], + pub checksum: &'a [u8], _extractor: std::marker::PhantomData, _digest: std::marker::PhantomData, } -/// Optional runtime override directing installation from local dev builds instead of remote hosts. -#[derive(Debug, Clone)] -pub enum LocalOverride { +/// Local toolchain definition that overrides remote specified in [`Config`]. +pub enum LocalOverride { Dir(std::path::PathBuf), - Archive(std::path::PathBuf), + Archive((std::path::PathBuf, std::marker::PhantomData)), +} + +impl LocalOverride { + pub fn dir(path: std::path::PathBuf) -> Self { + Self::Dir(path) + } + + pub fn archive(path: std::path::PathBuf) -> Self { + Self::Archive((path, std::marker::PhantomData)) + } } impl<'a, E: Extractor, D: digest::Digest> Config<'a, E, D> { /// Instantiates static toolchain parameters auto-detecting current runtime OS and Architecture. - pub fn new(url: &'a str, sha256: &'a [u8]) -> Self { + pub fn new(url: &'a str, checksum: &'a [u8]) -> Self { Self { os: std::env::consts::OS, arch: std::env::consts::ARCH, url, - sha256, + checksum, _extractor: std::marker::PhantomData, _digest: std::marker::PhantomData, } } /// Explicitly overrides target platform parameters for specialized configurations. - pub fn new_platform(os: &'a str, arch: &'a str, url: &'a str, sha256: &'a [u8]) -> Self { + pub fn new_platform(os: &'a str, arch: &'a str, url: &'a str, checksum: &'a [u8]) -> Self { Self { os, arch, url, - sha256, + checksum, _extractor: std::marker::PhantomData, _digest: std::marker::PhantomData, } @@ -43,8 +52,8 @@ impl<'a, E: Extractor, D: digest::Digest> Config<'a, E, D> { /// Resolves the deterministic subdirectory path containing the verified toolchain files. pub fn toolchain_dir(&self, root: &std::path::Path) -> std::path::PathBuf { - let expected_hex = encode_hex(self.sha256); - root.join(&expected_hex[..6]) + let expected_hex = encode_hex(self.checksum); + root.join(&format!("{}-{}-{}", self.os, self.arch, &expected_hex[..12])) } } @@ -53,13 +62,26 @@ impl<'a, E: Extractor, D: digest::Digest> Config<'a, E, D> { pub trait Extractor { /// Instantiates a new instance of this extractor type. /// - /// Instantiated and used for extracting archives downloaded from [`Config::url`] when - /// no local override is specified during invocation. + /// Instantiated and used for extracting archives downloaded from [`Config::url`] or archives + /// designated by [`LocalOverride::Archive`]. fn new() -> Self; + /// Unpacks stream bytes directly into the specified target directory synchronously on the calling thread. fn extract(&self, src: &mut dyn std::io::Read, dst: &std::path::Path) -> std::io::Result<()>; } +/// Marker type used by [`LocalOverride::Dir`] (which implies no archive extraction). +pub struct NoExtractor; + +impl Extractor for NoExtractor { + fn new() -> Self { + panic!("NoExtractor is not constructible"); + } + fn extract(&self, _src: &mut dyn std::io::Read, _dst: &std::path::Path) -> std::io::Result<()> { + panic!("Attempt to extract using NoExtractor"); + } +} + /// A concrete [`Extractor`] implementation delegating archive extraction duties entirely in-process /// via synchronous streaming pipelines using the `tar` and `zstd` library crates. pub struct TarZstLibraryExtractor; @@ -84,6 +106,7 @@ fn encode_hex(bytes: &[u8]) -> String { s } +/// [`std::io::Read`] abstraction that encapsulates read-and-update-hasher. struct HashReader { inner: R, hasher: D, @@ -91,10 +114,7 @@ struct HashReader { impl HashReader { fn new(inner: R) -> Self { - Self { - inner, - hasher: D::new(), - } + Self { inner, hasher: D::new() } } fn finalize(self) -> Vec { @@ -187,7 +207,8 @@ fn link_or_copy_dir(src: &std::path::Path, dst: &std::path::Path) -> std::io::Re fn setup_from_directory(src: &std::path::Path, dst: &std::path::Path) -> Result<(), String> { let parent = dst.parent().expect("toolchains directory has parent"); - std::fs::create_dir_all(parent).map_err(|e| format!("Failed to create toolchain parent directory: {e}"))?; + std::fs::create_dir_all(parent) + .map_err(|e| format!("Failed to create toolchain parent directory: {e}"))?; let old_dir = if dst.symlink_metadata().is_ok() { let old = tempfile::Builder::new() .prefix("setup-old-") @@ -208,43 +229,43 @@ fn setup_from_directory(src: &std::path::Path, dst: &std::path::Path) -> Result< Ok(()) } -fn setup_inner( - config: &Config<'_, E, D>, - local_override: Option, - toolchain_dir: std::path::PathBuf, +fn setup_inner( + config: &Config<'_, CE, D>, + local_override: Option>, + toolchain_root: std::path::PathBuf, fetcher: impl FnOnce(&str) -> Result, String>, ) -> Result<(), String> { - let target_dir = config.toolchain_dir(&toolchain_dir); - let expected_hex = encode_hex(config.sha256); + let toolchain_dir = config.toolchain_dir(&toolchain_root); + let expected_hex = encode_hex(config.checksum); if let Some(override_src) = local_override { match override_src { - LocalOverride::Archive(path) => { + LocalOverride::Archive((path, _)) => { log::warn!( "Toolchain contents from local archive may not match expected toolchain hash/version number." ); - let extractor = E::new(); + let extractor = LE::new(); let file = std::fs::File::open(path) .map_err(|e| format!("Failed to open local archive: {e}"))?; - setup_from_archive::(file, &target_dir, &extractor) + setup_from_archive::(file, &toolchain_dir, &extractor) .map_err(|e| format!("Failed to extract archive: {e}"))?; } LocalOverride::Dir(path) => { log::warn!( "Toolchain contents from local directory may not match expected toolchain hash/version number." ); - setup_from_directory(&path, &target_dir)?; + setup_from_directory(&path, &toolchain_dir)?; } } } else { - let extractor = E::new(); + let extractor = CE::new(); let response = fetcher(config.url)?; - let actual_hash = setup_from_archive::(response, &target_dir, &extractor) + let actual_hash = setup_from_archive::(response, &toolchain_dir, &extractor) .map_err(|e| format!("Failed to extract downloaded archive: {e}"))?; - if actual_hash.as_slice() != config.sha256 { - let _ = std::fs::remove_dir_all(&target_dir); + if actual_hash.as_slice() != config.checksum { + let _ = std::fs::remove_dir_all(&toolchain_dir); return Err(format!( "Checksum mismatch for downloaded archive. Expected {}, got {}", expected_hex, @@ -263,14 +284,14 @@ fn setup_inner( /// /// When no local override is specified, the configured [`Extractor`] type `E` is instantiated /// via [`Extractor::new`] and used to extract the downloaded toolchain archive stream. -pub fn setup( - config: &Config<'_, E, D>, - local_override: Option, - toolchain_dir: std::path::PathBuf, +pub fn setup( + config: &Config<'_, CE, D>, + local_override: Option>, + toolchain_root: std::path::PathBuf, ) -> Result<(), String> { - setup_inner(config, local_override, toolchain_dir, |url| { - let response = reqwest::blocking::get(url) - .map_err(|e| format!("Failed to download archive: {e}"))?; + setup_inner(config, local_override, toolchain_root, |url| { + let response = + reqwest::blocking::get(url).map_err(|e| format!("Failed to download archive: {e}"))?; let response = response .error_for_status() .map_err(|e| format!("HTTP error downloading archive: {e}"))?; @@ -332,7 +353,12 @@ mod tests { let dst = temp.path().join("dst"); let file = std::fs::File::open(&archive_path).unwrap(); - let hash = setup_from_archive::(file, &dst, &TarZstLibraryExtractor).unwrap(); + let hash = setup_from_archive::( + file, + &dst, + &TarZstLibraryExtractor, + ) + .unwrap(); assert_eq!(hash, compute_sha256(&archive_path)); assert_eq!(std::fs::read_to_string(dst.join("data.txt")).unwrap(), "archive_content"); @@ -346,12 +372,15 @@ mod tests { std::fs::write(src.join("test.txt"), "local_dir").unwrap(); let expected_hash = [1u8; 32]; - let config = Config::::new("http://example.com", &expected_hash); + let config = Config::::new( + "http://example.com", + &expected_hash, + ); let target_dir = config.toolchain_dir(temp.path()); setup_inner( &config, - Some(LocalOverride::Dir(src)), + Some(LocalOverride::::Dir(src)), temp.path().to_path_buf(), |_| unreachable!(), ) @@ -371,12 +400,15 @@ mod tests { create_test_archive(&src, &archive_path); let expected_hash = [2u8; 32]; - let config = Config::::new("http://example.com", &expected_hash); + let config = Config::::new( + "http://example.com", + &expected_hash, + ); let target_dir = config.toolchain_dir(temp.path()); setup_inner( &config, - Some(LocalOverride::Archive(archive_path)), + Some(LocalOverride::::archive(archive_path)), temp.path().to_path_buf(), |_| unreachable!(), ) @@ -396,19 +428,15 @@ mod tests { create_test_archive(&src, &archive_path); let actual_hash = compute_sha256(&archive_path); - let config = Config::::new("http://example.com", &actual_hash); + let config = + Config::::new("http://example.com", &actual_hash); let target_dir = config.toolchain_dir(temp.path()); let archive_path_clone = archive_path.clone(); - setup_inner( - &config, - None, - temp.path().to_path_buf(), - move |_url| { - let file = std::fs::File::open(&archive_path_clone).unwrap(); - Ok(Box::new(file)) - }, - ) + setup_inner::<_, _, NoExtractor>(&config, None, temp.path().to_path_buf(), move |_url| { + let file = std::fs::File::open(&archive_path_clone).unwrap(); + Ok(Box::new(file)) + }) .unwrap(); assert_eq!(std::fs::read_to_string(target_dir.join("test.txt")).unwrap(), "remote_content"); @@ -428,19 +456,17 @@ mod tests { let mut expected_hash = actual_hash; expected_hash[0] ^= 1; // invalidate checksum - let config = Config::::new("http://example.com", &expected_hash); + let config = Config::::new( + "http://example.com", + &expected_hash, + ); let target_dir = config.toolchain_dir(temp.path()); let archive_path_clone = archive_path.clone(); - let res = setup_inner( - &config, - None, - temp.path().to_path_buf(), - move |_url| { - let file = std::fs::File::open(&archive_path_clone).unwrap(); - Ok(Box::new(file)) - }, - ); + let res = setup_inner::<_, _, NoExtractor>(&config, None, temp.path().to_path_buf(), move |_url| { + let file = std::fs::File::open(&archive_path_clone).unwrap(); + Ok(Box::new(file)) + }); assert!(res.is_err()); assert!(res.unwrap_err().contains("Checksum mismatch"));