summaryrefslogtreecommitdiff
path: root/src/i3bar_protocol.rs
blob: 6bc05336c3ea8332e6d97812b342fbf7acbab37b (plain) (blame)
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
use crate::color::Color;
use crate::pointer_btn::PointerBtn;
use crate::text::Align;
use serde::Deserialize;
use serde::Serialize;
use serde_json::{Deserializer, Result as SerdeResult};
use std::io::{Error, ErrorKind, Result};

#[derive(Deserialize, Default)]
pub struct Block {
    pub full_text: String,
    #[serde(default)]
    pub short_text: Option<String>,
    #[serde(default)]
    pub color: Option<Color>,
    #[serde(default)]
    pub background: Option<Color>,
    #[serde(default)]
    pub min_width: Option<MinWidth>,
    #[serde(default)]
    pub align: Option<Align>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub instance: Option<String>,
    #[serde(default = "def_sep")]
    pub separator: bool,
    #[serde(default = "def_sep_width")]
    pub separator_block_width: u8,
    #[serde(default)]
    pub markup: Option<String>,
}

fn def_sep() -> bool {
    true
}

fn def_sep_width() -> u8 {
    9
}

#[derive(Deserialize, Clone)]
#[serde(untagged)]
pub enum MinWidth {
    Text(String),
    Pixels(u32),
}

#[derive(Serialize, Default)]
pub struct Event<'a> {
    pub name: Option<&'a str>,
    pub instance: Option<&'a str>,
    pub button: PointerBtn,
    // Not available on wayland
    pub modifiers: Vec<()>,
    // I see no reason to have these in the protocol, as a lot depends on font & pango markup
    pub x: u8,
    pub y: u8,
    pub relative_x: u8,
    pub relative_y: u8,
    pub output_x: u8,
    pub output_y: u8,
    pub width: u8,
    pub height: u8,
}

#[derive(Deserialize, Clone, Copy)]
#[serde(deny_unknown_fields)]
pub struct JsonHeader {
    version: u8,
    #[serde(default)]
    #[allow(dead_code)]
    stop_signal: i32,
    #[serde(default)]
    #[allow(dead_code)]
    cont_signal: i32,
    #[serde(default)]
    click_events: bool,
}

pub enum Protocol {
    Unknown,
    PlainText(Option<String>),
    JsonNotStarted(JsonHeader),
    Json(JsonHeader, Option<Vec<Block>>, Option<String>),
}

impl Protocol {
    pub fn process_line(&mut self, line: String) -> Result<()> {
        macro_rules! invalid {
            ($fmt:expr) => {
                return Err(Error::new(ErrorKind::InvalidData, format!($fmt)))
            };
        }

        match self {
            Self::Unknown => {
                if let Ok(header) = serde_json::from_str::<JsonHeader>(&line) {
                    if header.version == 1 {
                        *self = Protocol::JsonNotStarted(header);
                        return Ok(());
                    }
                }
                *self = Self::PlainText(Some(line))
            }
            Self::PlainText(s) => *s = Some(line),
            Self::JsonNotStarted(header) => {
                let line = line.trim_start();
                if !line.is_empty() {
                    if !line.starts_with('[') {
                        invalid!("Expected '['");
                    }
                    *self = Self::Json(*header, None, None);
                    self.process_line(line[1..].to_owned())?;
                }
            }
            Self::Json(_, blocks, start_orig) => match start_orig {
                Some(start) => {
                    start.push_str(&line);
                    match de_last::<Vec<Block>>(start) {
                        Err(e) => invalid!("Invalid JSON: {e}"),
                        Ok((rem, new_blocks)) => {
                            *start_orig = (!rem.is_empty()).then(|| rem.to_owned());
                            if let Some(new_blocks) = new_blocks {
                                *blocks = Some(new_blocks);
                            }
                        }
                    }
                }
                None => match de_last::<Vec<Block>>(&line) {
                    Err(e) => invalid!("Invalid JSON: {e}"),
                    Ok((rem, new_blocks)) => {
                        *start_orig = (!rem.is_empty()).then(|| rem.to_owned());
                        if let Some(new_blocks) = new_blocks {
                            *blocks = Some(new_blocks);
                        }
                    }
                },
            },
        }
        Ok(())
    }

    pub fn get_blocks(&mut self) -> Option<Vec<Block>> {
        match self {
            Self::Unknown => None,
            Self::PlainText(text) => Some(vec![Block {
                full_text: text.take()?,
                ..Default::default()
            }]),
            Self::JsonNotStarted(_) => None,
            Self::Json(_, blocks, _) => blocks.take(),
        }
    }

    pub fn supports_clicks(&self) -> bool {
        match self {
            Self::JsonNotStarted(h) => h.click_events,
            Self::Json(h, _, _) => h.click_events,
            _ => false,
        }
    }
}

/// Deserialize the last complete object. Returns (`remaining`, `object`). See tests for examples.
fn de_last<'a, T: Deserialize<'a>>(mut s: &'a str) -> SerdeResult<(&'a str, Option<T>)> {
    let mut last = None;
    let mut tmp;
    loop {
        (s, tmp) = de_once(s)?;
        last = match tmp {
            Some(obj) => Some(obj),
            None => return Ok((s, last)),
        };
    }
}

/// Deserialize the first complete object. Returns (`remaining`, `object`).
fn de_once<'a, T: Deserialize<'a>>(s: &'a str) -> SerdeResult<(&'a str, Option<T>)> {
    let s = s.trim_start_matches(|x: char| x.is_ascii_whitespace() || x == ',');
    let mut de = Deserializer::from_str(s).into_iter();
    match de.next() {
        Some(Ok(obj)) => Ok((&s[de.byte_offset()..], Some(obj))),
        Some(Err(e)) if e.is_eof() => Ok((&s[de.byte_offset()..], None)),
        Some(Err(e)) => Err(e),
        None => Ok((&s[de.byte_offset()..], None)),
    }
}

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

    #[test]
    fn streaming_json() {
        let s = ",[2], [3], [4, 3],[32][3] ";
        assert_eq!(de_last::<Vec<u8>>(s).unwrap(), ("", Some(vec![3])));

        let s = ",[2], [3], [4, 3],[32][3] [2, 4";
        assert_eq!(de_last::<Vec<u8>>(s).unwrap(), ("[2, 4", Some(vec![3])));

        let s = ",[2], [3], [4, 3],[32] invalid";
        assert!(de_last::<Vec<u8>>(s).is_err());
    }
}