use crate::{
Endian, HasFixedCommandId, MessageExt, ReadExt, ReadFromPayload,
Result, WithFixedPayloadLength, WriteExt,
};
use std::io::{Read, Write};
#[derive(Clone, Debug, PartialEq)]
pub struct Notification {
pub job_position: u16,
}
impl From<Notification> for super::Notification {
fn from(m: Notification) -> Self {
super::Notification::JobPositionAt(m)
}
}
impl WithFixedPayloadLength for Notification {
const FIXED_PAYLOAD_LENGTH: u16 = u16::FIXED_PAYLOAD_LENGTH;
}
impl HasFixedCommandId for Notification {
const COMMAND_ID: u16 = 0x8801;
}
impl MessageExt for Notification {
fn payload_length(&self) -> u16 {
Self::FIXED_PAYLOAD_LENGTH
}
fn write_payload(&self, w: &mut dyn Write) -> Result<()> {
w.write_u16::<Endian>(self.job_position)?;
Ok(())
}
}
impl ReadFromPayload for Notification {
fn read_from_payload<R: Read>(
r: &mut R,
payload_length: u16,
) -> Result<Self> {
Self::verify_payload_length(
payload_length,
"job_position_at_notification",
)?;
let job_position = r.read_u16::<Endian>()?;
Ok(Self { job_position })
}
}
#[cfg(test)]
mod test_notification {
use super::*;
#[test]
fn write_to() {
let mut buffer = Vec::new();
let options: u8 = 0x45;
let sequence: u8 = 0x93;
let message = Notification {
job_position: 0x1234,
};
message.write_to(&mut buffer, options, sequence).unwrap();
assert_eq!(
buffer,
[
0x45, 0x93, 0x01, 0x88, 0x02, 0x00, 0x34, 0x12, ]
);
}
#[test]
fn read_from_payload() {
let buffer = vec![0x34, 0x12];
let len = buffer.len() as u16;
let message =
Notification::read_from_payload(&mut buffer.as_slice(), len)
.unwrap();
assert_eq!(
message,
Notification {
job_position: 0x1234,
}
);
}
}