Skip to main content

worktree_io/config/
ops.rs

1use anyhow::{Context, Result};
2use std::path::PathBuf;
3
4use super::Config;
5
6impl Config {
7    /// Return the path to the config file (`~/.config/worktree/config.toml`).
8    ///
9    /// # Errors
10    ///
11    /// Returns an error if the home directory cannot be determined.
12    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    /// Load config from disk, returning `Default` if the file does not yet exist.
18    ///
19    /// # Errors
20    ///
21    /// Returns an error if the file cannot be read or parsed.
22    pub fn load() -> Result<Self> {
23        let path = Self::path()?;
24        if !path.exists() {
25            return Ok(Self::default());
26        }
27        // LLVM_COV_EXCL_START
28        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        // LLVM_COV_EXCL_STOP
34    }
35
36    /// Persist the current config to disk.
37    ///
38    /// # Errors
39    ///
40    /// Returns an error if the config directory cannot be created or the file
41    /// cannot be written.
42    pub fn save(&self) -> Result<()> {
43        // LLVM_COV_EXCL_LINE
44        // LLVM_COV_EXCL_START
45        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        // LLVM_COV_EXCL_STOP
55    }
56}