bspc_rs/parser/parse_events/
monitor_events.rs

1use super::*;
2
3impl FromStr for MonitorAddInfo {
4    type Err = ParseError;
5
6    fn from_str(input: &str) -> Result<Self, Self::Err> {
7        let reply = process_event_reply(input, "monitor_add", 3)?;
8
9        Ok(Self {
10            monitor_id: from_hex_to_id(reply[1])?,
11            monitor_name: reply[2].to_string(),
12            monitor_geometry: reply[3].parse()?,
13        })
14    }
15}
16
17impl FromStr for MonitorRenameInfo {
18    type Err = ParseError;
19
20    fn from_str(input: &str) -> Result<Self, Self::Err> {
21        let reply = process_event_reply(input, "monitor_rename", 3)?;
22
23        Ok(Self {
24            monitor_id: from_hex_to_id(reply[1])?,
25            old_name: reply[2].to_string(),
26            new_name: reply[3].to_string(),
27        })
28    }
29}
30
31impl FromStr for MonitorRemoveInfo {
32    type Err = ParseError;
33
34    fn from_str(input: &str) -> Result<Self, Self::Err> {
35        let reply = process_event_reply(input, "monitor_remove", 1)?;
36
37        Ok(Self {
38            monitor_id: from_hex_to_id(reply[1])?,
39        })
40    }
41}
42
43impl FromStr for MonitorSwapInfo {
44    type Err = ParseError;
45
46    fn from_str(input: &str) -> Result<Self, Self::Err> {
47        let reply = process_event_reply(input, "monitor_swap", 2)?;
48
49        Ok(Self {
50            src_monitor_id: from_hex_to_id(reply[1])?,
51            dst_monitor_id: from_hex_to_id(reply[2])?,
52        })
53    }
54}
55
56impl FromStr for MonitorFocusInfo {
57    type Err = ParseError;
58
59    fn from_str(input: &str) -> Result<Self, Self::Err> {
60        let reply = process_event_reply(input, "monitor_focus", 1)?;
61
62        Ok(Self {
63            monitor_id: from_hex_to_id(reply[1])?,
64        })
65    }
66}
67
68impl FromStr for MonitorGeometryInfo {
69    type Err = ParseError;
70
71    fn from_str(input: &str) -> Result<Self, Self::Err> {
72        let reply = process_event_reply(input, "monitor_geometry", 2)?;
73
74        Ok(Self {
75            monitor_id: from_hex_to_id(reply[1])?,
76            monitor_geometry: reply[2].parse()?,
77        })
78    }
79}
80
81impl FromStr for MonitorEvent {
82    type Err = ParseError;
83
84    fn from_str(input: &str) -> Result<Self, Self::Err> {
85        let event_type = get_event_type(input)?;
86
87        match event_type {
88            "monitor_add" => Ok(MonitorEvent::MonitorAdd(input.parse()?)),
89            "monitor_rename" => Ok(MonitorEvent::MonitorRename(input.parse()?)),
90            "monitor_remove" => Ok(MonitorEvent::MonitorRemove(input.parse()?)),
91            "monitor_swap" => Ok(MonitorEvent::MonitorSwap(input.parse()?)),
92            "monitor_focus" => Ok(MonitorEvent::MonitorFocus(input.parse()?)),
93            "monitor_geometry" => {
94                Ok(MonitorEvent::MonitorGeometry(input.parse()?))
95            }
96
97            _ => Err(ParseError::ConversionFailed),
98        }
99    }
100}