Skip to main content

iphone_cli/
config.rs

1//! `~/.config/iph/config.toml`.
2
3use serde::Deserialize;
4
5use crate::paths;
6
7#[derive(Debug, Clone, Deserialize, Default)]
8#[serde(default)]
9pub struct Config {
10    /// Device to use when `--udid` is not given.
11    pub udid: Option<String>,
12    pub dnd: DndConfig,
13    pub daemon: DaemonConfig,
14    pub script: ScriptConfig,
15}
16
17/// Limits for `iph script` and `iph run`.
18#[derive(Debug, Clone, Deserialize)]
19#[serde(default)]
20pub struct ScriptConfig {
21    /// A script is stopped after this many seconds (at most 840).
22    pub timeout_secs: u64,
23    /// A script is stopped when it holds more than this many megabytes.
24    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/// `iph dnd`.
34#[derive(Debug, Clone, Deserialize, Default)]
35#[serde(default)]
36pub struct DndConfig {
37    /// The Focus to switch to instead of Do Not Disturb, by the name the
38    /// phone shows for it.
39    pub focus: Option<String>,
40    /// Label of the Focus control in Control Center, for a phone whose
41    /// language is neither Korean nor English.
42    pub control: Option<String>,
43}
44
45#[derive(Debug, Clone, Deserialize)]
46#[serde(default)]
47pub struct DaemonConfig {
48    /// Close the touch-input media session after this many idle seconds.
49    /// 0 closes it at the end of every command.
50    pub input_idle_secs: u64,
51    /// Exit the daemon after this many idle seconds.
52    pub idle_exit_secs: u64,
53    /// Also take the screen video while an input session is open. Touches
54    /// count without it, so it is off: the video is megabits a second through
55    /// the same tunnel as everything else.
56    pub screen_video: bool,
57    /// When the phone's accessibility daemon stops answering, end it so the
58    /// phone starts a new one, and read again.
59    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}