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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Contains structs for deserializing messages from i3
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use std::collections::HashMap;

/// Generic success reply
#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Hash, Debug)]
pub struct Success {
    pub success: bool,
    pub error: Option<String>,
}

/// Workspaces reply
pub type Workspaces = Vec<Workspace>;

#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Hash, Debug)]
pub struct Workspace {
    #[serde(default)]
    pub id: usize,
    pub num: usize,
    pub name: String,
    pub visible: bool,
    pub focused: bool,
    // used in sway, TODO: put behind feature flag
    pub urgent: bool,
    pub rect: Rect,
    pub output: String,
    #[cfg(feature = "sway")]
    pub focus: Vec<usize>,
}

/// Outputs reply
pub type Outputs = Vec<Output>;

#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Hash, Debug)]
pub struct Output {
    pub name: String,
    pub active: bool,
    pub primary: bool,
    pub current_workspace: Option<String>,
    pub rect: Rect,
}

/// Tree/Node reply
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Node {
    pub id: usize,
    pub name: Option<String>,
    pub num: Option<i32>,
    #[serde(rename = "type")]
    pub node_type: NodeType,
    pub layout: NodeLayout,
    pub output: Option<String>,
    pub orientation: NodeOrientation,
    pub border: NodeBorder,
    pub percent: Option<f64>,
    pub rect: Rect,
    pub window_rect: Rect,
    pub deco_rect: Rect,
    pub geometry: Rect,
    pub window: Option<u32>,
    pub window_properties: Option<WindowProperties>,
    pub window_type: Option<WindowType>,
    pub current_border_width: i32,
    pub urgent: bool,
    pub marks: Option<Marks>,
    pub focused: bool,
    pub focus: Vec<usize>,
    pub sticky: bool,
    pub floating: Option<Floating>,
    pub floating_nodes: Vec<Node>,
    pub fullscreen_mode: FullscreenMode,
    pub nodes: Vec<Node>,
    #[cfg(feature = "sway")]
    pub app_id: Option<String>,
}

impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Node {}

#[derive(Eq, Serialize, PartialEq, Clone, Debug)]
pub struct WindowProperties {
    pub title: Option<String>,
    pub instance: Option<String>,
    pub class: Option<String>,
    pub window_role: Option<String>,
    pub transient_for: Option<u64>,
    #[cfg(feature = "sway")]
    pub window_type: Option<String>,
}

impl<'de> serde::Deserialize<'de> for WindowProperties {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize, Serialize, PartialEq, Clone, Debug)]
        struct Intermediate(HashMap<WindowProperty, Option<WindowData>>);

        #[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
        #[serde(untagged)]
        enum WindowData {
            Str(String),
            Num(u64),
        }
        impl WindowData {
            fn unwrap_str(self) -> String {
                match self {
                    WindowData::Str(s) => s,
                    _ => unreachable!("cant have non-string value"),
                }
            }

            fn unwrap_num(self) -> u64 {
                match self {
                    WindowData::Num(n) => n,
                    _ => unreachable!("cant have non-num value"),
                }
            }
        }
        let mut input = Intermediate::deserialize(deserializer)?;
        let title = input
            .0
            .get_mut(&WindowProperty::Title)
            .and_then(|x| x.take().map(|x| x.unwrap_str()));
        let instance = input
            .0
            .get_mut(&WindowProperty::Instance)
            .and_then(|x| x.take().map(|x| x.unwrap_str()));
        let class = input
            .0
            .get_mut(&WindowProperty::Class)
            .and_then(|x| x.take().map(|x| x.unwrap_str()));
        let window_role = input
            .0
            .get_mut(&WindowProperty::WindowRole)
            .and_then(|x| x.take().map(|x| x.unwrap_str()));
        let transient_for = input
            .0
            .get_mut(&WindowProperty::TransientFor)
            .and_then(|x| x.take().map(|x| x.unwrap_num()));
        #[cfg(feature = "sway")]
        let window_type = input
            .0
            .get_mut(&WindowProperty::WindowType)
            .and_then(|x| x.take().map(|x| x.unwrap_str()));

        Ok(WindowProperties {
            title,
            instance,
            class,
            window_role,
            transient_for,
            #[cfg(feature = "sway")]
            window_type,
        })
    }
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Copy, Clone, Hash, Debug)]
#[serde(rename_all = "snake_case")]
pub enum Floating {
    AutoOff,
    AutoOn,
    UserOff,
    UserOn,
}

#[derive(Deserialize_repr, Serialize_repr, Eq, PartialEq, Copy, Clone, Hash, Debug)]
#[repr(u8)]
pub enum FullscreenMode {
    None = 0,
    Output = 1,
    Global = 2,
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Copy, Hash, Debug)]
#[serde(rename_all = "snake_case")]
pub enum WindowProperty {
    Title,
    Instance,
    Class,
    WindowRole,
    TransientFor,
    #[cfg(feature = "sway")]
    WindowType,
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Copy, Hash, Debug)]
#[serde(rename_all = "snake_case")]
pub enum WindowType {
    Normal,
    Dock,
    Dialog,
    Utility,
    Toolbar,
    Splash,
    Menu,
    DropdownMenu,
    PopupMenu,
    Tooltip,
    Notification,
    Unknown,
}

