bspc_rs/parser/parse_events/
node_events.rs

1use super::*;
2
3impl FromStr for NodeAddInfo {
4    type Err = ParseError;
5
6    fn from_str(input: &str) -> Result<Self, Self::Err> {
7        let reply = process_event_reply(input, "node_add", 4)?;
8
9        Ok(Self {
10            monitor_id: from_hex_to_id(reply[1])?,
11            desktop_id: from_hex_to_id(reply[2])?,
12            ip_id: from_hex_to_id(reply[3])?,
13            node_id: from_hex_to_id(reply[4])?,
14        })
15    }
16}
17
18impl FromStr for NodeRemoveInfo {
19    type Err = ParseError;
20
21    fn from_str(input: &str) -> Result<Self, Self::Err> {
22        let reply = process_event_reply(input, "node_remove", 3)?;
23
24        Ok(Self {
25            monitor_id: from_hex_to_id(reply[1])?,
26            desktop_id: from_hex_to_id(reply[2])?,
27            node_id: from_hex_to_id(reply[3])?,
28        })
29    }
30}
31
32impl FromStr for NodeSwapInfo {
33    type Err = ParseError;
34
35    fn from_str(input: &str) -> Result<Self, Self::Err> {
36        let reply = process_event_reply(input, "node_swap", 6)?;
37
38        Ok(Self {
39            src_monitor_id: from_hex_to_id(reply[1])?,
40            src_desktop_id: from_hex_to_id(reply[2])?,
41            src_node_id: from_hex_to_id(reply[3])?,
42            dst_monitor_id: from_hex_to_id(reply[4])?,
43            dst_desktop_id: from_hex_to_id(reply[5])?,
44            dst_node_id: from_hex_to_id(reply[6])?,
45        })
46    }
47}
48
49impl FromStr for NodeTransferInfo {
50    type Err = ParseError;
51
52    fn from_str(input: &str) -> Result<Self, Self::Err> {
53        let reply = process_event_reply(input, "node_transfer", 6)?;
54
55        Ok(Self {
56            src_monitor_id: from_hex_to_id(reply[1])?,
57            src_desktop_id: from_hex_to_id(reply[2])?,
58            src_node_id: from_hex_to_id(reply[3])?,
59            dst_monitor_id: from_hex_to_id(reply[4])?,
60            dst_desktop_id: from_hex_to_id(reply[5])?,
61            dst_node_id: from_hex_to_id(reply[6])?,
62        })
63    }
64}
65
66impl FromStr for NodeFocusInfo {
67    type Err = ParseError;
68
69    fn from_str(input: &str) -> Result<Self, Self::Err> {
70        let reply = process_event_reply(input, "node_focus", 3)?;
71
72        Ok(Self {
73            monitor_id: from_hex_to_id(reply[1])?,
74            desktop_id: from_hex_to_id(reply[2])?,
75            node_id: from_hex_to_id(reply[3])?,
76        })
77    }
78}
79
80impl FromStr for NodeActivateInfo {
81    type Err = ParseError;
82
83    fn from_str(input: &str) -> Result<Self, Self::Err> {
84        let reply = process_event_reply(input, "node_activate", 3)?;
85
86        Ok(Self {
87            monitor_id: from_hex_to_id(reply[1])?,
88            desktop_id: from_hex_to_id(reply[2])?,
89            node_id: from_hex_to_id(reply[3])?,
90        })
91    }
92}
93
94impl FromStr for NodePreselInfo {
95    type Err = ParseError;
96
97    fn from_str(input: &str) -> Result<Self, Self::Err> {
98        let reply = process_event_reply(input, "node_presel", 4)?;
99
100        Ok(Self {
101            monitor_id: from_hex_to_id(reply[1])?,
102            desktop_id: from_hex_to_id(reply[2])?,
103            node_id: from_hex_to_id(reply[3])?,
104            presel: reply[4].parse()?,
105        })
106    }
107}
108
109impl FromStr for NodeStackInfo {
110    type Err = ParseError;
111
112    fn from_str(input: &str) -> Result<Self, Self::Err> {
113        let reply = process_event_reply(input, "node_stack", 3)?;
114
115        Ok(Self {
116            node_id_1: from_hex_to_id(reply[1])?,
117            stack: reply[2].parse()?,
118            node_id_2: from_hex_to_id(reply[3])?,
119        })
120    }
121}
122
123impl FromStr for NodeLayerInfo {
124    type Err = ParseError;
125
126    fn from_str(input: &str) -> Result<Self, Self::Err> {
127        let reply = process_event_reply(input, "node_layer", 4)?;
128
129        Ok(Self {
130            monitor_id: from_hex_to_id(reply[1])?,
131            desktop_id: from_hex_to_id(reply[2])?,
132            node_id: from_hex_to_id(reply[3])?,
133            layer: reply[4].parse()?,
134        })
135    }
136}
137
138impl FromStr for NodeFlagInfo {
139    type Err = ParseError;
140
141    fn from_str(input: &str) -> Result<Self, Self::Err> {
142        let reply = process_event_reply(input, "node_flag", 5)?;
143
144        Ok(Self {
145            monitor_id: from_hex_to_id(reply[1])?,
146            desktop_id: from_hex_to_id(reply[2])?,
147            node_id: from_hex_to_id(reply[3])?,
148            flag: reply[4].parse()?,
149            switch: reply[5].parse()?,
150        })
151    }
152}
153
154impl FromStr for NodeStateInfo {
155    type Err = ParseError;
156
157    fn from_str(input: &str) -> Result<Self, Self::Err> {
158        let reply = process_event_reply(input, "node_state", 5)?;
159
160        Ok(Self {
161            monitor_id: from_hex_to_id(reply[1])?,
162            desktop_id: from_hex_to_id(reply[2])?,
163            node_id: from_hex_to_id(reply[3])?,
164            state: reply[4].parse()?,
165            switch: reply[5].parse()?,
166        })
167    }
168}
169
170impl FromStr for NodeGeometryInfo {
171    type Err = ParseError;
172
173    fn from_str(input: &str) -> Result<Self, Self::Err> {
174        let reply = process_event_reply(input, "node_geometry", 4)?;
175
176        Ok(Self {
177            monitor_id: from_hex_to_id(reply[1])?,
178            desktop_id: from_hex_to_id(reply[2])?,
179            node_id: from_hex_to_id(reply[3])?,
180            node_geometry: reply[4].parse()?,
181        })
182    }
183}
184
185impl FromStr for NodeEvent {
186    type Err = ParseError;
187
188    fn from_str(input: &str) -> Result<Self, Self::Err> {
189        let event_type = get_event_type(input)?;
190
191        match event_type {
192            "node_add" => Ok(NodeEvent::NodeAdd(input.parse()?)),
193            "node_remove" => Ok(NodeEvent::NodeRemove(input.parse()?)),
194            "node_swap" => Ok(NodeEvent::NodeSwap(input.parse()?)),
195            "node_transfer" => Ok(NodeEvent::NodeTransfer(input.parse()?)),
196            "node_focus" => Ok(NodeEvent::NodeFocus(input.parse()?)),
197            "node_activate" => Ok(NodeEvent::NodeActivate(input.parse()?)),
198            "node_presel" => Ok(NodeEvent::NodePresel(input.parse()?)),
199            "node_stack" => Ok(NodeEvent::NodeStack(input.parse()?)),
200            "node_geometry" => Ok(NodeEvent::NodeGeometry(input.parse()?)),
201            "node_state" => Ok(NodeEvent::NodeState(input.parse()?)),
202            "node_flag" => Ok(NodeEvent::NodeFlag(input.parse()?)),
203            "node_layer" => Ok(NodeEvent::NodeLayer(input.parse()?)),
204            _ => Err(ParseError::ConversionFailed),
205        }
206    }
207}
208
209#[cfg(test)]
210mod test {
211    use super::*;
212
213    #[test]
214    fn parse_node_add() {
215        let input = "node_add 0x00200002 0x0020000D 0x05200002 0x04E00002";
216        let node_add_info: NodeAddInfo = input.parse().unwrap();
217
218        println!("{node_add_info:#?}");
219    }
220    #[test]
221    fn parse_node_focus() {
222        let input = "node_focus 0x00200002 0x00200007 0x07800002";
223        let node_focus_info: NodeFocusInfo = input.parse().unwrap();
224
225        println!("{node_focus_info:#?}");
226    }
227    #[test]
228    fn parse_node_stack() {
229        let input = "node_stack 0x07800002 below 0x04200003";
230        let node_stack_info: NodeStackInfo = input.parse().unwrap();
231
232        println!("{node_stack_info:#?}");
233    }
234    #[test]
235    fn parse_node_geometry() {
236        let input =
237            "node_geometry 0x00200002 0x00200007 0x07800002 681x365+0+403\n";
238        let node_geometry_info: NodeGeometryInfo = input.parse().unwrap();
239
240        println!("{node_geometry_info:#?}");
241    }
242
243    #[test]
244    fn parse_node_event() {
245        let tests = [
246            "node_state 0x00200002 0x00200007 0x04400002 tiled on",
247            "node_add 0x00200002 0x00200007 0x04400002 0x08600002",
248            "node_geometry 0x00200002 0x00200007 0x04400002 677x361+0+35",
249            "node_stack 0x04400002 below 0x04200003",
250            "node_state 0x00200002 0x00200007 0x04400002 fullscreen off",
251        ];
252
253        for test in tests {
254            let data: NodeEvent = test.parse().unwrap();
255
256            println!("{data:#?}")
257        }
258    }
259}