From 94a8deabd176c07fbf825627cbbbdb3b95b85b15 Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Mon, 26 Apr 2021 20:04:06 +0200 Subject: New clippy lints, better code... --- clock/src/lib.rs | 14 +++++++------- swaystatus-plugin/src/lib.rs | 18 +++++++++++++++--- swaystatus/src/communication/mod.rs | 4 ++-- testconfig | 2 +- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/clock/src/lib.rs b/clock/src/lib.rs index 3fa1b97..109045b 100644 --- a/clock/src/lib.rs +++ b/clock/src/lib.rs @@ -81,7 +81,7 @@ impl<'c> SwayStatusModuleRunnable for ClockRunnable<'c> { ClockRefreshRate::NotSynchronized { seconds } => { self.simple_loop(std::time::Duration::from_secs_f32(seconds.abs())); }, - ClockRefreshRate::UTCSynchronized { updates_per_thirty_minutes }=> { + ClockRefreshRate::UtcSynchronized { updates_per_thirty_minutes }=> { self.synchronized_loop(std::cmp::max(updates_per_thirty_minutes,1) as u64); } } @@ -112,7 +112,7 @@ enum ClockRefreshRate { #[serde(rename = "Seconds")] seconds : f32 }, - UTCSynchronized { + UtcSynchronized { #[serde(rename = "PerThirtyMinutes")] updates_per_thirty_minutes : u16 } @@ -129,7 +129,7 @@ impl Default for ClockConfig { fn default() -> Self { ClockConfig { format : String::from("%R"), - refresh_rate : ClockRefreshRate::UTCSynchronized { updates_per_thirty_minutes: 1800 } + refresh_rate : ClockRefreshRate::UtcSynchronized { updates_per_thirty_minutes: 1800 } } } } @@ -175,11 +175,11 @@ enum MessagesFromMain { struct SenderForMain(Sender); impl MsgMainToModule for SenderForMain { - fn send_quit(&self) -> Result<(),()> { - self.0.send(MessagesFromMain::Quit).map_err(|_| ()) + fn send_quit(&self) -> Result<(),PluginCommunicationError> { + self.0.send(MessagesFromMain::Quit).map_err(|_| PluginCommunicationError) } - fn send_refresh(&self) -> Result<(),()> { - self.0.send(MessagesFromMain::Refresh).map_err(|_| ()) + fn send_refresh(&self) -> Result<(),PluginCommunicationError> { + self.0.send(MessagesFromMain::Refresh).map_err(|_| PluginCommunicationError) } } diff --git a/swaystatus-plugin/src/lib.rs b/swaystatus-plugin/src/lib.rs index 3e27d19..1d2d22b 100644 --- a/swaystatus-plugin/src/lib.rs +++ b/swaystatus-plugin/src/lib.rs @@ -77,13 +77,13 @@ pub trait MsgMainToModule { /// Implement this in such a way, that when it's called from the main thread, your module will /// soon-ish return from it's main function, after cleaning up it's resources and after joining /// all threads it spawns. - fn send_quit(&self) -> Result<(),()>; + fn send_quit(&self) -> Result<(),PluginCommunicationError>; /// Implement this in such a way, that when it's called from the main thread, your module will /// soon-ish send an updated text. The main module does not really wait for updates, so /// ignoring this or implementing it empty is perfectly fine if you know that your module's /// output cannot possibly change between updates it sends anyhow. - fn send_refresh(&self) -> Result<(),()>; + fn send_refresh(&self) -> Result<(),PluginCommunicationError>; } /// When communicating an error to the main program, this allows to choose an appropriate handling @@ -124,7 +124,7 @@ pub trait MsgModuleToMain : Send { /// stdout output. While this can in theory return an error, that should practically never /// happen. If this errors, you should probably clean up your resources and return from the /// run() function. In other words, act as if main had sent you a quit command. - fn send_update(&self, text : Result) -> Result<(),()>; + fn send_update(&self, text : Result) -> Result<(),PluginCommunicationError>; } /// Interface your module should implement. All functions of this will be called in the main thread. @@ -161,3 +161,15 @@ pub trait SwayStatusModuleInstance : erased_serde::Serialize { fn make_runnable<'p>(&'p self, to_main : Box) -> (Box, Box); } serialize_trait_object!(SwayStatusModuleInstance); + +///Error type used by send functions. +#[derive(Debug)] +pub struct PluginCommunicationError; + +impl std::fmt::Display for PluginCommunicationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"Communication between plugin and main program terminated unexpectedly") + } +} + +impl std::error::Error for PluginCommunicationError {} diff --git a/swaystatus/src/communication/mod.rs b/swaystatus/src/communication/mod.rs index c0deff9..ff4a5ce 100644 --- a/swaystatus/src/communication/mod.rs +++ b/swaystatus/src/communication/mod.rs @@ -46,8 +46,8 @@ impl Drop for SenderToMain { } impl plugin::MsgModuleToMain for SenderToMain { - fn send_update(&self, text : Result) -> Result<(),()> { + fn send_update(&self, text : Result) -> Result<(),plugin::PluginCommunicationError> { let message = Message::External { text , element_number : self.element_number }; - self.sender.send(message).map_err(|_| {}) + self.sender.send(message).map_err(|_| plugin::PluginCommunicationError) } } diff --git a/testconfig b/testconfig index 3e0768b..89c9dcf 100644 --- a/testconfig +++ b/testconfig @@ -8,5 +8,5 @@ Plugin = "ClockPlugin" Format = "%+" [Element.Config.RefreshRate] -Synchronization = "UTCSynchronized" +Synchronization = "UtcSynchronized" PerThirtyMinutes = 1800 -- cgit v1.2.3