1use crate::error::BondError;
2use serde::{Deserialize, Serialize};
3use std::fs;
4use std::path::PathBuf;
5
6#[derive(Debug, Serialize, Deserialize, Default)]
9pub struct BondsConfig {
10 pub default_target: Option<PathBuf>,
13}
14
15impl BondsConfig {
16 pub fn config_path() -> PathBuf {
18 std::env::var("HOME")
19 .map(PathBuf::from)
20 .unwrap_or_else(|_| PathBuf::from("."))
21 .join(".bonds")
22 .join("config.toml")
23 }
24
25 pub fn load() -> Result<Self, BondError> {
28 let path = Self::config_path();
29 if !path.exists() {
30 return Ok(Self::default());
31 }
32 let content = fs::read_to_string(&path)?;
33 toml::from_str(&content).map_err(|e| BondError::Config(e.to_string()))
35 }
36
37 pub fn save(&self) -> Result<(), BondError> {
40 let path = Self::config_path();
41 if let Some(parent) = path.parent() {
42 fs::create_dir_all(parent)?;
43 }
44 let content = toml::to_string_pretty(self).map_err(|e| BondError::Config(e.to_string()))?;
45 fs::write(&path, content)?;
46 Ok(())
47 }
48}