From 5d131a18b129cab8df89a97d3802307e3f3764bb Mon Sep 17 00:00:00 2001 From: Hlars Date: Tue, 9 Nov 2021 21:32:53 +0100 Subject: [PATCH] various changes --- src/sps_datatypes.rs | 7 ++++--- src/types/boolean.rs | 6 +++--- src/types/date_time.rs | 6 +++--- src/types/dint.rs | 7 +++---- src/types/int.rs | 6 +++--- src/types/real.rs | 6 +++--- src/types/string.rs | 12 +++++++----- src/types/time.rs | 6 +++--- src/types/udint.rs | 6 +++--- src/types/uint.rs | 6 +++--- 10 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/sps_datatypes.rs b/src/sps_datatypes.rs index b652b10..a0ff6e1 100644 --- a/src/sps_datatypes.rs +++ b/src/sps_datatypes.rs @@ -96,9 +96,9 @@ impl SPSDataTypes { } } -impl Into for SPSDataTypes { - fn into(self) -> UnparsedSPSDataType { - self.into().into_unparsed() +impl From for UnparsedSPSDataType { + fn from(datatype: SPSDataTypes) -> UnparsedSPSDataType { + datatype.into().into_unparsed() } } @@ -111,6 +111,7 @@ pub struct UnparsedSPSDataType { pub trait DataEvaluation { fn into_unparsed(&self) -> UnparsedSPSDataType; + fn into_string(&self) -> String; fn get_end_byte(&self) -> u32; fn get_byte_positon(&self) -> u32; fn get_length(&self) -> u32; diff --git a/src/types/boolean.rs b/src/types/boolean.rs index e510034..bf29f6b 100644 --- a/src/types/boolean.rs +++ b/src/types/boolean.rs @@ -27,9 +27,6 @@ impl BooleanType { None => Err(Errors::MissingBit) } } - fn into_string(self) -> String { - "boolean".to_string() - } } impl DataEvaluation for BooleanType { @@ -41,6 +38,9 @@ impl DataEvaluation for BooleanType { data_length: None, } } + fn into_string(&self) -> String { + "boolean".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte } diff --git a/src/types/date_time.rs b/src/types/date_time.rs index a65a235..95820ac 100644 --- a/src/types/date_time.rs +++ b/src/types/date_time.rs @@ -17,9 +17,6 @@ impl DateTimeType { position: BitPosition { byte, bit }, } } - fn into_string(self) -> String { - "datetime".to_string() - } fn assert_range_inclusive(&self, input: u8, min: u8, max: u8, field: &str) -> Result { if input < min { @@ -75,6 +72,9 @@ impl DataEvaluation for DateTimeType { data_length: None, } } + fn into_string(&self) -> String { + "datetime".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length } diff --git a/src/types/dint.rs b/src/types/dint.rs index 7d485c4..425346a 100644 --- a/src/types/dint.rs +++ b/src/types/dint.rs @@ -21,10 +21,6 @@ impl DIntType { } ) } - - fn into_string(self) -> String { - "dint".to_string() - } } impl DataEvaluation for DIntType { @@ -36,6 +32,9 @@ impl DataEvaluation for DIntType { data_length: None, } } + fn into_string(&self) -> String { + "dint".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length } diff --git a/src/types/int.rs b/src/types/int.rs index 13c130e..525246f 100644 --- a/src/types/int.rs +++ b/src/types/int.rs @@ -20,9 +20,6 @@ impl IntType { } ) } - fn into_string(self) -> String { - "int".to_string() - } } impl DataEvaluation for IntType { @@ -34,6 +31,9 @@ impl DataEvaluation for IntType { data_length: None, } } + fn into_string(&self) -> String { + "int".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length } diff --git a/src/types/real.rs b/src/types/real.rs index 41e3865..5785df3 100644 --- a/src/types/real.rs +++ b/src/types/real.rs @@ -20,9 +20,6 @@ impl RealType { } ) } - fn into_string(self) -> String { - "real".to_string() - } } impl DataEvaluation for RealType { @@ -34,6 +31,9 @@ impl DataEvaluation for RealType { data_length: None, } } + fn into_string(&self) -> String { + "real".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length } diff --git a/src/types/string.rs b/src/types/string.rs index 20a367d..0bb1292 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -18,9 +18,6 @@ impl StringType { } ) } - fn into_string(self) -> String { - "string".to_string() - } } impl DataEvaluation for StringType { fn into_unparsed(&self) -> UnparsedSPSDataType { @@ -31,6 +28,9 @@ impl DataEvaluation for StringType { data_length: Some(self.length), } } + fn into_string(&self) -> String { + "string".to_string() + } fn get_end_byte(&self) -> u32 { // first two bytes are header bytes self.position.byte + self.length + 2 @@ -54,6 +54,8 @@ impl DataEvaluation for StringType { 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})?; + + // let bytes = bytes.unwrap(); match String::from_utf8(bytes[2..bytes[1] as usize + 2].to_vec()) { Ok(string) => Ok(string), Err(err) => Err(anyhow!( @@ -71,7 +73,7 @@ impl DataEvaluation for StringType { } #[test] -fn test() { +fn test2() { const BYTEPOS: u32 = 5; const LEN: u32 = 20; const VAL: &str = "ich bin ein pfau"; @@ -85,10 +87,10 @@ fn test() { BYTEPOS as usize + 2..BYTEPOS as usize + LEN as usize, VAL.as_bytes().iter().cloned(), ); - let result = match test_item.parse_s7(&test_vec) { Ok(res) => res, Err(_) => "Error".to_string(), }; + println!("{:?}", result); assert_eq!(result, VAL.to_string()) } diff --git a/src/types/time.rs b/src/types/time.rs index e4238d1..b9c3f06 100644 --- a/src/types/time.rs +++ b/src/types/time.rs @@ -19,9 +19,6 @@ impl TimeType { } ) } - fn into_string(self) -> String { - "time".to_string() - } } impl DataEvaluation for TimeType { fn into_unparsed(&self) -> UnparsedSPSDataType { @@ -32,6 +29,9 @@ impl DataEvaluation for TimeType { data_length: None, } } + fn into_string(&self) -> String { + "time".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length } diff --git a/src/types/udint.rs b/src/types/udint.rs index 561168f..6a74ac5 100644 --- a/src/types/udint.rs +++ b/src/types/udint.rs @@ -19,9 +19,6 @@ impl UDIntType { } ) } - fn into_string(self) -> String { - "udint".to_string() - } } impl DataEvaluation for UDIntType { fn into_unparsed(&self) -> UnparsedSPSDataType { @@ -32,6 +29,9 @@ impl DataEvaluation for UDIntType { data_length: None, } } + fn into_string(&self) -> String { + "udint".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length } diff --git a/src/types/uint.rs b/src/types/uint.rs index ab66f96..29a6318 100644 --- a/src/types/uint.rs +++ b/src/types/uint.rs @@ -19,9 +19,6 @@ impl UIntType { } ) } - fn into_string(self) -> String { - "uint".to_string() - } } impl DataEvaluation for UIntType { fn into_unparsed(&self) -> UnparsedSPSDataType { @@ -32,6 +29,9 @@ impl DataEvaluation for UIntType { data_length: None, } } + fn into_string(&self) -> String { + "uint".to_string() + } fn get_end_byte(&self) -> u32 { self.position.byte + self.length }