bspc_rs/parser/parse_events/
desktop_events.rs

1use super::*;
2
3impl FromStr for DesktopAddInfo {
4    type Err = ParseError;
5
6    fn from_str(input: &str) -> Result<Self, Self::Err> {
7        let reply = process_event_reply(input, "desktop_add", 3)?;
8
9        Ok(Self {
10            monitor_id: from_hex_to_id(reply[1])?,
11            desktop_id: from_hex_to_id(reply[2])?,
12            desktop_name: reply[3].to_string(),
13        })
14    }
15}
16
17impl FromStr for DesktopRenameInfo {
18    type Err = ParseError;
19
20    fn from_str(input: &str) -> Result<Self, Self::Err> {
21        let reply = process_event_reply(input, "desktop_rename", 4)?;
22
23        Ok(Self {
24            monitor_id: from_hex_to_id(reply[1])?,
25            desktop_id: from_hex_to_id(reply[2])?,
26            old_name: reply[3].to_string(),
27            new_name: reply[4].to_string(),
28        })
29    }
30}
31
32impl FromStr for DesktopRemoveInfo {
33    type Err = ParseError;
34
35    fn from_str(input: &str) -> Result<Self, Self::Err> {
36        let reply = process_event_reply(input, "desktop_remove", 2)?;
37
38        Ok(Self {
39            monitor_id: from_hex_to_id(reply[1])?,
40            desktop_id: from_hex_to_id(reply[2])?,
41        })
42    }
43}
44
45impl FromStr for DesktopSwapInfo {
46    type Err = ParseError;
47
48    fn from_str(input: &str) -> Result<Self, Self::Err> {
49        let reply = process_event_reply(input, "desktop_swap", 4)?;
50
51        Ok(Self {
52            src_monitor_id: from_hex_to_id(reply[1])?,
53            src_desktop_id: from_hex_to_id(reply[2])?,
54            dst_monitor_id: from_hex_to_id(reply[3])?,
55            dst_desktop_id: from_hex_to_id(reply[4])?,
56        })
57    }
58}
59
60impl FromStr for DesktopTransferInfo {
61    type Err = ParseError;
62
63    fn from_str(input: &str) -> Result<Self, Self::Err> {
64        let reply = process_event_reply(input, "desktop_transfer", 3)?;
65
66        Ok(Self {
67            src_monitor_id: from_hex_to_id(reply[1])?,
68            src_desktop_id: from_hex_to_id(reply[2])?,
69            dst_monitor_id: from_hex_to_id(reply[3])?,
70        })
71    }
72}
73
74impl FromStr for DesktopFocusInfo {
75    type Err = ParseError;
76
77    fn from_str(input: &str) -> Result<Self, Self::Err> {
78        let reply = process_event_reply(input, "desktop_focus", 2)?;
79
80        Ok(Self {
81            monitor_id: from_hex_to_id(reply[1])?,
82            desktop_id: from_hex_to_id(reply[2])?,
83        })
84    }
85}
86
87impl FromStr for DesktopActivateInfo {
88    type Err = ParseError;
89
90    fn from_str(input: &str) -> Result<Self, Self::Err> {
91        let reply = process_event_reply(input, "desktop_activate", 2)?;
92
93        Ok(Self {
94            monitor_id: from_hex_to_id(reply[1])?,
95            desktop_id: from_hex_to_id(reply[2])?,
96        })
97    }
98}
99
100impl FromStr for DesktopLayoutInfo {
101    type Err = ParseError;
102
103    fn from_str(input: &str) -> Result<Self, Self::Err> {
104        let reply = process_event_reply(input, "desktop_layout", 2)?;
105
106        Ok(Self {
107            monitor_id: from_hex_to_id(reply[1])?,
108            desktop_id: from_hex_to_id(reply[2])?,
109            layout: reply[3].parse()?,
110        })
111    }
112}
113
114impl FromStr for DesktopEvent {
115    type Err = ParseError;
116
117    fn from_str(input: &str) -> Result<Self, Self::Err> {
118        let event_type = get_event_type(input)?;
119
120        match event_type {
121            "desktop_add" => Ok(DesktopEvent::DesktopAdd(input.parse()?)),
122            "desktop_rename" => Ok(DesktopEvent::DesktopRename(input.parse()?)),
123            "desktop_remove" => Ok(DesktopEvent::DesktopRemove(input.parse()?)),
124            "desktop_swap" => Ok(DesktopEvent::DesktopSwap(input.parse()?)),
125            "desktop_transfer" => {
126                Ok(DesktopEvent::DesktopTransfer(input.parse()?))
127            }
128            "desktop_focus" => Ok(DesktopEvent::DesktopFocus(input.parse()?)),
129            "desktop_activate" => {
130                Ok(DesktopEvent::DesktopActivate(input.parse()?))
131            }
132            "desktop_layout" => Ok(DesktopEvent::DesktopLayout(input.parse()?)),
133            _ => Err(ParseError::ConversionFailed),
134        }
135    }
136}