From 9200db526ab46e5748a11a77aadc905d9578f792 Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 11 Feb 2021 11:59:30 +0100 Subject: [PATCH] Don't use well-typed structs anymore This can lead to maintainance problems if `sway` decides to change a structure by only a field that we don't even use. This seems to already happen if you have an output declared in your `sway` configuration that is unplugged. --- Cargo.lock | 52 +------------------------------------------- Cargo.toml | 1 - src/main.rs | 62 +++++++---------------------------------------------- 3 files changed, 9 insertions(+), 106 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 884d903..d344ef2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,24 +12,6 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" -[[package]] -name = "proc-macro2" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" -dependencies = [ - "unicode-xid", -] - -[[package]] -name = "quote" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" -dependencies = [ - "proc-macro2", -] - [[package]] name = "ryu" version = "1.0.5" @@ -41,20 +23,6 @@ name = "serde" version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.118" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] [[package]] name = "serde_json" @@ -69,26 +37,8 @@ dependencies = [ [[package]] name = "swaysome" -version = "0.1.0" +version = "0.2.0" dependencies = [ "byteorder", - "serde", "serde_json", ] - -[[package]] -name = "syn" -version = "1.0.55" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a571a711dddd09019ccc628e1b17fe87c59b09d513c06c026877aa708334f37a" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - -[[package]] -name = "unicode-xid" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/Cargo.toml b/Cargo.toml index 4f360b1..927547c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,4 @@ edition = "2018" [dependencies] byteorder = "1" -serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src/main.rs b/src/main.rs index 63bcc16..84a3240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ // client.rs extern crate byteorder; -extern crate serde; extern crate serde_json; use std::env; @@ -11,8 +10,6 @@ use std::os::unix::net::UnixStream; use std::mem; use std::io::Cursor; -use serde::{Deserialize, Serialize}; - use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian}; const RUN_COMMAND: u32 = 0; @@ -20,49 +17,6 @@ const GET_WORKSPACES: u32 = 1; const SUBSCRIBE: u32 = 2; const GET_OUTPUTS: u32 = 3; -#[derive(Debug)] -#[derive(Serialize, Deserialize)] -struct WorkspaceRect { - x: usize, - y: usize, -} - -#[derive(Debug)] -#[derive(Serialize, Deserialize)] -struct Workspace { - num: usize, - name: String, - visible: bool, - focused: bool, - rect: WorkspaceRect, - output: String, -} - -#[derive(Debug)] -#[derive(Serialize, Deserialize)] -struct OutputMode { - width: usize, - height: usize, - refresh: usize, -} - -#[derive(Debug)] -#[derive(Serialize, Deserialize)] -struct Output { - name: String, - make: String, - model: String, - serial: String, - active: bool, - primary: bool, - focused: bool, - scale: f32, - subpixel_hinting: String, - transform: String, - current_workspace: String, - modes: Vec, - current_mode: OutputMode, -} fn get_stream() -> UnixStream { @@ -146,7 +100,7 @@ fn check_success(stream: &UnixStream) { }; } -fn get_outputs(stream: &UnixStream) -> Vec { +fn get_outputs(stream: &UnixStream) -> Vec { send_msg(&stream, GET_OUTPUTS, ""); let o = match read_msg(&stream) { Ok(msg) => msg, @@ -155,7 +109,7 @@ fn get_outputs(stream: &UnixStream) -> Vec { serde_json::from_str(&o).unwrap() } -fn get_workspaces(stream: &UnixStream) -> Vec { +fn get_workspaces(stream: &UnixStream) -> Vec { send_msg(&stream, GET_WORKSPACES, ""); let ws = match read_msg(&stream) { Ok(msg) => msg, @@ -167,7 +121,7 @@ fn get_workspaces(stream: &UnixStream) -> Vec { fn get_current_output_name(stream: &UnixStream) -> String { let outputs = get_outputs(&stream); - let focused_output_index = match outputs.iter().position(|x| x.focused) { + let focused_output_index = match outputs.iter().position(|x| x["focused"] == serde_json::Value::Bool(true)) { Some(i) => i, None => panic!("WTF! No focused output???"), }; @@ -205,7 +159,7 @@ fn move_container_to_prev_output(stream: &UnixStream) { fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool) { let outputs = get_outputs(&stream); - let focused_output_index = match outputs.iter().position(|x| x.focused) { + let focused_output_index = match outputs.iter().position(|x| x["focused"] == serde_json::Value::Bool(true)) { Some(i) => i, None => panic!("WTF! No focused output???"), }; @@ -219,18 +173,18 @@ fn move_container_to_next_or_prev_output(stream: &UnixStream, go_to_prev: bool) let workspaces = get_workspaces(&stream); let target_workspace = workspaces.iter() - .filter(|x| x.output == target_output.name && x.visible) + .filter(|x| x["output"] == target_output["name"] && x["visible"] == serde_json::Value::Bool(true)) .next().unwrap(); // Move container to target workspace let mut cmd: String = "move container to workspace ".to_string(); - cmd.push_str(&target_workspace.name); + cmd.push_str(&target_workspace["name"].as_str().unwrap()); send_msg(&stream, RUN_COMMAND, &cmd); check_success(&stream); // Focus that workspace to follow the container let mut cmd: String = "workspace ".to_string(); - cmd.push_str(&target_workspace.name); + cmd.push_str(&target_workspace["name"].as_str().unwrap()); send_msg(&stream, RUN_COMMAND, &cmd); check_success(&stream); } @@ -241,7 +195,7 @@ fn init_workspaces(stream: &UnixStream) { let cmd_prefix: String = "focus output ".to_string(); for output in outputs.iter().rev() { let mut cmd = cmd_prefix.clone(); - cmd.push_str(&output.name); + cmd.push_str(&output["name"].as_str().unwrap()); println!("Sending command: '{}'", &cmd); send_msg(&stream, RUN_COMMAND, &cmd); check_success(&stream);