From 103aa3bc384ac70effdf96eab151f1cb245293a2 Mon Sep 17 00:00:00 2001 From: Max Verevkin Date: Fri, 6 May 2022 15:35:37 +0300 Subject: [PATCH] use serde_json for serialization --- Cargo.lock | 100 +++++++++++++++++++++++++------- Cargo.toml | 4 +- src/main.rs | 162 ++++++++++++++++++++++------------------------------ 3 files changed, 148 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7370ed0..3bb0f33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,15 +10,15 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cc" -version = "1.0.67" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cfg-if" @@ -33,10 +33,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] -name = "libc" -version = "0.2.119" +name = "itoa" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[package]] +name = "libc" +version = "0.2.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" [[package]] name = "memoffset" @@ -62,30 +68,30 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.7.2" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" [[package]] name = "pkg-config" -version = "0.3.19" +version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.9" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" dependencies = [ "proc-macro2", ] @@ -94,6 +100,8 @@ dependencies = [ name = "ristate" version = "0.1.0" dependencies = [ + "serde", + "serde_json", "wayland-client", "wayland-commons", "wayland-protocols", @@ -101,16 +109,64 @@ dependencies = [ ] [[package]] -name = "smallvec" -version = "1.6.1" +name = "ryu" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "smallvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" + +[[package]] +name = "syn" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] [[package]] name = "unicode-xid" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" [[package]] name = "wayland-client" @@ -172,6 +228,6 @@ dependencies = [ [[package]] name = "xml-rs" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a" +checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" diff --git a/Cargo.toml b/Cargo.toml index aabe794..c07264a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,6 @@ version = "0.1.0" authors = ["Bryan Ndjeutcha "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [build-dependencies] wayland-scanner = "0.29.3" @@ -13,3 +11,5 @@ wayland-scanner = "0.29.3" wayland-protocols = "0.29.3" wayland-commons = "0.29.3" wayland-client = "0.29.3" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/src/main.rs b/src/main.rs index 339eaf6..d316b8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ mod wayland; use crate::wayland::river_status_unstable_v1::{ zriver_output_status_v1, zriver_seat_status_v1, zriver_status_manager_v1::ZriverStatusManagerV1, }; +use serde::ser::{SerializeSeq, Serializer}; +use serde::Serialize; use std::collections::BTreeMap; use wayland_client::protocol::{wl_output, wl_output::WlOutput, wl_seat, wl_seat::WlSeat}; use wayland_client::{Display, GlobalManager, Main}; @@ -30,83 +32,44 @@ impl Flags { } } -#[derive(Debug)] +struct Tags(u32); + +#[derive(Serialize)] struct Env { + #[serde(skip)] flags: Flags, + #[serde(skip_serializing_if = "Option::is_none")] title: Option, - tags: BTreeMap, - urgency: BTreeMap, - viewstag: BTreeMap>, + #[serde(skip_serializing_if = "Option::is_none")] + tags: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + urgency: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + viewstag: Option>>, + #[serde(skip)] status_manager: Option>, } impl Env { fn new() -> Env { + let flags = configuration(); Env { title: None, - flags: configuration(), - viewstag: BTreeMap::new(), - urgency: BTreeMap::new(), - tags: BTreeMap::new(), + tags: flags.tags.then(BTreeMap::new), + urgency: flags.urgency.then(BTreeMap::new), + viewstag: flags.viewstag.then(BTreeMap::new), status_manager: None, + flags, } } + fn fmt(&self) { - if !self.tags.is_empty() - || !self.viewstag.is_empty() - || !self.urgency.is_empty() - || self.title.is_some() { - print!("{{"); - let mut comma = false; - if !self.tags.is_empty() { - print!("\"tags\" : ["); - let len = self.tags.len(); - for (i, (key, tags)) in self.tags.iter().enumerate() { - print!("{{{:?} : ", key); - print!("["); - fmt_tags(*tags); - print!("]}}"); - if i < len - 1 { print!(", "); } - } - print!("]"); - comma = true; - } - if !self.urgency.is_empty() { - print!("\"urgent\" : ["); - let len = self.urgency.len(); - for (i, (key, tags)) in self.urgency.iter().enumerate() { - print!("{{{:?} : ", key); - print!("["); - fmt_tags(*tags); - print!("]}}"); - if i < len - 1 { print!(", "); } - } - print!("]"); - comma = true; - } - if !self.viewstag.is_empty() { - if comma { print!(", "); } - print!("\"viewstag\" : ["); - let vlen = self.viewstag.len(); - for (i, (key, tags)) in self.viewstag.iter().enumerate() { - print!("{{{:?} : ", key); - print!("["); - let len = tags.len(); - for (i, tag) in tags.iter().enumerate() { - print!("\"{}\"", tag); - if i < len - 1 { print!(", "); } - } - print!("]}}"); - if i < vlen - 1 { print!(", "); } - } - print!("]"); - comma = true; - } - if let Some(title) = self.title.as_ref() { - if comma { print!(", "); } - print!("\"title\" : {:?}", title); - } - println!("}}"); + if self.title.is_some() + || self.tags.is_some() + || self.urgency.is_some() + || self.viewstag.is_some() + { + println!("{}", serde_json::to_string(self).unwrap()); } } } @@ -181,20 +144,25 @@ fn main() { if let Some(env) = env.get::() { match event { zriver_output_status_v1::Event::FocusedTags { - tags, + tags: focused_tags, } => { - if env.flags.tags { - if let Some(inner_value) = env.tags.get_mut(&make) { - (*inner_value) = tags; + if let Some(tags) = &mut env.tags { + if let Some(inner_value) = + tags.get_mut(&make) + { + (*inner_value) = Tags(focused_tags); } else { - env.tags.insert(make.clone(), tags); + tags.insert( + make.clone(), + Tags(focused_tags), + ); } } } zriver_output_status_v1::Event::ViewTags { tags, } => { - if env.flags.viewstag { + if let Some(viewstag) = &mut env.viewstag { let tags: Vec = tags[0..] .chunks(4) .map(|s| { @@ -203,27 +171,32 @@ fn main() { u32::from_le_bytes(buf); for i in 0..32 { if 1 << i == tagmask { - return 1+i; + return 1 + i; } } 0 }) .collect(); - if let Some(inner_value) = env.viewstag.get_mut(&make) { + if let Some(inner_value) = + viewstag.get_mut(&make) + { (*inner_value) = tags; } else { - env.viewstag.insert(make.clone(), tags); + viewstag.insert(make.clone(), tags); } } } zriver_output_status_v1::Event::UrgentTags { tags, } => { - if env.flags.urgency { - if let Some(inner_value) = env.urgency.get_mut(&make) { - (*inner_value) = tags; + if let Some(urgency) = &mut env.urgency { + if let Some(inner_value) = + urgency.get_mut(&make) + { + (*inner_value) = Tags(tags); } else { - env.urgency.insert(make.clone(), tags); + urgency + .insert(make.clone(), Tags(tags)); } } } @@ -262,13 +235,13 @@ fn configuration() -> Flags { loop { match args.next() { Some(flag) => match flag.as_str() { - "--seat" | "-s" => default.seat = args.next(), - "--output" | "-o" => default.output = args.next(), - "--urgency" | "-u" => default.urgency = true, - "--title" | "-w" => default.title = true, - "--tags" | "-t" => default.tags = true, - "--views-tag" | "-vt" => default.viewstag = true, - "--help" | "-h" => { + "--seat" | "-s" => default.seat = args.next(), + "--output" | "-o" => default.output = args.next(), + "--urgency" | "-u" => default.urgency = true, + "--title" | "-w" => default.title = true, + "--tags" | "-t" => default.tags = true, + "--views-tag" | "-vt" => default.viewstag = true, + "--help" | "-h" => { print!("Usage: ristate [option]\n\n"); print!(" --tag | -t the focused tag\n"); print!(" --title | -w the title of the focused view\n"); @@ -279,23 +252,24 @@ fn configuration() -> Flags { std::process::exit(0); } _ => {} - } - None => break + }, + None => break, } } default } -fn fmt_tags(tagmask: u32) { - let mut first = true; - for i in 0..32 { - if tagmask >> i & 1 == 1 { - if !first { - print!(", \"{}\"", i + 1); - } else { - print!("\"{}\"", i + 1); - first = false; +impl Serialize for Tags { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut seq = serializer.serialize_seq(Some(self.0.count_ones() as usize))?; + for i in 0..32 { + if self.0 >> i & 1 == 1 { + seq.serialize_element(&format!("{}", i + 1))?; } } + seq.end() } }