1use serde::Deserialize;
4
5use crate::paths;
6
7#[derive(Debug, Clone, Deserialize, Default)]
8#[serde(default)]
9pub struct Config {
10 pub udid: Option<String>,
12 pub dnd: DndConfig,
13 pub daemon: DaemonConfig,
14 pub script: ScriptConfig,
15}
16
17#[derive(Debug, Clone, Deserialize)]
19#[serde(default)]
20pub struct ScriptConfig {
21 pub timeout_secs: u64,
23 pub memory_mb: u64,
25}
26
27impl Default for ScriptConfig {
28 fn default() -> Self {
29 Self { timeout_secs: 300, memory_mb: 64 }
30 }
31}
32
33#[derive(Debug, Clone, Deserialize, Default)]
35#[serde(default)]
36pub struct DndConfig {
37 pub focus: Option<String>,
40 pub control: Option<String>,
43}
44
45#[derive(Debug, Clone, Deserialize)]
46#[serde(default)]
47pub struct DaemonConfig {
48 pub input_idle_secs: u64,
51 pub idle_exit_secs: u64,
53 pub screen_video: bool,
57 pub restart_accessibility: bool,
60}
61
62impl Default for DaemonConfig {
63 fn default() -> Self {
64 Self {
65 input_idle_secs: 120,
66 idle_exit_secs: 1800,
67 screen_video: false,
68 restart_accessibility: true,
69 }
70 }
71}
72
73impl Config {
74 pub fn load() -> anyhow::Result<Config> {
75 let path = paths::config_file();
76 match std::fs::read_to_string(&path) {
77 Ok(text) => toml::from_str(&text)
78 .map_err(|e| anyhow::anyhow!("{}: {e}", path.display())),
79 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Config::default()),
80 Err(e) => Err(anyhow::anyhow!("{}: {e}", path.display())),
81 }
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88
89 #[test]
90 fn parses_partial_config() {
91 let c: Config = toml::from_str("udid = \"X\"\n[daemon]\ninput_idle_secs = 0\n").unwrap();
92 assert_eq!(c.udid.as_deref(), Some("X"));
93 assert_eq!(c.daemon.input_idle_secs, 0);
94 assert_eq!(c.daemon.idle_exit_secs, 1800);
95 assert_eq!((c.script.timeout_secs, c.script.memory_mb), (300, 64));
96 }
97
98 #[test]
99 fn dnd_can_point_at_another_focus() {
100 let c: Config = toml::from_str("[dnd]\nfocus = \"업무\"\n").unwrap();
101 assert_eq!(c.dnd.focus.as_deref(), Some("업무"));
102 }
103
104 #[test]
105 fn an_old_safety_section_is_ignored() {
106 let c: Config = toml::from_str("[safety]\nonly_apps = [\"com.apple.Preferences\"]\ndnd = false\n").unwrap();
107 assert!(c.dnd.focus.is_none());
108 }
109}