#[cfg(feature = "sway")]
#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Hash, Debug)]
pub struct Rect {
    pub x: isize,
    pub y: isize,
    pub width: isize,
    pub height: isize,
}

#[cfg(not(feature = "sway"))]
#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Hash, Debug)]
pub struct Rect {
    pub x: usize,
    pub y: usize,
    pub width: usize,
    pub height: usize,
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Hash, Debug, Copy)]
#[serde(rename_all = "snake_case")]
pub enum NodeType {
    Root,
    Output,
    Con,
    FloatingCon,
    Workspace,
    Dockarea,
}
#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
#[serde(rename_all = "lowercase")]
pub enum NodeBorder {
    Normal,
    None,
    Pixel,
    #[cfg(feature = "sway")]
    CSD,
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum NodeLayout {
    SplitH,
    SplitV,
    Stacked,
    Tabbed,
    Dockarea,
    Output,
    #[cfg(feature = "sway")]
    None,
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum NodeOrientation {
    Horizontal,
    Vertical,
    None,
}

/// Marks Reply
#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
pub struct Marks(Vec<String>);

/// BarIds
#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
pub struct BarIds(Vec<String>);

/// BarConfig Reply
#[derive(Deserialize, Serialize, Eq, PartialEq, Debug, Clone)]
pub struct BarConfig {
    pub id: String,
    pub mode: String,
    pub position: String,
    pub status_command: String,
    pub font: String,
    pub workspace_buttons: bool,
    pub binding_mode_indicator: bool,
    pub verbose: bool,
    pub colors: HashMap<BarPart, String>,
}

#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum BarPart {
    Background,
    Statusline,
    Separator,
    FocusedBackground,
    FocusedStatusline,
    FocusedSeparator,
    FocusedWorkspaceText,
    FocusedWorkspaceBg,
    FocusedWorkspaceBorder,
    ActiveWorkspaceText,
    ActiveWorkspaceBg,
    ActiveWorkspaceBorder,
    InactiveWorkspaceText,
    InactiveWorkspaceBg,
    InactiveWorkspaceBorder,
    UrgentWorkspaceText,
    UrgentWorkspaceBg,
    UrgentWorkspaceBorder,
    BindingModeText,
    BindingModeBg,
    BindingModeBorder,
}

/// Version reply
#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
pub struct Version {
    pub major: usize,
    pub minor: usize,
    pub patch: usize,
    pub human_readable: String,
    pub loaded_config_file_name: String,
}

/// Binding Modes Reply
#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
pub struct BindingModes(Vec<String>);

/// Config Reply
#[derive(Deserialize, Serialize, Eq, PartialEq, Hash, Debug, Clone)]
pub struct Config {
    pub config: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_output() {
        let output = "{\"name\":\"xroot-0\",\"active\":false,\"primary\":false,\"rect\":{\"x\":0,\"y\":0,\"width\":5120,\"height\":1600},\"current_workspace\":null}";
        let o: Result<Output, serde_json::error::Error> = serde_json::from_str(output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_success() {
        let output = "{\"success\":true}";
        let o: Result<Success, serde_json::error::Error> = serde_json::from_str(output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_workspace() {
        let output = "{\"id\":1,\"num\":2,\"name\":\"2\",\"visible\":false,\"focused\":false,\"rect\":{\"x\":2560,\"y\":29,\"width\":2560,\"height\":1571},\"output\":\"DVI-I-3\",\"urgent\":false}";
        let o: Result<Workspace, serde_json::error::Error> = serde_json::from_str(output);
        dbg!(&o);
        assert!(o.is_ok());
    }

    #[test]
    fn test_workspace_no_id() {
        let output = "{\"num\":2,\"name\":\"2\",\"visible\":false,\"focused\":false,\"rect\":{\"x\":2560,\"y\":29,\"width\":2560,\"height\":1571},\"output\":\"DVI-I-3\",\"urgent\":false}";
        let o: Result<Workspace, serde_json::error::Error> = serde_json::from_str(output);
        dbg!(&o);
        assert!(o.is_ok());
        assert_eq!(o.unwrap().id, 0);
    }

    #[test]
    fn test_binding_modes() {
        let output = "[\"resize\",\"default\"]";
        let o: Result<BindingModes, serde_json::error::Error> = serde_json::from_str(output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_config() {
        let output = "{\"config\": \"some config data here\"}";
        let o: Result<Config, serde_json::error::Error> = serde_json::from_str(output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_tree() {
        let output = include_str!("../test/tree.json");
        let o: Result<Node, serde_json::error::Error> = serde_json::from_str(&output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_other_tree() {
        let output = include_str!("../test/other_tree.json");
        let o: Result<Node, serde_json::error::Error> = serde_json::from_str(&output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_last_tree() {
        let output = include_str!("../test/last_tree.json");
        let o: Result<Node, serde_json::error::Error> = serde_json::from_str(&output);
        assert!(o.is_ok());
    }

    #[test]
    fn test_version() {
        let output = include_str!("../test/version.json");
        let o: Result<Version, serde_json::error::Error> = serde_json::from_str(&output);
        assert!(o.is_ok());
    }
}