use crate::std;
use std::fmt;
use crate::{
impl_message_ops, impl_omnibus_nop_reply, len::QUERY_APPLICATION_PART_NUMBER_REPLY,
ApplicationPartNumber, MessageOps, MessageType, PartVersion, ProjectNumber,
};
pub mod index {
pub const PROJECT_NUM: usize = 3;
pub const VERSION: usize = 9;
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct QueryApplicationPartNumberReply {
buf: [u8; QUERY_APPLICATION_PART_NUMBER_REPLY],
}
impl QueryApplicationPartNumberReply {
pub fn new() -> Self {
let mut message = Self {
buf: [0u8; QUERY_APPLICATION_PART_NUMBER_REPLY],
};
message.init();
message.set_message_type(MessageType::AuxCommand);
message
}
pub fn application_part_number(&self) -> ApplicationPartNumber {
self.buf[index::PROJECT_NUM..self.etx_index()]
.as_ref()
.into()
}
pub fn project_number(&self) -> ProjectNumber {
self.buf[index::PROJECT_NUM..index::VERSION].as_ref().into()
}
pub fn version(&self) -> PartVersion {
self.buf[index::VERSION..self.etx_index()].as_ref().into()
}
}
impl_message_ops!(QueryApplicationPartNumberReply);
impl_omnibus_nop_reply!(QueryApplicationPartNumberReply);
impl fmt::Display for QueryApplicationPartNumberReply {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AckNak: {}, DeviceType: {}, MessageType: {}, ApplicationPartNumber: {}, ProjectNumber: {}, Version: {}",
self.acknak(),
self.device_type(),
self.message_type(),
self.application_part_number(),
self.project_number(),
self.version(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CheckDigit;
use crate::Result;
#[test]
#[rustfmt::skip]
fn test_query_application_part_number_reply_from_buf() -> Result<()> {
let msg_bytes = [
0x02, 0x0e, 0x60,
b'2', b'8', b'0', b'0', b'0',
b'0',
b'1', b'2', b'3',
0x03, 0x54,
];
let mut msg = QueryApplicationPartNumberReply::new();
msg.from_buf(msg_bytes.as_ref())?;
assert_eq!(msg.message_type(), MessageType::AuxCommand);
let exp_project_number = ProjectNumber::type1(28_000, CheckDigit::from(b'0'));
let exp_part_version = PartVersion::from(b"123");
let exp_app_part_number = ApplicationPartNumber::new(exp_project_number, exp_part_version);
assert_eq!(msg.application_part_number(), exp_app_part_number);
assert_eq!(msg.project_number(), exp_project_number);
assert_eq!(msg.version(), exp_part_version);
assert_eq!(msg.version().as_string().as_str(), "V1.23");
let msg_bytes = [
0x02, 0x0e, 0x60,
b'2', b'8', b'6', b'0', b'0', b'0',
b'1', b'2', b'3',
0x03, 0x52,
];
msg.from_buf(msg_bytes.as_ref())?;
assert_eq!(msg.message_type(), MessageType::AuxCommand);
let exp_project_number = ProjectNumber::type2(286_000);
let exp_part_version = PartVersion::from(b"123");
let exp_app_part_number = ApplicationPartNumber::new(exp_project_number, exp_part_version);
assert_eq!(msg.application_part_number(), exp_app_part_number);
assert_eq!(msg.project_number(), exp_project_number);
assert_eq!(msg.version(), exp_part_version);
assert_eq!(msg.version().as_string().as_str(), "V1.23");
Ok(())
}
}