From c300575dc880fe61d0f07e7865f277e0d38d7d5f Mon Sep 17 00:00:00 2001 From: Bo Jeanes Date: Thu, 18 Aug 2022 09:28:43 +1000 Subject: [PATCH] Pass with clippy --- src/main.rs | 16 ++++++++-------- src/modbus/config.rs | 19 ++----------------- src/modbus/mod.rs | 12 +++++------- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8dfc58d..e0199e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use serde_json::json; use std::{collections::HashMap, time::Duration}; use tokio::{sync::mpsc, sync::oneshot, time::MissedTickBehavior}; use tokio_modbus::prelude::*; -use tracing::{debug, error, info, span, warn, Level}; +use tracing::{debug, error, info}; use clap::Parser; @@ -44,8 +44,8 @@ async fn main() { let args = Cli::parse(); - let (registry_tx, mut registry_rx) = mpsc::channel::(32); - let (dispatcher_tx, mut dispatcher_rx) = mpsc::channel::(32); + let (registry_tx, registry_rx) = mpsc::channel::(32); + let (dispatcher_tx, dispatcher_rx) = mpsc::channel::(32); // Modbus connection registry let registry_handle = { @@ -86,7 +86,7 @@ async fn mqtt_dispatcher( info!("Connecting to MQTT broker..."); options.set_last_will(LastWill { - topic: format!("{}/status", prefix).to_string(), + topic: format!("{}/status", prefix), message: serde_json::to_vec(&json!({ "status": MainStatus::Stopped, })) @@ -100,7 +100,7 @@ async fn mqtt_dispatcher( client .publish( - format!("{}/status", prefix).to_string(), + format!("{}/status", prefix), QoS::AtMostOnce, false, serde_json::to_vec(&json!({ @@ -344,7 +344,7 @@ async fn handle_connect( topic: format!("{}/status/{}", topic_prefix, id), payload: serde_json::to_vec(&json!({ "status": ConnectState::Errored, - "error": format!("Invalid config: {}", err.to_string()), + "error": format!("Invalid config: {}", err), })) .unwrap(), }) @@ -396,7 +396,7 @@ async fn watch_registers( let swapped_words = r.apply_swaps(&words); - let value = r.from_words(&swapped_words); + let value = r.parse_words(&swapped_words); debug!( name = r.name.as_ref().unwrap_or(&"".to_string()), @@ -420,7 +420,7 @@ async fn watch_registers( dispatcher .send(DispatchCommand::Publish { topic: format!("{}/{}", registers_prefix, name), - payload: payload, + payload, }) .await .unwrap(); diff --git a/src/modbus/config.rs b/src/modbus/config.rs index 9210fdc..ae27b36 100644 --- a/src/modbus/config.rs +++ b/src/modbus/config.rs @@ -53,7 +53,7 @@ fn default_modbus_parity() -> tokio_serial::Parity { tokio_serial::Parity::None } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "lowercase", default)] pub struct RegisterNumericAdjustment { pub scale: i8, // powers of 10 (0 = no adjustment, 1 = x10, -1 = /10) @@ -61,15 +61,6 @@ pub struct RegisterNumericAdjustment { // precision: Option, } -impl Default for RegisterNumericAdjustment { - fn default() -> Self { - Self { - scale: 0, - offset: 0, - } - } -} - #[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum RegisterNumeric { @@ -185,16 +176,10 @@ impl RegisterValueType { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] #[serde(transparent)] pub struct Swap(pub bool); -impl Default for Swap { - fn default() -> Self { - Self(false) - } -} - trait IsDefault { fn is_default(&self) -> bool; } diff --git a/src/modbus/mod.rs b/src/modbus/mod.rs index a73b5db..92a55bf 100644 --- a/src/modbus/mod.rs +++ b/src/modbus/mod.rs @@ -1,8 +1,6 @@ use rust_decimal::{prelude::FromPrimitive, Decimal}; use serde::Serialize; -use crate::modbus::config::RegisterNumericAdjustment; - use self::config::{Register, RegisterValueType}; pub mod config; @@ -26,7 +24,7 @@ pub type UnitId = tokio_modbus::prelude::SlaveId; pub type Unit = tokio_modbus::prelude::Slave; impl RegisterValueType { - pub fn from_words(&self, words: &[u16]) -> serde_json::Value { + pub fn parse_words(&self, words: &[u16]) -> serde_json::Value { use self::config::RegisterValueType as T; use self::config::{RegisterArray, RegisterNumeric as N, RegisterString}; use serde_json::json; @@ -96,7 +94,7 @@ impl RegisterValueType { } } T::String(RegisterString { .. }) => { - json!(String::from_utf16_lossy(&words)) + json!(String::from_utf16_lossy(words)) } T::Array(RegisterArray { .. }) => todo!(), } @@ -104,8 +102,8 @@ impl RegisterValueType { } impl Register { - pub fn from_words(&self, words: &[u16]) -> serde_json::Value { - self.parse.value_type.from_words(words) + pub fn parse_words(&self, words: &[u16]) -> serde_json::Value { + self.parse.value_type.parse_words(words) } pub fn apply_swaps(&self, words: &[u16]) -> Vec { @@ -148,5 +146,5 @@ fn test_parse_1() { }, }; - assert_eq!(reg.from_words(&vec![843, 0]), json!(843)); + assert_eq!(reg.parse_words(&vec![843, 0]), json!(843)); }