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
+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
}
}