1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// See the MachServices section in https://www.manpagez.com/man/5/launchd.plist/
//
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MachServiceEntry {
    Boolean(bool),
    Map(MachServiceOptions),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "io", serde(rename_all = "PascalCase"))]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MachServiceOptions {
    reset_at_close: Option<bool>,
    hide_until_check_in: Option<bool>,
}

impl MachServiceOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_reset_at_close(mut self, value: bool) -> Self {
        self.reset_at_close = Some(value);
        self
    }

    pub fn reset_at_close(self) -> Self {
        self.with_reset_at_close(true)
    }

    pub fn with_hide_until_check_in(mut self, value: bool) -> Self {
        self.hide_until_check_in = Some(value);
        self
    }

    pub fn hide_until_check_in(self) -> Self {
        self.with_hide_until_check_in(true)
    }
}

impl From<MachServiceOptions> for MachServiceEntry {
    fn from(options: MachServiceOptions) -> Self {
        MachServiceEntry::Map(options)
    }
}

impl From<bool> for MachServiceEntry {
    fn from(value: bool) -> Self {
        MachServiceEntry::Boolean(value)
    }
}