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
57 pub fn get_value(&self, key: &str) -> Result<String> {
63 match key {
64 "editor.command" => Ok(self.editor.command.clone().unwrap_or_default()),
65 "open.editor" => Ok(self.open.editor.to_string()),
66 _ => anyhow::bail!("Unknown config key: {key}"),
67 }
68 }
69
70 pub fn set_value(&mut self, key: &str, value: &str) -> Result<()> {
77 match key {
78 "editor.command" => {
79 self.editor.command = if value.is_empty() {
80 None
81 } else {
82 Some(value.to_string())
83 };
84 }
85 "open.editor" => {
86 self.open.editor = value
87 .parse::<bool>()
88 .with_context(|| format!("Invalid boolean value: {value}"))?;
89 }
90 _ => anyhow::bail!("Unknown config key: {key}"),
91 }
92 Ok(())
93 }
94}