From 51a991314be1f9576e4d3f4ccf58a5e74b94c115 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 9 Jan 2025 19:16:36 +0400 Subject: [PATCH 1/2] feat: uformat macro wrapping ufmt::uwrite --- src/utils/mod.rs | 1 + src/utils/uformat.rs | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/utils/uformat.rs diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 069d6de..b7193a0 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,6 +2,7 @@ //! mod hifitime_serde; +pub mod uformat; use phf::phf_map; use serde::{Deserialize, Deserializer, Serialize, Serializer}; diff --git a/src/utils/uformat.rs b/src/utils/uformat.rs new file mode 100644 index 0000000..28b0e9f --- /dev/null +++ b/src/utils/uformat.rs @@ -0,0 +1,8 @@ +#[macro_export] +macro_rules! uformat { + ($($arg:tt)*) => {{ + let mut buf = String::new(); + ufmt::uwrite!(&mut buf, $($arg)*).unwrap(); + buf + }}; +} -- 2.48.1 From 5c4d70b46e3f036e62b0b838fd0697a0136f7fd0 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 9 Jan 2025 19:18:14 +0400 Subject: [PATCH 2/2] refactor: replace uwrite with uformat --- src/ingest_protocol/packet_types.rs | 31 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/ingest_protocol/packet_types.rs b/src/ingest_protocol/packet_types.rs index 1655728..de8e5c8 100644 --- a/src/ingest_protocol/packet_types.rs +++ b/src/ingest_protocol/packet_types.rs @@ -8,6 +8,7 @@ use crate::ingest_protocol::error::Error; use crate::ingest_protocol::parser::parse_mac_address; +use crate::uformat; use crate::utils::{EpochUTC, SupportedUnit}; use crate::web_server::app_error::{AppError, ServerRedisSnafu}; @@ -18,7 +19,6 @@ use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; use serde_with::serde_as; use snafu::ResultExt; -use ufmt::uwrite; use std::collections::{HashMap, HashSet}; use std::hash::{Hash, Hasher}; @@ -102,8 +102,6 @@ impl NMDeviceDataPacket { pub async fn save_to_db(&self, redis: &RedisClient) -> Result<(), AppError> { let device_mac_enc = hex::encode(self.mac); - let mut key = String::new(); - let now = Epoch::now().unwrap().into(); let mut device_time = self.time.unwrap_or(now); @@ -115,39 +113,42 @@ impl NMDeviceDataPacket { let device_tai_timestamp = device_time.0.to_duration_since_j1900().to_seconds(); - uwrite!(&mut key, "devices_{}", device_mac_enc).unwrap(); + let key = uformat!("devices_{}", device_mac_enc); - let device_exists: Option = redis.hget(key.as_str(), "exists").await.context(ServerRedisSnafu)?; + let device_exists: Option = redis + .hget(key.as_str(), "exists") + .await + .context(ServerRedisSnafu)?; if !device_exists.is_some_and(|v| v) { - return Err(AppError::DeviceNotFound { mac: hex::encode(self.mac) }); + return Err(AppError::DeviceNotFound { + mac: hex::encode(self.mac), + }); } // devices_{device_id}_{tai_timestamp}_{sensor_id} for sensor in &self.values { - let mut key = String::new(); - uwrite!( - &mut key, + let key = uformat!( "devices_{}_{}_{}", device_mac_enc, device_tai_timestamp.to_string(), sensor.mac - ) - .unwrap(); + ); redis .set(key.as_str(), sensor.value.to_string(), None, None, false) - .await.context(ServerRedisSnafu)?; + .await + .context(ServerRedisSnafu)?; } if let Some(commands) = &self.commands { for (cmd_key, cmd_value) in commands { - let mut key = String::new(); - uwrite!(&mut key, "devices_{}_cmds_{}", device_mac_enc, cmd_key).unwrap(); + let key = uformat!("devices_{}_cmds_{}", device_mac_enc, cmd_key); redis .set(key.as_str(), cmd_value, None, None, false) - .await.context(ServerRedisSnafu)?; + .await + .context(ServerRedisSnafu)?; } } -- 2.48.1