cargo fmt -all

This commit is contained in:
2026-09-12 17:45:43 +01:00
parent 27ec2b90a6
commit b20e03def7
8 changed files with 769 additions and 60 deletions
+1 -3
View File
@@ -5,9 +5,7 @@
use iced::widget::{container, mouse_area, text};
use iced::{Element, Task};
use common::{
BarModule, ClockKind, ClockMsg, ClockPayload, Endpoint, ModuleEffect, Service, Wire,
};
use common::{BarModule, ClockKind, ClockMsg, ClockPayload, Endpoint, ModuleEffect, Service, Wire};
use serde::Deserialize;
use toml::Table;
+1
View File
@@ -12,3 +12,4 @@ common = { path = "../common" }
iced = { workspace = true }
tokio = { workspace = true }
chrono = { workspace = true }
reqwest = "0.13.5"
+34 -47
View File
@@ -7,6 +7,7 @@
use std::collections::HashMap;
use chrono::{DateTime, Local, Timelike};
use iced::futures::{channel::mpsc, SinkExt, StreamExt};
use iced::Subscription;
@@ -17,15 +18,15 @@ pub const NAMESPACE: &str = "clock.";
/// Drives the clock, publishing only the kind modules last asked for.
pub struct ClockTicker {
last: HashMap<ClockKind, String>,
want: ClockKind,
last: DateTime<Local>,
// want: ClockKind,
}
impl Default for ClockTicker {
fn default() -> Self {
Self {
last: HashMap::new(),
want: ClockKind::Seconds,
last: DateTime::default(),
// want: ClockKind::Seconds,
}
}
}
@@ -40,58 +41,44 @@ impl ClockTicker {
pub fn run(inbox: Inbox) -> Subscription<Wire> {
Subscription::run_with(inbox, |inbox| {
let key = inbox.key();
// ponytail: the builder runs once (identity is stable, see `Inbox`
// hashing). If a module ever makes `services()` dynamic and a key
// is dropped then recreated, `take()` returns None and this panics
// — make `Bar` rebuild the inbox per start instead.
let mut rx = inbox.take().expect("inbox receiver is taken once");
iced::stream::channel(
0,
move |mut sender: mpsc::Sender<Wire>| async move {
let mut clock = ClockTicker::new();
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
loop {
tokio::select! {
_ = interval.tick() => {
if let Some(payload) = clock.tick() {
let wire = Wire::topic(Endpoint::service(key), key, payload);
if sender.send(wire).await.is_err() {
return;
}
// We dont need to rx any msgs from modules
let mut _rx = inbox.take().expect("inbox receiver is taken once");
iced::stream::channel(0, move |mut sender: mpsc::Sender<Wire>| async move {
let mut clock = ClockTicker::new();
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
loop {
tokio::select! {
_ = interval.tick() => {
if let Some(payload) = clock.tick() {
let wire = Wire::topic(Endpoint::service(key), key, payload);
if sender.send(wire).await.is_err() {
return;
}
}
msg = rx.next() => match msg {
Some(wire) => {
// A module poking us: switch published kind.
if let Some(kind) = wire.downcast::<ClockKind>() {
clock.want = *kind;
clock.last.clear();
}
}
None => return,
},
}
}
},
)
}
})
})
}
/// Latest value for the wanted kind, or `None` if unchanged.
pub fn tick(&mut self) -> Option<ClockPayload> {
let value = chrono::Local::now()
.format(match self.want {
ClockKind::Mins => "%H:%M",
ClockKind::Seconds => "%H:%M:%S",
})
.to_string();
if self.last.get(&self.want) == Some(&value) {
return None;
let value = chrono::Local::now();
let meow = self.last;
if meow.minute() != value.minute() {
return Some(ClockPayload {
kind: ClockKind::Mins,
value: value.format("%H:%M:%S").to_string(),
});
}
self.last.insert(self.want, value.clone());
Some(ClockPayload {
kind: self.want,
value,
})
if meow.second() != value.second() {
return Some(ClockPayload {
kind: ClockKind::Seconds,
value: value.format("%H:%M:%S").to_string(),
});
}
None
}
}
+2
View File
@@ -7,6 +7,7 @@ use iced::Subscription;
use common::{Inbox, Service, Wire};
pub mod datetime;
pub mod weather;
/// Conversion from a route key + its inbox to the subscription backing it.
///
@@ -21,6 +22,7 @@ impl IntoSubscription for Service {
fn into_subscription(self, inbox: Inbox) -> Subscription<Wire> {
match self.0 {
key if key.starts_with(datetime::NAMESPACE) => datetime::ClockTicker::run(inbox),
key if key.starts_with(weather::NAMESPACE) => datetime::ClockTicker::run(inbox),
_ => Subscription::none(),
}
}
+2
View File
@@ -0,0 +1,2 @@
pub struct WeatherService {}
pub const NAMESPACE: &str = "weather.";