rust-s7-datatypes/src/types/time.rs

56 lines
1.5 KiB
Rust

use anyhow::Result;
use serde::{Deserialize, Serialize};
use super::super::errors::*;
use super::super::sps_datatypes::{BitPosition, DataEvaluation, UnparsedSPSDataType};
#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Hash)]
pub struct TimeType {
length: u32,
position: BitPosition,
}
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 },
})
}
}
impl DataEvaluation for TimeType {
fn into_unparsed(&self) -> UnparsedSPSDataType {
UnparsedSPSDataType {
data_type: self.into_string(),
data_byte: self.position.byte,
data_bit: self.position.bit,
data_length: None,
}
}
fn into_string(&self) -> String {
"time".to_string()
}
fn get_end_byte(&self) -> u32 {
self.position.byte + self.length
}
fn get_byte_positon(&self) -> u32 {
self.position.byte
}
fn get_length(&self) -> u32 {
self.length
}
fn parse_serial(&self, data: &[&str]) -> Result<String> {
Ok(data
.get((self.position.byte) as usize)
.ok_or_else(|| serial_error(self.into_string(), self.position.byte))?
.to_string())
}
fn parse_s7(&self, _data: &[u8]) -> Result<String> {
Ok("time".to_string())
}
fn sql_equivalent(&self) -> &str {
r"BIGINT UNSIGNED DEFAULT 0"
}
}