use serde_json for serialization

This commit is contained in:
Max Verevkin 2022-05-06 15:35:37 +03:00
parent 0adc7239dd
commit 103aa3bc38
3 changed files with 148 additions and 118 deletions

100
Cargo.lock generated
View File

@ -10,15 +10,15 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.67" version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -33,10 +33,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]] [[package]]
name = "libc" name = "itoa"
version = "0.2.119" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "memoffset" name = "memoffset"
@ -62,30 +68,30 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.7.2" version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.19" version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.27" version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.9" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -94,6 +100,8 @@ dependencies = [
name = "ristate" name = "ristate"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"serde",
"serde_json",
"wayland-client", "wayland-client",
"wayland-commons", "wayland-commons",
"wayland-protocols", "wayland-protocols",
@ -101,16 +109,64 @@ dependencies = [
] ]
[[package]] [[package]]
name = "smallvec" name = "ryu"
version = "1.6.1" version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.2" version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]] [[package]]
name = "wayland-client" name = "wayland-client"
@ -172,6 +228,6 @@ dependencies = [
[[package]] [[package]]
name = "xml-rs" name = "xml-rs"
version = "0.8.3" version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a" checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"

View File

@ -4,8 +4,6 @@ version = "0.1.0"
authors = ["Bryan Ndjeutcha <ndjeutcha@gmail.com>"] authors = ["Bryan Ndjeutcha <ndjeutcha@gmail.com>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies] [build-dependencies]
wayland-scanner = "0.29.3" wayland-scanner = "0.29.3"
@ -13,3 +11,5 @@ wayland-scanner = "0.29.3"
wayland-protocols = "0.29.3" wayland-protocols = "0.29.3"
wayland-commons = "0.29.3" wayland-commons = "0.29.3"
wayland-client = "0.29.3" wayland-client = "0.29.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -3,6 +3,8 @@ mod wayland;
use crate::wayland::river_status_unstable_v1::{ use crate::wayland::river_status_unstable_v1::{
zriver_output_status_v1, zriver_seat_status_v1, zriver_status_manager_v1::ZriverStatusManagerV1, 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 std::collections::BTreeMap;
use wayland_client::protocol::{wl_output, wl_output::WlOutput, wl_seat, wl_seat::WlSeat}; use wayland_client::protocol::{wl_output, wl_output::WlOutput, wl_seat, wl_seat::WlSeat};
use wayland_client::{Display, GlobalManager, Main}; use wayland_client::{Display, GlobalManager, Main};
@ -30,83 +32,44 @@ impl Flags {
} }
} }
#[derive(Debug)] struct Tags(u32);
#[derive(Serialize)]
struct Env { struct Env {
#[serde(skip)]
flags: Flags, flags: Flags,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>, title: Option<String>,
tags: BTreeMap<String, u32>, #[serde(skip_serializing_if = "Option::is_none")]
urgency: BTreeMap<String, u32>, tags: Option<BTreeMap<String, Tags>>,
viewstag: BTreeMap<String, Vec<u32>>, #[serde(skip_serializing_if = "Option::is_none")]
urgency: Option<BTreeMap<String, Tags>>,
#[serde(skip_serializing_if = "Option::is_none")]
viewstag: Option<BTreeMap<String, Vec<u32>>>,
#[serde(skip)]
status_manager: Option<Main<ZriverStatusManagerV1>>, status_manager: Option<Main<ZriverStatusManagerV1>>,
} }
impl Env { impl Env {
fn new() -> Env { fn new() -> Env {
let flags = configuration();
Env { Env {
title: None, title: None,
flags: configuration(), tags: flags.tags.then(BTreeMap::new),
viewstag: BTreeMap::new(), urgency: flags.urgency.then(BTreeMap::new),
urgency: BTreeMap::new(), viewstag: flags.viewstag.then(BTreeMap::new),
tags: BTreeMap::new(),
status_manager: None, status_manager: None,
flags,
} }
} }
fn fmt(&self) { fn fmt(&self) {
if !self.tags.is_empty() if self.title.is_some()
|| !self.viewstag.is_empty() || self.tags.is_some()
|| !self.urgency.is_empty() || self.urgency.is_some()
|| self.title.is_some() { || self.viewstag.is_some()
print!("{{"); {
let mut comma = false; println!("{}", serde_json::to_string(self).unwrap());
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!("}}");
} }
} }
} }
@ -181,20 +144,25 @@ fn main() {
if let Some(env) = env.get::<Env>() { if let Some(env) = env.get::<Env>() {
match event { match event {
zriver_output_status_v1::Event::FocusedTags { zriver_output_status_v1::Event::FocusedTags {
tags, tags: focused_tags,
} => { } => {
if env.flags.tags { if let Some(tags) = &mut env.tags {
if let Some(inner_value) = env.tags.get_mut(&make) { if let Some(inner_value) =
(*inner_value) = tags; tags.get_mut(&make)
{
(*inner_value) = Tags(focused_tags);
} else { } else {
env.tags.insert(make.clone(), tags); tags.insert(
make.clone(),
Tags(focused_tags),
);
} }
} }
} }
zriver_output_status_v1::Event::ViewTags { zriver_output_status_v1::Event::ViewTags {
tags, tags,
} => { } => {
if env.flags.viewstag { if let Some(viewstag) = &mut env.viewstag {
let tags: Vec<u32> = tags[0..] let tags: Vec<u32> = tags[0..]
.chunks(4) .chunks(4)
.map(|s| { .map(|s| {
@ -203,27 +171,32 @@ fn main() {
u32::from_le_bytes(buf); u32::from_le_bytes(buf);
for i in 0..32 { for i in 0..32 {
if 1 << i == tagmask { if 1 << i == tagmask {
return 1+i; return 1 + i;
} }
} }
0 0
}) })
.collect(); .collect();
if let Some(inner_value) = env.viewstag.get_mut(&make) { if let Some(inner_value) =
viewstag.get_mut(&make)
{
(*inner_value) = tags; (*inner_value) = tags;
} else { } else {
env.viewstag.insert(make.clone(), tags); viewstag.insert(make.clone(), tags);
} }
} }
} }
zriver_output_status_v1::Event::UrgentTags { zriver_output_status_v1::Event::UrgentTags {
tags, tags,
} => { } => {
if env.flags.urgency { if let Some(urgency) = &mut env.urgency {
if let Some(inner_value) = env.urgency.get_mut(&make) { if let Some(inner_value) =
(*inner_value) = tags; urgency.get_mut(&make)
{
(*inner_value) = Tags(tags);
} else { } else {
env.urgency.insert(make.clone(), tags); urgency
.insert(make.clone(), Tags(tags));
} }
} }
} }
@ -279,23 +252,24 @@ fn configuration() -> Flags {
std::process::exit(0); std::process::exit(0);
} }
_ => {} _ => {}
} },
None => break None => break,
} }
} }
default default
} }
fn fmt_tags(tagmask: u32) { impl Serialize for Tags {
let mut first = true; fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.0.count_ones() as usize))?;
for i in 0..32 { for i in 0..32 {
if tagmask >> i & 1 == 1 { if self.0 >> i & 1 == 1 {
if !first { seq.serialize_element(&format!("{}", i + 1))?;
print!(", \"{}\"", i + 1);
} else {
print!("\"{}\"", i + 1);
first = false;
} }
} }
seq.end()
} }
} }