From 9d5049dde01cdb76f4772f8ce8f61a8b5bad3a50 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 11 Dec 2022 21:31:45 +0000 Subject: [PATCH] refactor: standardise error messages --- src/bar.rs | 18 ++++++++--------- src/bridge_channel.rs | 3 ++- src/clients/wayland/client.rs | 15 +++++++------- src/clients/wayland/toplevel.rs | 3 ++- src/config.rs | 4 ++++ src/dynamic_string.rs | 21 +++++++++----------- src/error.rs | 13 ++++++++++++ src/main.rs | 14 ++++++++----- src/modules/clock.rs | 5 +++-- src/modules/custom.rs | 11 +++++------ src/modules/focused.rs | 10 +++------- src/modules/launcher/item.rs | 15 +++++++------- src/modules/launcher/mod.rs | 35 +++++++++++++-------------------- src/modules/mpd.rs | 13 ++++++------ src/modules/sysinfo.rs | 15 +++++++------- src/modules/tray.rs | 4 ++-- src/modules/workspaces.rs | 9 ++++----- src/script.rs | 15 +++++++------- src/style.rs | 4 ++-- 19 files changed, 117 insertions(+), 110 deletions(-) create mode 100644 src/error.rs diff --git a/src/bar.rs b/src/bar.rs index d81b293..a114663 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -4,7 +4,7 @@ use crate::dynamic_string::DynamicString; use crate::modules::{Module, ModuleInfo, ModuleLocation, ModuleUpdateEvent, WidgetContext}; use crate::popup::Popup; use crate::script::{OutputStream, Script}; -use crate::{await_sync, Config}; +use crate::{await_sync, error as err, Config}; use color_eyre::Result; use gtk::gdk::Monitor; use gtk::prelude::*; @@ -253,7 +253,7 @@ where fn register_popup_content(popup: &Arc>, id: usize, popup_content: gtk::Box) { popup .write() - .expect("Failed to get write lock on popup") + .expect(err::ERR_WRITE_LOCK) .register_content(id, popup_content); } @@ -277,15 +277,14 @@ fn setup_receiver( match ev { ModuleUpdateEvent::Update(update) => { if has_popup { - p_tx.send(update.clone()) - .expect("Failed to send update to popup"); + p_tx.send(update.clone()).expect(err::ERR_CHANNEL_SEND); } - w_tx.send(update).expect("Failed to send update to widget"); + w_tx.send(update).expect(err::ERR_CHANNEL_SEND); } ModuleUpdateEvent::TogglePopup(geometry) => { debug!("Toggling popup for {} [#{}]", name, id); - let popup = popup.read().expect("Failed to get read lock on popup"); + let popup = popup.read().expect(err::ERR_READ_LOCK); if popup.is_visible() { popup.hide(); } else { @@ -296,7 +295,7 @@ fn setup_receiver( ModuleUpdateEvent::OpenPopup(geometry) => { debug!("Opening popup for {} [#{}]", name, id); - let popup = popup.read().expect("Failed to get read lock on popup"); + let popup = popup.read().expect(err::ERR_READ_LOCK); popup.hide(); popup.show_content(id); popup.show(geometry); @@ -304,7 +303,7 @@ fn setup_receiver( ModuleUpdateEvent::ClosePopup => { debug!("Closing popup for {} [#{}]", name, id); - let popup = popup.read().expect("Failed to get read lock on popup"); + let popup = popup.read().expect(err::ERR_READ_LOCK); popup.hide(); } } @@ -334,8 +333,7 @@ fn setup_module_common_options(container: EventBox, common: CommonConfig) { spawn(async move { script .run(|(_, success)| { - tx.send(success) - .expect("Failed to send widget visibility toggle message"); + tx.send(success).expect(err::ERR_CHANNEL_SEND); }) .await; }); diff --git a/src/bridge_channel.rs b/src/bridge_channel.rs index ed516e5..3283de6 100644 --- a/src/bridge_channel.rs +++ b/src/bridge_channel.rs @@ -1,3 +1,4 @@ +use crate::error; use tokio::spawn; use tokio::sync::mpsc; @@ -21,7 +22,7 @@ impl BridgeChannel { spawn(async move { while let Some(val) = async_rx.recv().await { - sync_tx.send(val).expect("Failed to send message"); + sync_tx.send(val).expect(error::ERR_CHANNEL_SEND); } }); diff --git a/src/clients/wayland/client.rs b/src/clients/wayland/client.rs index 149e3e8..6dc610e 100644 --- a/src/clients/wayland/client.rs +++ b/src/clients/wayland/client.rs @@ -18,6 +18,7 @@ use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::{ zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1, zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1, }; +use crate::error as err; pub struct WaylandClient { pub outputs: Vec, @@ -48,7 +49,7 @@ impl WaylandClient { let outputs = Self::get_outputs(&env); output_tx .send(outputs) - .expect("Failed to send outputs out of task"); + .expect(err::ERR_CHANNEL_SEND); let seats = env.get_all_seats(); seat_tx @@ -58,7 +59,7 @@ impl WaylandClient { .map(|seat| seat.detach()) .collect::>(), ) - .expect("Failed to send seats out of task"); + .expect(err::ERR_CHANNEL_SEND); let _toplevel_manager = env.require_global::(); @@ -68,18 +69,18 @@ impl WaylandClient { if event.change == ToplevelChange::Close { toplevels2 .write() - .expect("Failed to get write lock on toplevels") + .expect(err::ERR_WRITE_LOCK) .remove(&event.toplevel.id); } else { toplevels2 .write() - .expect("Failed to get write lock on toplevels") + .expect(err::ERR_WRITE_LOCK) .insert(event.toplevel.id, (event.toplevel.clone(), handle)); } toplevel_tx2 .send(event) - .expect("Failed to send toplevel event"); + .expect(err::ERR_CHANNEL_SEND); }); let mut event_loop = @@ -101,9 +102,9 @@ impl WaylandClient { let outputs = output_rx .await - .expect("Failed to receive outputs from task"); + .expect(err::ERR_CHANNEL_RECV); - let seats = seat_rx.await.expect("Failed to receive seats from task"); + let seats = seat_rx.await.expect(err::ERR_CHANNEL_RECV); Self { outputs, diff --git a/src/clients/wayland/toplevel.rs b/src/clients/wayland/toplevel.rs index 5559db1..57325d8 100644 --- a/src/clients/wayland/toplevel.rs +++ b/src/clients/wayland/toplevel.rs @@ -4,6 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use tracing::trace; use wayland_client::{DispatchData, Main}; use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::zwlr_foreign_toplevel_handle_v1::{Event, ZwlrForeignToplevelHandleV1}; +use crate::error; const STATE_ACTIVE: u32 = 2; const STATE_FULLSCREEN: u32 = 3; @@ -144,7 +145,7 @@ impl Toplevel { handle.quick_assign(move |_handle, event, ddata| { let mut inner = inner .write() - .expect("Failed to get write lock on toplevel inner state"); + .expect(error::ERR_WRITE_LOCK); toplevel_implem(event, &mut inner, &mut callback, ddata); }); diff --git a/src/config.rs b/src/config.rs index 8d251b7..9e96900 100644 --- a/src/config.rs +++ b/src/config.rs @@ -63,6 +63,8 @@ impl Default for BarPosition { } impl BarPosition { + /// Gets the orientation the bar and widgets should use + /// based on this position. pub fn get_orientation(self) -> Orientation { if self == Self::Top || self == Self::Bottom { Orientation::Horizontal @@ -71,6 +73,8 @@ impl BarPosition { } } + /// Gets the angle that label text should be displayed at + /// based on this position. pub const fn get_angle(self) -> f64 { match self { Self::Top | Self::Bottom => 0.0, diff --git a/src/dynamic_string.rs b/src/dynamic_string.rs index ddd9ebe..ef09c47 100644 --- a/src/dynamic_string.rs +++ b/src/dynamic_string.rs @@ -1,3 +1,4 @@ +use crate::error; use crate::script::{OutputStream, Script}; use gtk::prelude::*; use indexmap::IndexMap; @@ -10,9 +11,7 @@ enum DynamicStringSegment { Dynamic(Script), } -pub struct DynamicString { - // pub label: gtk::Label, -} +pub struct DynamicString; impl DynamicString { pub fn new(input: &str, f: F) -> Self @@ -69,7 +68,7 @@ impl DynamicString { DynamicStringSegment::Static(str) => { label_parts .lock() - .expect("Failed to get lock on label parts") + .expect(error::ERR_MUTEX_LOCK) .insert(i, str); } DynamicStringSegment::Dynamic(script) => { @@ -80,9 +79,8 @@ impl DynamicString { script .run(|(out, _)| { if let OutputStream::Stdout(out) = out { - let mut label_parts = label_parts - .lock() - .expect("Failed to get lock on label parts"); + let mut label_parts = + label_parts.lock().expect(error::ERR_MUTEX_LOCK); label_parts // .lock() @@ -94,7 +92,7 @@ impl DynamicString { .map(|(_, part)| part.as_str()) .collect::(); - tx.send(string).expect("Failed to send update"); + tx.send(string).expect(error::ERR_CHANNEL_SEND); } }) .await; @@ -107,18 +105,17 @@ impl DynamicString { { let label_parts = label_parts .lock() - .expect("Failed to get lock on label parts") + .expect(error::ERR_MUTEX_LOCK) .iter() .map(|(_, part)| part.as_str()) .collect::(); - tx.send(label_parts).expect("Failed to send update"); + tx.send(label_parts).expect(error::ERR_CHANNEL_SEND); } rx.attach(None, f); - // Self { label } - Self {} + Self } } diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..d5a19b2 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,13 @@ +#[repr(i32)] +pub enum ExitCode { + GtkDisplay = 1, + CreateBars = 2, + Config = 3, +} + +pub const ERR_OUTPUTS: &str = "GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"; +pub const ERR_MUTEX_LOCK: &str = "Failed to get lock on Mutex"; +pub const ERR_READ_LOCK: &str = "Failed to get read lock"; +pub const ERR_WRITE_LOCK: &str = "Failed to get write lock"; +pub const ERR_CHANNEL_SEND: &str = "Failed to send message to channel"; +pub const ERR_CHANNEL_RECV: &str = "Failed to receive message from channel"; diff --git a/src/main.rs b/src/main.rs index de6d71a..09ef4a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod bridge_channel; mod clients; mod config; mod dynamic_string; +mod error; mod icon; mod logging; mod modules; @@ -30,6 +31,7 @@ use crate::error::ExitCode; use clients::wayland::{self, WaylandClient}; use tracing::{debug, error, info}; +const GTK_APP_ID: &str = "dev.jstanger.ironbar"; const VERSION: &str = env!("CARGO_PKG_VERSION"); #[tokio::main] @@ -41,9 +43,7 @@ async fn main() -> Result<()> { let wayland_client = wayland::get_client().await; - let app = Application::builder() - .application_id("dev.jstanger.ironbar") - .build(); + let app = Application::builder().application_id(GTK_APP_ID).build(); app.connect_activate(move |app| { let display = Display::default().map_or_else( @@ -112,8 +112,12 @@ fn create_bars( let num_monitors = display.n_monitors(); for i in 0..num_monitors { - let monitor = display.monitor(i).ok_or_else(|| Report::msg("GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"))?; - let output = outputs.get(i as usize).ok_or_else(|| Report::msg("GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"))?; + let monitor = display + .monitor(i) + .ok_or_else(|| Report::msg(error::ERR_OUTPUTS))?; + let output = outputs + .get(i as usize) + .ok_or_else(|| Report::msg(error::ERR_OUTPUTS))?; let monitor_name = &output.name; // TODO: Could we use an Arc or `Cow` here to avoid cloning? diff --git a/src/modules/clock.rs b/src/modules/clock.rs index 49da2d4..daf7c1d 100644 --- a/src/modules/clock.rs +++ b/src/modules/clock.rs @@ -1,4 +1,5 @@ use crate::config::CommonConfig; +use crate::error; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::popup::Popup; use chrono::{DateTime, Local}; @@ -48,7 +49,7 @@ impl Module