From ce9c14325042b42bad9b26c61ec7d7819c6e1d14 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 27 Jun 2026 00:13:40 +0200 Subject: [PATCH] unify formattign --- src/device/mod.rs | 49 +++++++++++++++++++++++++++++++++-------------- src/main.rs | 12 ------------ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/device/mod.rs b/src/device/mod.rs index a1afedb..a8ac38a 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -34,6 +34,22 @@ pub struct DeviceStates { pub trait FormattableDevice { fn format_open_metrics(&self) -> impl Display + '_; + + fn format_dyn(&self) -> DynFormattable<'_> { + DynFormattable { + inner: Box::new(self.format_open_metrics()), + } + } +} + +pub struct DynFormattable<'a> { + inner: Box, +} + +impl FormattableDevice for DynFormattable<'_> { + fn format_open_metrics(&self) -> impl Display + '_ { + &self.inner + } } impl DeviceStates { @@ -48,12 +64,25 @@ impl DeviceStates { } } - pub fn devices(&self) -> impl Iterator { - self.devices.values() - } - - pub fn dsmr_devices(&self) -> impl Iterator { - self.dsmr_devices.values() + pub fn devices(&self) -> impl Iterator { + self.devices + .values() + .map(FormattableDevice::format_dyn) + .chain( + self.dsmr_devices + .values() + .map(FormattableDevice::format_dyn), + ) + .chain( + self.mi_temp_devices + .values() + .map(FormattableDevice::format_dyn), + ) + .chain( + self.rf_temp_devices + .values() + .map(FormattableDevice::format_dyn), + ) } pub fn update(&mut self, device: Device, json: JsonValue) { @@ -151,14 +180,6 @@ impl DeviceStates { } } - pub fn mi_temp(&self) -> impl Iterator { - self.mi_temp_devices.values() - } - - pub fn rf_temp(&self) -> impl Iterator { - self.rf_temp_devices.values() - } - pub fn retain(&mut self, cleanup_time: Instant, ping_time: Instant, client: &AsyncClient) { self.devices.retain(|device, state| { if state.last_seen < cleanup_time { diff --git a/src/main.rs b/src/main.rs index d6afec3..326249a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,18 +82,6 @@ async fn serve(device_states: Arc>, listen: ListenConfig) { write!(&mut response, "{}", state.format_open_metrics()) .expect("formatting failed"); } - for state in state.dsmr_devices() { - write!(&mut response, "{}", state.format_open_metrics()) - .expect("formatting failed"); - } - for state in state.mi_temp() { - write!(&mut response, "{}", state.format_open_metrics()) - .expect("formatting failed"); - } - for state in state.rf_temp() { - write!(&mut response, "{}", state.format_open_metrics()) - .expect("formatting failed"); - } response });