worktree_io/config/
ops_get_set.rs1use anyhow::{Context, Result};
2
3use super::Config;
4
5impl Config {
6 pub fn get_value(&self, key: &str) -> Result<String> {
12 match key {
13 "editor" | "editor.command" => Ok(self.editor.command.clone().unwrap_or_default()),
14 "open.editor" => Ok(self.open.editor.to_string()),
15 "workspace.ttl" => Ok(self
16 .workspace
17 .ttl
18 .map_or_else(String::new, |t| t.to_string())),
19 "workspace.auto_prune" => Ok(self.workspace.auto_prune.to_string()),
20 "workspace.temp" => Ok(self.workspace.temp.to_string()),
21 _ => anyhow::bail!("Unknown config key: {key}"),
22 }
23 }
24
25 pub fn set_value(&mut self, key: &str, value: &str) -> Result<()> {
32 match key {
33 "editor" | "editor.command" => {
34 self.editor.command = (!value.is_empty()).then(|| value.to_string());
35 }
36 "open.editor" => {
37 self.open.editor = value
38 .parse::<bool>()
39 .with_context(|| format!("Invalid boolean value: {value}"))?;
40 }
41 "workspace.ttl" => {
42 self.workspace.ttl = (!value.is_empty())
43 .then(|| {
44 value
45 .parse()
46 .map_err(|e| anyhow::anyhow!("Invalid duration {value:?}: {e}"))
47 })
48 .transpose()?;
49 }
50 "workspace.auto_prune" => {
51 self.workspace.auto_prune = value
52 .parse::<bool>()
53 .with_context(|| format!("Invalid boolean value: {value}"))?;
54 }
55 "workspace.temp" => {
56 self.workspace.temp = value
57 .parse::<bool>()
58 .with_context(|| format!("Invalid boolean value: {value}"))?;
59 }
60 _ => anyhow::bail!("Unknown config key: {key}"),
61 }
62 Ok(())
63 }
64}