rust-s7-datatypes/src/sps_datatypes.rs
2021-10-16 17:17:09 +02:00

106 lines
2.6 KiB
Rust

use super::types::boolean::*;
use super::types::date_time::*;
use super::types::dint::*;
use super::types::int::*;
use super::types::real::*;
use super::types::string::*;
use super::types::time::*;
use super::types::udint::*;
use super::types::uint::*;
use serde::{Deserialize, Serialize};
use anyhow::Result;
#[derive(Debug, Serialize, Copy, Clone)]
pub enum SPSDataTypes {
Boolean(BooleanType),
Int(IntType),
DInt(DIntType),
Real(RealType),
Time(TimeType),
DateTime(DateTimeType),
UDInt(UDIntType),
UInt(UIntType),
String(StringType),
}
impl SPSDataTypes {
fn into(self) -> Box<dyn DataEvaluation> {
match self {
Self::Boolean(raw) => Box::new(raw),
Self::DInt(raw) => Box::new(raw),
Self::Int(raw) => Box::new(raw),
Self::Real(raw) => Box::new(raw),
Self::Time(raw) => Box::new(raw),
Self::DateTime(raw) => Box::new(raw),
Self::UDInt(raw) => Box::new(raw),
Self::UInt(raw) => Box::new(raw),
Self::String(raw) => Box::new(raw),
}
}
pub fn get_end_byte(self) -> u32 {
self.into().get_end_byte()
}
pub fn get_byte_positon(self) -> u32 {
self.into().get_byte_positon()
}
pub fn get_length(self) -> u32 {
self.into().get_length()
}
pub fn parse_serial_value(self, data: &[&str]) -> Result<String> {
self.into().parse_serial(data)
}
pub fn parse_s7_value(self, data: &[u8]) -> Result<String> {
self.into().parse_s7(data)
}
pub fn create_sql_data_type(self) -> SQLDataType {
self.into().create_sql_data_type()
}
}
impl Into<UnparsedSPSDataType> for SPSDataTypes {
fn into(self) -> UnparsedSPSDataType {
self.into().into_unparsed()
}
}
pub struct UnparsedSPSDataType {
pub data_type: String,
pub data_byte: u32,
pub data_bit: Option<u32>,
pub data_length: Option<u32>,
}
pub trait DataEvaluation {
fn into_unparsed(&self) -> UnparsedSPSDataType;
fn get_end_byte(&self) -> u32;
fn get_byte_positon(&self) -> u32;
fn get_length(&self) -> u32;
fn parse_serial(&self, data: &[&str]) -> Result<String>;
fn parse_s7(&self, data: &[u8]) -> Result<String>;
fn sql_equivalent(&self) -> &str;
fn create_sql_data_type(&self) -> SQLDataType {
SQLDataType {
mysql_type: self.sql_equivalent().to_owned(),
}
}
}
#[derive(Debug)]
pub struct SQLDataType {
pub mysql_type: String,
}
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
pub struct BitPosition {
pub byte: u32,
pub bit: Option<u32>,
}