implemented PartialEq, Eq for all types

This commit is contained in:
Hlars 2022-07-10 15:24:37 +02:00
parent d3ef430bdb
commit e6f0b18361
10 changed files with 59 additions and 71 deletions

View File

@ -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<u32>,
}
#[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<DataInfo> 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<SPSDataTypes> 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<u32>,

View File

@ -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),
}
}
}

View File

@ -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,

View File

@ -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<u32>) -> Result<Self, Errors> {
Ok(
DIntType {
length: Self::LEN as u32,
position: BitPosition { byte, bit },
}
)
Ok(DIntType {
length: Self::LEN as u32,
position: BitPosition { byte, bit },
})
}
}

View File

@ -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<u32>) -> Result<Self, Errors> {
Ok(
IntType {
length: Self::LEN as u32,
position: BitPosition { byte, bit },
}
)
Ok(IntType {
length: Self::LEN as u32,
position: BitPosition { byte, bit },
})
}
}

View File

@ -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<u32>) -> Result<Self, Errors> {
Ok(
RealType {
length: Self::LEN as u32,
position: BitPosition { byte, bit },
}
)
Ok(RealType {
length: Self::LEN as u32,
position: BitPosition { byte, bit },
})
}
}

View File

@ -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<u32>) -> Result<Self, Errors> {
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<String> {
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<String> {
@ -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()) {

View File

@ -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<u32>) -> Result<Self, Errors> {
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 {

View File

@ -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<u32>) -> Result<Self, Errors> {
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 {

View File

@ -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<u32>) -> Result<Self, Errors> {
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 {