various changes

This commit is contained in:
Hlars 2021-11-09 21:32:53 +01:00
parent 6230817405
commit 5d131a18b1
10 changed files with 35 additions and 33 deletions

View File

@ -96,9 +96,9 @@ impl SPSDataTypes {
}
}
impl Into<UnparsedSPSDataType> for SPSDataTypes {
fn into(self) -> UnparsedSPSDataType {
self.into().into_unparsed()
impl From<SPSDataTypes> 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;

View File

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

View File

@ -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<u8> {
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
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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