use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes, Unaligned};
#[allow(unused)]
pub mod request {
include!(concat!(env!("OUT_DIR"), "/pdu_req.rs"));
#[cfg(test)]
mod tests {
use hex_literal::hex;
use zerocopy::IntoBytes;
use crate::frame::FrameBuilder;
use super::*;
#[test]
fn test_read_holding_registers() {
let frame = FrameBuilder::with_pdu(
0x01,
ReadHoldings::new()
.with_n_registers(0x03E8)
.with_starting_register(0x1001),
)
.build();
assert_eq!(frame.as_bytes(), hex!("01 03 10 01 03 E8 10 74"),);
}
}
}
pub mod response {
include!(concat!(env!("OUT_DIR"), "/pdu_res.rs"));
}
pub trait Pdu: Unaligned + Immutable + IntoBytes + TryFromBytes + KnownLayout {
const FUNCTION_CODE: u8;
const DEFAULT: Self;
}
pub trait Response<Request>: Pdu {
type Data;
fn matches_request(&self, request: &Request) -> bool;
fn into_data(self) -> Self::Data;
}
#[derive(Debug, Clone, Copy, thiserror::Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[error("CRC validation failed")]
pub struct CrcError;
#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ValidationError {
#[error(transparent)]
Crc(#[from] CrcError),
#[error("unexpected response")]
UnexpectedResponse,
}