From 943f371cf14f62af623e30b0b0e52fd51d8b5b88 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 30 Jul 2024 16:15:15 +1000 Subject: [PATCH] config: not oncelock --- src/config.rs | 34 ++++++++++++++++++++++++++++++++-- src/flash.rs | 2 +- src/main.rs | 11 ++++------- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6cf792b..f4099f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,7 +15,37 @@ pub struct Config { pub output_blocklist: Vec, } -pub fn parse_config() -> Res<()> { +pub struct ConfigHandle { + inner: tokio::sync::RwLock>, +} + +pub struct ConfigHandleHandle<'a>(tokio::sync::RwLockReadGuard<'a, Option>); + +impl std::ops::Deref for ConfigHandleHandle<'_> { + type Target = Config; + + fn deref(&self) -> &Self::Target { + self.0.as_ref().unwrap() + } +} + +impl ConfigHandle { + pub(super) const fn new() -> Self { + Self { + inner: tokio::sync::RwLock::const_new(None), + } + } + + async fn set(&self, config: Config) { + *self.inner.write().await = Some(config) + } + + pub async fn get(&self) -> ConfigHandleHandle { + ConfigHandleHandle(self.inner.read().await) + } +} + +pub async fn parse_config() -> Res<()> { let dirs = directories::ProjectDirs::from("com", "alexjanka", "sway-flash-indicator") .ok_or(Error::NoMatchingConfig)?; @@ -38,7 +68,7 @@ pub fn parse_config() -> Res<()> { c }; - crate::CONFIG.set(config)?; + crate::CONFIG.set(config).await; Ok(()) } diff --git a/src/flash.rs b/src/flash.rs index 14fe262..4259970 100644 --- a/src/flash.rs +++ b/src/flash.rs @@ -4,7 +4,7 @@ pub async fn interpolate_task() -> Res<()> { let mut connection = swayipc_async::Connection::new().await?; let to = crate::DEFAULT_BORDER.get().ok_or(Error::NoMatchingConfig)?; - let config = CONFIG.get().ok_or(Error::NoMatchingConfig)?; + let config = CONFIG.get().await; let per_frame = 1.0 / config.frames_anim as f32; let (d_l, d_a, d_b) = ( (to.l - config.flash_colour.l) * per_frame, diff --git a/src/main.rs b/src/main.rs index caf4456..fe56fe3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use config::Config; use futures_util::StreamExt; use lab::Lab; @@ -13,8 +12,7 @@ pub mod prelude { } use prelude::*; - -pub static CONFIG: tokio::sync::OnceCell = tokio::sync::OnceCell::const_new(); +pub static CONFIG: config::ConfigHandle = config::ConfigHandle::new(); static DEFAULT_COLOURS: tokio::sync::OnceCell = tokio::sync::OnceCell::const_new(); static DEFAULT_COLOURS_NO_INDICATOR: tokio::sync::OnceCell = @@ -28,7 +26,7 @@ async fn main() -> Res<()> { } pretty_env_logger::init(); - config::parse_config()?; + config::parse_config().await?; let mut event_connection = swayipc_async::Connection::new().await?; let mut autosplit_connection = swayipc_async::Connection::new().await?; @@ -84,7 +82,7 @@ async fn main() -> Res<()> { if let Err(e) = code_trigger( &mut autosplit_connection, id, - &CONFIG.get().unwrap().output_blocklist, + &CONFIG.get().await.output_blocklist, ) .await { @@ -101,8 +99,7 @@ async fn main() -> Res<()> { continue; } let ratio = (width as f64) / (height as f64); - let autosplit_ratio = - CONFIG.get().ok_or(Error::NoMatchingConfig)?.autosplit_ratio; + let autosplit_ratio = CONFIG.get().await.autosplit_ratio; if let Err(e) = if ratio > autosplit_ratio { autosplit_connection.run_command("splith").await } else {