worktree_io/config/
ops.rs1use anyhow::{Context, Result};
2use std::path::PathBuf;
3
4use super::Config;
5
6impl Config {
7 pub fn path() -> Result<PathBuf> {
13 let home = dirs::home_dir().context("Could not determine home directory")?;
14 Ok(home.join(".config").join("worktree").join("config.toml"))
15 }
16
17 pub fn load() -> Result<Self> {
23 let path = Self::path()?;
24 if !path.exists() {
25 return Ok(Self::default());
26 }
27 let content = std::fs::read_to_string(&path)
29 .with_context(|| format!("Failed to read config from {}", path.display()))?;
30 let config: Self = toml::from_str(&content)
31 .with_context(|| format!("Failed to parse config at {}", path.display()))?;
32 Ok(config)
33 }
35
36 pub fn save(&self) -> Result<()> {
43 let path = Self::path()?;
46 if let Some(parent) = path.parent() {
47 std::fs::create_dir_all(parent)
48 .with_context(|| format!("Failed to create config dir {}", parent.display()))?;
49 }
50 let content = self.to_toml_with_comments();
51 std::fs::write(&path, content)
52 .with_context(|| format!("Failed to write config to {}", path.display()))?;
53 Ok(())
54 }
56}