use std::fs;
use std::path::Path;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct VltConfig {
pub active_env: Option<String>,
}
impl VltConfig {
pub fn load(path: &Path) -> Result<Self> {
let contents = fs::read_to_string(path)
.with_context(|| format!("failed to read {}", path.display()))?;
if contents.trim().is_empty() {
return Ok(Self::default());
}
toml::from_str(&contents).with_context(|| format!("failed to parse {}", path.display()))
}
pub fn load_or_default(path: &Path) -> Result<Self> {
if !path.exists() {
return Ok(Self::default());
}
Self::load(path)
}
pub fn save(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
}
let contents = toml::to_string_pretty(self).context("failed to serialize config")?;
fs::write(path, contents).with_context(|| format!("failed to write {}", path.display()))
}
}