Skip to content

Commit 2a5e189

Browse files
committed
Forward compatibility: Replace unreachable!() with Unknown enums
1 parent fd8d754 commit 2a5e189

File tree

6 files changed

+69
-21
lines changed

6 files changed

+69
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ repository = "https://github.com/tmerr/i3ipc-rs"
99
[dependencies]
1010
unix_socket = "0.4.5"
1111
byteorder = "0.3"
12+
log = "0.3.7"
1213
serde = "0.6.0"
1314
serde_json = "0.6.0"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ fn main() {
5959
}
6060
```
6161

62-
## Status
62+
## Compatibility
6363

64-
This library was last updated for **i3 version 4.11**. There is an open ticket concerning forward compatibility: https://github.com/tmerr/i3ipc-rs/issues/8. Contributions are welcome!
64+
This library was last updated for **i3 version 4.11**, but is forward compatible. Contributions are welcome!
65+
66+
All documented functionality from i3 version 4.11 can be handled by this library. Additions to the i3 IPC interface that are not understood by this library will generally return an `Unknown` value and log a warning to the target `"i3ipc"` using the [log crate](http://doc.rust-lang.org/log). Binaries using this library should [install a logger](https://doc.rust-lang.org/log/log/index.html#in-executables) to view details of such additions.

src/common.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ pub fn build_tree(val: &json::Value) -> reply::Node {
2929
"floating_con" => reply::NodeType::FloatingCon,
3030
"workspace" => reply::NodeType::Workspace,
3131
"dockarea" => reply::NodeType::DockArea,
32-
_ => unreachable!()
32+
other => {
33+
warn!(target: "i3ipc", "Unknown NodeType {}", other);
34+
reply::NodeType::Unknown
35+
}
3336
},
3437
border: match val.find("border").unwrap().as_string().unwrap().as_ref() {
3538
"normal" => reply::NodeBorder::Normal,
3639
"none" => reply::NodeBorder::None,
3740
"pixel" => reply::NodeBorder::Pixel,
38-
_ => unreachable!()
41+
other => {
42+
warn!(target: "i3ipc", "Unknown NodeBorder {}", other);
43+
reply::NodeBorder::Unknown
44+
}
3945
},
4046
current_border_width: val.find("current_border_width").unwrap().as_i64().unwrap() as i32,
4147
layout: match val.find("layout").unwrap().as_string().unwrap().as_ref() {
@@ -45,7 +51,10 @@ pub fn build_tree(val: &json::Value) -> reply::Node {
4551
"tabbed" => reply::NodeLayout::Tabbed,
4652
"dockarea" => reply::NodeLayout::DockArea,
4753
"output" => reply::NodeLayout::Output,
48-
_ => unreachable!()
54+
other => {
55+
warn!(target: "i3ipc", "Unknown NodeLayout {}", other);
56+
reply::NodeLayout::Unknown
57+
}
4958
},
5059
percent: match *val.find("percent").unwrap() {
5160
json::Value::F64(f) => Some(f),
@@ -108,7 +117,10 @@ pub fn build_bar_config(j: &json::Value) -> reply::BarConfig {
108117
"binding_mode_text" => reply::ColorableBarPart::BindingModeText,
109118
"binding_mode_bg" => reply::ColorableBarPart::BindingModeBg,
110119
"binding_mode_border" => reply::ColorableBarPart::BindingModeBorder,
111-
other => reply::ColorableBarPart::Undocumented(other.to_owned())
120+
other => {
121+
warn!(target: "i3ipc", "Unknown ColorableBarPart {}", other);
122+
reply::ColorableBarPart::Unknown
123+
}
112124
};
113125
let hex = colors.get(c).unwrap().as_string().unwrap().to_owned();
114126
map.insert(enum_key, hex);

src/event.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ impl FromStr for WorkspaceEventInfo {
4141
"init" => WorkspaceChange::Init,
4242
"empty" => WorkspaceChange::Empty,
4343
"urgent" => WorkspaceChange::Urgent,
44-
_ => unreachable!()
44+
other => {
45+
warn!(target: "i3ipc", "Unknown WorkspaceChange {}", other);
46+
WorkspaceChange::Unknown
47+
}
4548
},
4649
current: match val.find("current").unwrap().clone() {
4750
json::Value::Null => None,
@@ -72,7 +75,10 @@ impl FromStr for OutputEventInfo {
7275
Ok(OutputEventInfo {
7376
change: match val.find("change").unwrap().as_string().unwrap().as_ref() {
7477
"unspecified" => OutputChange::Unspecified,
75-
_ => unreachable!()
78+
other => {
79+
warn!(target: "i3ipc", "Unknown OutputChange {}", other);
80+
OutputChange::Unknown
81+
}
7682
}
7783
})
7884
}
@@ -121,7 +127,10 @@ impl FromStr for WindowEventInfo {
121127
"move" => WindowChange::Move,
122128
"floating" => WindowChange::Floating,
123129
"urgent" => WindowChange::Urgent,
124-
_ => unreachable!()
130+
other => {
131+
warn!(target: "i3ipc", "Unknown WindowChange {}", other);
132+
WindowChange::Unknown
133+
}
125134
},
126135
container: common::build_tree(val.find("container").unwrap())
127136
})
@@ -164,7 +173,10 @@ impl FromStr for BindingEventInfo {
164173
Ok(BindingEventInfo {
165174
change: match val.find("change").unwrap().as_string().unwrap().as_ref() {
166175
"run" => BindingChange::Run,
167-
_ => unreachable!()
176+
other => {
177+
warn!(target: "i3ipc", "Unknown BindingChange {}", other);
178+
BindingChange::Unknown
179+
}
168180
},
169181
binding: Binding {
170182
command: bind.find("command").unwrap().as_string().unwrap().to_owned(),
@@ -181,7 +193,10 @@ impl FromStr for BindingEventInfo {
181193
input_type: match bind.find("input_type").unwrap().as_string().unwrap().as_ref() {
182194
"keyboard" => InputType::Keyboard,
183195
"mouse" => InputType::Mouse,
184-
_ => unreachable!()
196+
other => {
197+
warn!(target: "i3ipc", "Unknown InputType {}", other);
198+
InputType::Unknown
199+
}
185200
}
186201
}
187202
})
@@ -196,13 +211,17 @@ pub mod inner {
196211
Focus,
197212
Init,
198213
Empty,
199-
Urgent
214+
Urgent,
215+
/// A WorkspaceChange we don't support yet.
216+
Unknown,
200217
}
201218

202219
/// The kind of output change.
203220
#[derive(Debug)]
204221
pub enum OutputChange {
205-
Unspecified
222+
Unspecified,
223+
/// An OutputChange we don't support yet.
224+
Unknown,
206225
}
207226

208227
/// The kind of window change.
@@ -223,14 +242,18 @@ pub mod inner {
223242
/// The window has transitioned to or from floating.
224243
Floating,
225244
/// The window has become urgent or lost its urgent status.
226-
Urgent
245+
Urgent,
246+
/// A WindowChange we don't support yet.
247+
Unknown,
227248
}
228249

229250
/// Either keyboard or mouse.
230251
#[derive(Debug)]
231252
pub enum InputType {
232253
Keyboard,
233-
Mouse
254+
Mouse,
255+
/// An InputType we don't support yet.
256+
Unknown,
234257
}
235258

236259
/// Contains details about the binding that was run.
@@ -258,6 +281,8 @@ pub mod inner {
258281
/// The kind of binding change.
259282
#[derive(Debug)]
260283
pub enum BindingChange {
261-
Run
284+
Run,
285+
/// A BindingChange we don't support yet.
286+
Unknown,
262287
}
263288
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
1818
extern crate unix_socket;
1919
extern crate byteorder;
20+
#[macro_use]
21+
extern crate log;
2022
extern crate serde;
2123
extern crate serde_json;
2224

src/reply.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,18 @@ pub enum NodeType {
8787
Con,
8888
FloatingCon,
8989
Workspace,
90-
DockArea
90+
DockArea,
91+
/// A NodeType we don't support yet.
92+
Unknown,
9193
}
9294

9395
#[derive(Debug)]
9496
pub enum NodeBorder {
9597
Normal,
9698
None,
97-
Pixel
99+
Pixel,
100+
/// A NodeBorder we don't support yet.
101+
Unknown,
98102
}
99103

100104
#[derive(Debug)]
@@ -104,7 +108,9 @@ pub enum NodeLayout {
104108
Stacked,
105109
Tabbed,
106110
DockArea,
107-
Output
111+
Output,
112+
/// A NodeLayout we don't support yet.
113+
Unknown,
108114
}
109115

110116
/// The reply to the `get_tree` request.
@@ -265,8 +271,8 @@ pub enum ColorableBarPart {
265271
/// Border color for the binding mode indicator.
266272
BindingModeBorder,
267273

268-
/// A colorable bar part that was not documented. (The String is its name).
269-
Undocumented(String)
274+
/// A ColorableBarPart we don't support yet.
275+
Unknown,
270276
}
271277

272278
/// The reply to the `get_bar_config` request.

0 commit comments

Comments
 (0)