From 5c4d70b46e3f036e62b0b838fd0697a0136f7fd0 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 9 Jan 2025 19:18:14 +0400 Subject: [PATCH] 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)?; } }