From ad1d0bde3530dddf28f380564afcf2d6b20d0c52 Mon Sep 17 00:00:00 2001 From: Skye Date: Sat, 16 Dec 2023 15:20:59 -0500 Subject: [PATCH] Make name optional, clean config_path, skip nones when serializing --- src/config.rs | 14 ++++++-------- src/output.rs | 23 +++++++++++++++++++++++ src/tile.rs | 3 ++- src/tiles/battery.rs | 1 - src/tiles/hostname.rs | 1 - src/tiles/internet_time.rs | 1 - src/tiles/iwd.rs | 1 - src/tiles/load.rs | 1 - src/tiles/local_time.rs | 1 - src/tiles/memory.rs | 1 - src/tiles/system_time.rs | 1 - src/tiles/utc_time.rs | 1 - 12 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 61360c2..0538968 100644 --- a/src/config.rs +++ b/src/config.rs @@ -180,21 +180,19 @@ struct Args { pub async fn read_config() -> eyre::Result { let args = Args::from_args(); - let Some(config_path) = args.config.clone().or_else(|| { - Some(if let Ok(rusty_conf) = var("RUSTYBAR_CONFIG") { + let config_path = args.config.map(Ok).unwrap_or_else(|| { + Ok(if let Ok(rusty_conf) = var("RUSTYBAR_CONFIG") { rusty_conf.into() } else if let Ok(config) = var("XDG_CONFIG_HOME") { PathBuf::from_iter([&config, "rustybar/config.toml"]) } else if let Ok(home) = var("HOME") { PathBuf::from_iter([&home, ".config/rustybar/config.toml"]) } else { - return None; + eyre::bail!( + "Could not find RUSTYBAR_CONFIG, XDG_CONFIG_HOME, or HOME environment variables" + ) }) - }) else { - eyre::bail!( - "Could not find RUSTYBAR_CONFIG, XDG_CONFIG_HOME, or HOME environment variables" - ) - }; + })?; let string = tokio::fs::read_to_string(config_path) .await diff --git a/src/output.rs b/src/output.rs index cd45d28..f77b97b 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,5 +1,6 @@ use crate::tile::{Block, TileData}; use eyre::OptionExt; +use serde::{Serialize, ser::SerializeSeq}; use std::sync::Arc; use tokio::{ io::{self, AsyncWriteExt}, @@ -55,3 +56,25 @@ pub async fn run(num_tiles: usize, mut receiver: mpsc::Receiver) -> ey stdout.write_all(&serialized).await?; } } + +struct NoneSkipper<'b, I, T>(&'b I) +where + &'b I: IntoIterator>, + T: Serialize + 'b; + +impl<'b, I, T> Serialize for NoneSkipper<'b, I, T> +where + &'b I: IntoIterator>, + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mut seq = serializer.serialize_seq(None)?; + for e in self.0.into_iter().filter_map(Option::as_ref) { + seq.serialize_element(e)?; + } + seq.end() + } +} \ No newline at end of file diff --git a/src/tile.rs b/src/tile.rs index d01be35..3330a79 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -74,7 +74,8 @@ pub struct Block { pub min_width: Option, #[serde(skip_serializing_if = "Option::is_none")] pub align: Option, - pub name: &'static str, + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option<&'static str>, #[serde(serialize_with = "arc_default")] #[default = ""] pub instance: Arc, diff --git a/src/tiles/battery.rs b/src/tiles/battery.rs index 89ae447..e55f293 100644 --- a/src/tiles/battery.rs +++ b/src/tiles/battery.rs @@ -35,7 +35,6 @@ pub async fn battery(config: BatteryConfig, output: OutputChannel) -> eyre::Resu .send(Block { full_text: format!("{}% {}", percentage, status).into(), short_text: format!("{}%", percentage).into_boxed_str().into(), - name: "battery", ..Default::default() }) .await?; diff --git a/src/tiles/hostname.rs b/src/tiles/hostname.rs index ea2e64a..110ab0c 100644 --- a/src/tiles/hostname.rs +++ b/src/tiles/hostname.rs @@ -27,7 +27,6 @@ pub async fn hostname( output .send(Block { full_text: hostname.into(), - name: "hostname", ..Default::default() }) .await?; diff --git a/src/tiles/internet_time.rs b/src/tiles/internet_time.rs index a874ab0..fa15656 100644 --- a/src/tiles/internet_time.rs +++ b/src/tiles/internet_time.rs @@ -30,7 +30,6 @@ pub async fn internet_time( ) .into_boxed_str(), short_text: Some(format!("@{:03.0}", internet_time).into_boxed_str()), - name: "internet_time", ..Default::default() }) .await?; diff --git a/src/tiles/iwd.rs b/src/tiles/iwd.rs index de9253d..a26174b 100644 --- a/src/tiles/iwd.rs +++ b/src/tiles/iwd.rs @@ -63,7 +63,6 @@ pub async fn iwd( output .send(Block { full_text: text.into(), - name: config.interface, ..Default::default() }) .await?; diff --git a/src/tiles/load.rs b/src/tiles/load.rs index be78477..7c4464f 100644 --- a/src/tiles/load.rs +++ b/src/tiles/load.rs @@ -16,7 +16,6 @@ pub async fn load(config: LoadConfig, output: OutputChannel) -> eyre::Result eyre::Result .send(Block { full_text, short_text: Some(short_text), - name: "memory", ..Default::default() }) .await?; diff --git a/src/tiles/system_time.rs b/src/tiles/system_time.rs index eec5739..03ebe28 100644 --- a/src/tiles/system_time.rs +++ b/src/tiles/system_time.rs @@ -16,7 +16,6 @@ pub async fn system_time( .send(Block { full_text: now.format(&config.format).to_string().into(), short_text: Some(now.format(&config.short_format).to_string().into()), - name: "system_time", ..Default::default() }) .await?; diff --git a/src/tiles/utc_time.rs b/src/tiles/utc_time.rs index 74157f6..e24e452 100644 --- a/src/tiles/utc_time.rs +++ b/src/tiles/utc_time.rs @@ -13,7 +13,6 @@ pub async fn utc_time(config: UtcTimeConfig, output: OutputChannel) -> eyre::Res .send(Block { full_text: now.format(&config.format).to_string().into(), short_text: Some(now.format(&config.short_format).to_string().into()), - name: "utc_time", ..Default::default() }) .await?;