From e6f0b1836163db797a9fe54ccf6d81224b3ab586 Mon Sep 17 00:00:00 2001 From: Hlars Date: Sun, 10 Jul 2022 15:24:37 +0200 Subject: [PATCH] implemented PartialEq, Eq for all types --- src/sps_datatypes.rs | 20 +++++++++----------- src/types/boolean.rs | 14 ++++++-------- src/types/date_time.rs | 2 +- src/types/dint.rs | 12 +++++------- src/types/int.rs | 12 +++++------- src/types/real.rs | 12 +++++------- src/types/string.rs | 22 +++++++++++++--------- src/types/time.rs | 12 +++++------- src/types/udint.rs | 12 +++++------- src/types/uint.rs | 12 +++++------- 10 files changed, 59 insertions(+), 71 deletions(-) diff --git a/src/sps_datatypes.rs b/src/sps_datatypes.rs index 2129006..a9ea3f1 100644 --- a/src/sps_datatypes.rs +++ b/src/sps_datatypes.rs @@ -1,3 +1,4 @@ +use super::errors::Errors; use super::types::boolean::*; use super::types::date_time::*; use super::types::dint::*; @@ -7,7 +8,6 @@ use super::types::string::*; use super::types::time::*; use super::types::udint::*; use super::types::uint::*; -use super::errors::Errors; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; @@ -20,7 +20,7 @@ pub struct DataInfo { pub bit: Option, } -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub enum SPSDataTypes { Boolean(BooleanType), Int(IntType), @@ -45,13 +45,11 @@ impl TryFrom for SPSDataTypes { "time" => Ok(Self::Time(TimeType::new(data.byte, data.bit)?)), "udint" => Ok(Self::UDInt(UDIntType::new(data.byte, data.bit)?)), "uint" => Ok(Self::UInt(UIntType::new(data.byte, data.bit)?)), - "string" => { - match data.length { - Some(number) => Ok(Self::String(StringType::new(number, data.byte, data.bit)?)), - None => Err(Errors::MissingLength) - } - } - _ => Err(Errors::UnknownType) + "string" => match data.length { + Some(number) => Ok(Self::String(StringType::new(number, data.byte, data.bit)?)), + None => Err(Errors::MissingLength), + }, + _ => Err(Errors::UnknownType), } } } @@ -97,7 +95,7 @@ impl SPSDataTypes { pub fn into_string(self) -> String { self.into().into_string() - } + } } impl From for UnparsedSPSDataType { @@ -135,7 +133,7 @@ pub struct SQLDataType { pub mysql_type: String, } -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct BitPosition { pub byte: u32, pub bit: Option, diff --git a/src/types/boolean.rs b/src/types/boolean.rs index bf29f6b..55396d4 100644 --- a/src/types/boolean.rs +++ b/src/types/boolean.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct BooleanType { position: BitPosition, } @@ -15,16 +15,14 @@ impl BooleanType { match bit { Some(bit_) => { if bit_ < 8 { - Ok( - BooleanType { - position: BitPosition { byte, bit }, - } - ) + Ok(BooleanType { + position: BitPosition { byte, bit }, + }) } else { Err(Errors::InvalidBit) } - }, - None => Err(Errors::MissingBit) + } + None => Err(Errors::MissingBit), } } } diff --git a/src/types/date_time.rs b/src/types/date_time.rs index 95820ac..82740e5 100644 --- a/src/types/date_time.rs +++ b/src/types/date_time.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct DateTimeType { length: u32, position: BitPosition, diff --git a/src/types/dint.rs b/src/types/dint.rs index 425346a..f16be07 100644 --- a/src/types/dint.rs +++ b/src/types/dint.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct DIntType { length: u32, position: BitPosition, @@ -14,12 +14,10 @@ impl DIntType { const LEN: usize = 4; pub fn new(byte: u32, bit: Option) -> Result { - Ok( - DIntType { - length: Self::LEN as u32, - position: BitPosition { byte, bit }, - } - ) + Ok(DIntType { + length: Self::LEN as u32, + position: BitPosition { byte, bit }, + }) } } diff --git a/src/types/int.rs b/src/types/int.rs index 525246f..ed2e3d3 100644 --- a/src/types/int.rs +++ b/src/types/int.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct IntType { length: u32, position: BitPosition, @@ -13,12 +13,10 @@ pub struct IntType { impl IntType { const LEN: usize = 2; pub fn new(byte: u32, bit: Option) -> Result { - Ok( - IntType { - length: Self::LEN as u32, - position: BitPosition { byte, bit }, - } - ) + Ok(IntType { + length: Self::LEN as u32, + position: BitPosition { byte, bit }, + }) } } diff --git a/src/types/real.rs b/src/types/real.rs index 5785df3..275ea10 100644 --- a/src/types/real.rs +++ b/src/types/real.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct RealType { length: u32, position: BitPosition, @@ -13,12 +13,10 @@ pub struct RealType { impl RealType { const LEN: usize = 4; pub fn new(byte: u32, bit: Option) -> Result { - Ok( - RealType { - length: Self::LEN as u32, - position: BitPosition { byte, bit }, - } - ) + Ok(RealType { + length: Self::LEN as u32, + position: BitPosition { byte, bit }, + }) } } diff --git a/src/types/string.rs b/src/types/string.rs index 0bb1292..3cda59a 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -4,19 +4,17 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct StringType { length: u32, position: BitPosition, } impl StringType { pub fn new(length: u32, byte: u32, bit: Option) -> Result { - Ok( - StringType { - length, - position: BitPosition { byte, bit }, - } - ) + Ok(StringType { + length, + position: BitPosition { byte, bit }, + }) } } impl DataEvaluation for StringType { @@ -44,7 +42,10 @@ impl DataEvaluation for StringType { fn parse_serial(&self, data: &[&str]) -> Result { Ok(data .get((self.position.byte) as usize) - .ok_or(Errors::ConversionError{type_string: self.into_string(), byte: self.position.byte})? + .ok_or(Errors::ConversionError { + type_string: self.into_string(), + byte: self.position.byte, + })? .to_string()) } fn parse_s7(&self, data: &[u8]) -> Result { @@ -53,7 +54,10 @@ impl DataEvaluation for StringType { // Byte 3: 1. Nutzzeichen let bytes = data .get((self.position.byte) as usize..(self.position.byte + self.length) as usize) - .ok_or(Errors::ConversionError{type_string: self.into_string(), byte: self.position.byte})?; + .ok_or(Errors::ConversionError { + type_string: self.into_string(), + byte: self.position.byte, + })?; // let bytes = bytes.unwrap(); match String::from_utf8(bytes[2..bytes[1] as usize + 2].to_vec()) { diff --git a/src/types/time.rs b/src/types/time.rs index b9c3f06..2289417 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct TimeType { length: u32, position: BitPosition, @@ -12,12 +12,10 @@ pub struct TimeType { impl TimeType { const LEN: usize = 4; pub fn new(byte: u32, bit: Option) -> Result { - Ok( - TimeType { - length: Self::LEN as u32, - position: BitPosition { byte, bit }, - } - ) + Ok(TimeType { + length: Self::LEN as u32, + position: BitPosition { byte, bit }, + }) } } impl DataEvaluation for TimeType { diff --git a/src/types/udint.rs b/src/types/udint.rs index 6a74ac5..c944450 100644 --- a/src/types/udint.rs +++ b/src/types/udint.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct UDIntType { length: u32, position: BitPosition, @@ -12,12 +12,10 @@ pub struct UDIntType { impl UDIntType { const LEN: usize = 4; pub fn new(byte: u32, bit: Option) -> Result { - Ok( - UDIntType { - length: Self::LEN as u32, - position: BitPosition { byte, bit }, - } - ) + Ok(UDIntType { + length: Self::LEN as u32, + position: BitPosition { byte, bit }, + }) } } impl DataEvaluation for UDIntType { diff --git a/src/types/uint.rs b/src/types/uint.rs index 29a6318..60529bb 100644 --- a/src/types/uint.rs +++ b/src/types/uint.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use super::super::errors::*; use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType}; -#[derive(Debug, Serialize, Deserialize, Copy, Clone)] +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub struct UIntType { length: u32, position: BitPosition, @@ -12,12 +12,10 @@ pub struct UIntType { impl UIntType { const LEN: usize = 2; pub fn new(byte: u32, bit: Option) -> Result { - Ok( - UIntType { - length: Self::LEN as u32, - position: BitPosition { byte, bit }, - } - ) + Ok(UIntType { + length: Self::LEN as u32, + position: BitPosition { byte, bit }, + }) } } impl DataEvaluation for UIntType {