#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
extern crate alloc;
pub use error::Error;
pub mod error;
pub trait FrameParser {
type Frame: core::fmt::Debug + Clone + PartialEq;
fn parse(&self, bytes: &[u8]) -> Result<Self::Frame, Error>;
fn try_parse(&self, bytes: &[u8]) -> Result<Option<(Self::Frame, usize)>, Error> {
self.parse(bytes).map(|f| Some((f, bytes.len())))
}
}
pub trait StateValidator {
type State: core::fmt::Debug + Clone + PartialEq;
fn validate(&self, state: &Self::State) -> Result<(), Error>;
}
pub trait DataNormalizer {
type Frame: core::fmt::Debug + Clone + PartialEq;
fn normalize(&self, frame: &Self::Frame) -> Result<tpt_protocol_tup::DataPoint, Error>;
fn normalize_all(
&self,
frame: &Self::Frame,
) -> Result<alloc::vec::Vec<tpt_protocol_tup::DataPoint>, Error> {
Ok(alloc::vec![self.normalize(frame)?])
}
fn normalize_batch(
&self,
frames: &[Self::Frame],
) -> Result<alloc::vec::Vec<tpt_protocol_tup::DataPoint>, Error> {
let mut out = alloc::vec::Vec::new();
for frame in frames {
out.extend(self.normalize_all(frame)?);
}
Ok(out)
}
}
pub trait FrameEncoder {
type Frame;
fn encode(&self, frame: &Self::Frame) -> Result<alloc::vec::Vec<u8>, Error>;
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub trait ProtocolPlugin: Send + 'static {
fn simulate(
self: alloc::boxed::Box<Self>,
) -> core::pin::Pin<
alloc::boxed::Box<
dyn futures_util::stream::Stream<Item = tpt_protocol_tup::DataPoint> + Send,
>,
>;
}
#[cfg(test)]
mod tests {
use super::*;
use tpt_protocol_tup::{DataPoint, Quality, SourceProtocol, TupValue};
#[derive(Debug, Clone, PartialEq)]
struct TestFrame {
value: f64,
point: &'static str,
}
struct TestParser;
impl FrameParser for TestParser {
type Frame = TestFrame;
fn parse(&self, bytes: &[u8]) -> Result<Self::Frame, Error> {
if bytes.len() < 4 {
return Err(Error::parse("too short"));
}
let val = f64::from(bytes[0]) + f64::from(bytes[1]) / 100.0;
Ok(TestFrame {
value: val,
point: "test:1",
})
}
}
struct TestNormalizer;
impl DataNormalizer for TestNormalizer {
type Frame = TestFrame;
fn normalize(&self, frame: &Self::Frame) -> Result<DataPoint, Error> {
Ok(DataPoint::new(
SourceProtocol::Modbus,
"test-device",
frame.point,
1753900000000,
TupValue::Float(frame.value),
Quality::Good,
))
}
}
struct TestValidator;
impl StateValidator for TestValidator {
type State = bool;
fn validate(&self, state: &Self::State) -> Result<(), Error> {
if *state {
Ok(())
} else {
Err(Error::state("connection lost"))
}
}
}
#[test]
fn frame_parser_basic() {
let parser = TestParser;
let frame = parser.parse(&[42, 50, 0, 0]).unwrap();
assert!((frame.value - 42.5).abs() < 0.001);
assert_eq!(frame.point, "test:1");
}
#[test]
fn frame_parser_too_short() {
let parser = TestParser;
let result = parser.parse(&[42]);
assert!(result.is_err());
}
#[test]
fn data_normalizer_roundtrip() {
let parser = TestParser;
let normalizer = TestNormalizer;
let frame = parser.parse(&[68, 50, 0, 0]).unwrap();
let dp = normalizer.normalize(&frame).unwrap();
assert_eq!(dp.source_protocol, SourceProtocol::Modbus);
let diff: f64 = 68.5 - 68.5;
assert!(diff.abs() < 0.001);
}
#[test]
fn state_validator_ok() {
let validator = TestValidator;
assert!(validator.validate(&true).is_ok());
}
#[test]
fn state_validator_fail() {
let validator = TestValidator;
assert!(validator.validate(&false).is_err());
}
}