use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; /// A configured registry in config.toml. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RegistryConfig { pub url: String, #[serde(default)] pub name: Option, } /// Contents of ~/.wasmbox/config.toml. #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct WasmBoxConfig { #[serde(default)] pub registries: Vec, } impl WasmBoxConfig { pub fn load(path: &Path) -> Result> { if path.exists() { let content = std::fs::read_to_string(path)?; Ok(toml::from_str(&content)?) } else { Ok(Self::default()) } } pub fn save(&self, path: &Path) -> Result<(), Box> { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } let content = toml::to_string_pretty(self)?; std::fs::write(path, content)?; Ok(()) } } /// Resolved paths for all WasmBox directories and files. /// /// Resolution order: --home flag > WASMBOX_HOME env > ~/.wasmbox/ #[derive(Debug, Clone)] pub struct WasmBoxPaths { home: PathBuf, } impl WasmBoxPaths { /// Create paths from an explicit home directory. pub fn new(home: PathBuf) -> Self { Self { home } } /// Resolve paths using: explicit override > WASMBOX_HOME env > ~/.wasmbox/ pub fn resolve(cli_override: Option<&str>) -> Self { let home = if let Some(path) = cli_override { PathBuf::from(path) } else if let Ok(path) = std::env::var("WASMBOX_HOME") { PathBuf::from(path) } else { dirs::home_dir() .expect("could not determine home directory") .join(".wasmbox") }; Self { home } } pub fn home(&self) -> &Path { &self.home } pub fn config_file(&self) -> PathBuf { self.home.join("config.toml") } pub fn permissions_file(&self) -> PathBuf { self.home.join("permissions.toml") } pub fn cache_dir(&self) -> PathBuf { self.home.join("cache") } pub fn tool_dir(&self, name: &str, version: &str) -> PathBuf { self.cache_dir().join(name).join(version) } pub fn tool_wasm_path(&self, name: &str, version: &str, wasm_file: &str) -> PathBuf { self.tool_dir(name, version).join(wasm_file) } pub fn tool_manifest_path(&self, name: &str, version: &str) -> PathBuf { self.tool_dir(name, version).join("wasmbox.toml") } /// Create all necessary directories. pub fn ensure_dirs(&self) -> std::io::Result<()> { std::fs::create_dir_all(&self.home)?; std::fs::create_dir_all(self.cache_dir())?; Ok(()) } }