diff --git a/Cargo.lock b/Cargo.lock index 6aab309..6063021 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,6 +303,20 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.29" @@ -310,6 +324,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -318,6 +333,12 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" +[[package]] +name = "futures-io" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" + [[package]] name = "futures-sink" version = "0.3.29" @@ -337,6 +358,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" dependencies = [ "futures-core", + "futures-sink", "futures-task", "pin-project-lite", "pin-utils", @@ -1006,6 +1028,7 @@ dependencies = [ "dbus-codegen", "dbus-tokio", "eyre", + "futures", "local-ip-address", "log", "maxminddb", diff --git a/Cargo.toml b/Cargo.toml index 4cbca8f..59bcb6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ chrono-tz = "0.8" dbus = "0.9" dbus-tokio = "0.7" eyre = "0.6" +futures = { version = "0.3", default-features = false } local-ip-address = "0.5" log = "0.4" maxminddb = "0.23" diff --git a/src/config.rs b/src/config.rs index 0538968..fc47240 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,11 +3,14 @@ use crate::tile::TileData; use crate::tiles; use dbus::nonblock::SyncConnection; +use futures::FutureExt; use log::error; use serde::{Deserialize, Deserializer}; use smart_default::SmartDefault; +use std::convert::Infallible; use std::env::var; +use std::future::Future; use std::iter::FromIterator as _; use std::path::Path; use std::path::PathBuf; @@ -203,31 +206,38 @@ pub async fn read_config() -> eyre::Result { Ok(toml::from_str(&string)?) } +fn spawn(future: F, tile_id: usize) -> JoinHandle<()> +where + F: Future> + Send + 'static, +{ + tokio::spawn(future.map(move |r| match r { + Ok(i) => match i {}, + Err(e) => error!("Tile {} exited with error: {:?}", tile_id, e), + })) +} + pub fn launch_tile( tile: TileConfig, sender: mpsc::Sender, tile_id: usize, - dbus_conn: Arc, + dbus_conn: &Arc, ) -> JoinHandle<()> { - tokio::spawn(async move { - let output_chan = OutputChannel::with_random_uuid(sender, tile_id); - let result = match tile { - TileConfig::Battery(c) => tiles::battery(c, output_chan).await, - TileConfig::Hostname(c) => tiles::hostname(c, output_chan, dbus_conn).await, - TileConfig::Load(c) => tiles::load(c, output_chan).await, - TileConfig::Memory(c) => tiles::memory(c, output_chan).await, - TileConfig::UtcTime(c) => tiles::utc_time(c, output_chan).await, - TileConfig::Iwd(c) => tiles::iwd(c, output_chan, dbus_conn).await, - TileConfig::SystemTime(c) => tiles::system_time(c, output_chan).await, - TileConfig::LocalTime(c) => tiles::local_time(c, output_chan).await, - TileConfig::InternetTime(c) => tiles::internet_time(c, output_chan).await, - TileConfig::NetworkManager(c) => { - tiles::network_manager(c, output_chan, dbus_conn).await - } - }; - match result { - Ok(t) => match t {}, - Err(e) => error!("Tile {} exited with error: {:?}", tile_id, e), + let output_chan = OutputChannel::with_random_uuid(sender.clone(), tile_id); + match tile.clone() { + TileConfig::Battery(c) => spawn(tiles::battery(c, output_chan), tile_id), + TileConfig::Hostname(c) => { + spawn(tiles::hostname(c, output_chan, dbus_conn.clone()), tile_id) } - }) + TileConfig::Load(c) => spawn(tiles::load(c, output_chan), tile_id), + TileConfig::Memory(c) => spawn(tiles::memory(c, output_chan), tile_id), + TileConfig::UtcTime(c) => spawn(tiles::utc_time(c, output_chan), tile_id), + TileConfig::Iwd(c) => spawn(tiles::iwd(c, output_chan, dbus_conn.clone()), tile_id), + TileConfig::SystemTime(c) => spawn(tiles::system_time(c, output_chan), tile_id), + TileConfig::LocalTime(c) => spawn(tiles::local_time(c, output_chan), tile_id), + TileConfig::InternetTime(c) => spawn(tiles::internet_time(c, output_chan), tile_id), + TileConfig::NetworkManager(c) => spawn( + tiles::network_manager(c, output_chan, dbus_conn.clone()), + tile_id, + ), + } } diff --git a/src/main.rs b/src/main.rs index 13ddb0f..b862f13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,9 +26,7 @@ async fn main() -> eyre::Result<()> { .tile .drain(..) .enumerate() - .map(|(sender_id, tile)| { - config::launch_tile(tile, sender.clone(), sender_id, dbus_conn.clone()) - }) + .map(|(sender_id, tile)| config::launch_tile(tile, sender.clone(), sender_id, &dbus_conn)) .collect(); let num_tiles = tiles.len();