feat: cargo config writer for automatic hot tier routing

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
Omar Sobh
2026-06-16 03:50:39 +00:00
co-authored by Claude Sonnet 4.6
parent 08b7f94e4c
commit dfb40eb13b
+76
View File
@@ -0,0 +1,76 @@
use anyhow::{Context, Result};
use std::path::Path;
/// Write .cargo/config.toml in the project's warm directory pointing
/// CARGO_TARGET_DIR at the hot NVMe path. Safe to re-run (idempotent).
pub fn write_cargo_config(warm_path: &Path, hot_target_path: &Path) -> Result<()> {
let cargo_dir = warm_path.join(".cargo");
std::fs::create_dir_all(&cargo_dir)
.with_context(|| format!("creating .cargo dir at {}", cargo_dir.display()))?;
let target = hot_target_path.to_str()
.context("hot_target_path is not valid UTF-8")?;
let content = format!(
"# claw-store managed — do not edit manually\n\
[build]\n\
target-dir = \"{}\"\n",
target
);
let config_path = cargo_dir.join("config.toml");
std::fs::write(&config_path, content)
.with_context(|| format!("writing {}", config_path.display()))
}
/// Returns true if .cargo/config.toml exists and points to the expected hot path.
pub fn verify_cargo_config(warm_path: &Path, hot_target_path: &Path) -> Result<bool> {
let config_path = warm_path.join(".cargo/config.toml");
if !config_path.exists() { return Ok(false); }
let content = std::fs::read_to_string(&config_path)?;
let target = hot_target_path.to_str().unwrap_or("");
Ok(content.contains(target))
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_write_cargo_config() {
let dir = TempDir::new().unwrap();
let warm = dir.path().join("my-project");
std::fs::create_dir_all(&warm).unwrap();
let hot_target: std::path::PathBuf = "/hot/targets/my-project".into();
write_cargo_config(&warm, &hot_target).unwrap();
let cargo_dir = warm.join(".cargo");
let config_file = cargo_dir.join("config.toml");
assert!(config_file.exists());
let content = std::fs::read_to_string(&config_file).unwrap();
assert!(content.contains("[build]"));
assert!(content.contains("/hot/targets/my-project"));
}
#[test]
fn test_verify_cargo_config_detects_correct() {
let dir = TempDir::new().unwrap();
let warm = dir.path().join("proj");
std::fs::create_dir_all(&warm).unwrap();
let hot: std::path::PathBuf = "/hot/targets/proj".into();
write_cargo_config(&warm, &hot).unwrap();
assert!(verify_cargo_config(&warm, &hot).unwrap());
}
#[test]
fn test_verify_cargo_config_detects_missing() {
let dir = TempDir::new().unwrap();
let warm = dir.path().join("no-config-proj");
std::fs::create_dir_all(&warm).unwrap();
let hot: std::path::PathBuf = "/hot/targets/no-config-proj".into();
assert!(!verify_cargo_config(&warm, &hot).unwrap());
}
}