diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..2c6a88e --- /dev/null +++ b/src/config.rs @@ -0,0 +1,38 @@ +use std::{ + fs, io, + path::{Path, PathBuf}, +}; + +use serde::Deserialize; + +pub const CONFIG_FILENAME: &str = "vibebox.toml"; + +#[derive(Debug, Default, Deserialize)] +pub struct ProjectConfig { + pub auto_shutdown_ms: Option, +} + +pub fn config_path(project_root: &Path) -> PathBuf { + project_root.join(CONFIG_FILENAME) +} + +pub fn ensure_config_file(project_root: &Path) -> Result { + let path = config_path(project_root); + if !path.exists() { + fs::write(&path, "")?; + tracing::info!(path = %path.display(), "created vibebox config"); + } + Ok(path) +} + +pub fn load_config( + project_root: &Path, +) -> Result> { + let path = ensure_config_file(project_root)?; + let raw = fs::read_to_string(&path)?; + tracing::debug!(path = %path.display(), bytes = raw.len(), "loaded vibebox config"); + if raw.trim().is_empty() { + return Ok(ProjectConfig::default()); + } + Ok(toml::from_str::(&raw)?) +}