ssp/get_barcode_data/
response.rs

1use crate::{
2    impl_default, impl_message_from_buf, impl_response_ops, impl_var_message_ops, len, std::fmt,
3    BarcodeTicketStatus, MessageOps, MessageType, ResponseOps,
4};
5
6mod index {
7    pub const STATUS: usize = 4;
8    pub const DATA_LEN: usize = 5;
9    pub const DATA: usize = 6;
10}
11
12/// GetBarcodeData - Response (0x27)
13///
14/// Represents a response to an [GetBarcodeDataCommand](crate::GetBarcodeDataCommand) message.
15#[repr(C)]
16#[derive(Clone, Copy, Debug, PartialEq)]
17pub struct GetBarcodeDataResponse {
18    buf: [u8; len::GET_BARCODE_DATA_RESPONSE],
19}
20
21impl GetBarcodeDataResponse {
22    /// Creates a new [GetBarcodeDataResponse] message.
23    pub fn new() -> Self {
24        let mut msg = Self {
25            buf: [0u8; len::GET_BARCODE_DATA_RESPONSE],
26        };
27
28        msg.init();
29
30        msg
31    }
32
33    /// Gets the [BarcodeTicketStatus].
34    pub fn ticket_status(&self) -> BarcodeTicketStatus {
35        self.buf[index::STATUS].into()
36    }
37
38    /// Gets the length of the barcode data.
39    pub fn barcode_data_len(&self) -> usize {
40        self.buf[index::DATA_LEN] as usize
41    }
42
43    /// Gets the barcode data.
44    pub fn barcode_data(&self) -> &[u8] {
45        let data_end = index::DATA + self.barcode_data_len();
46        self.buf[index::DATA..data_end].as_ref()
47    }
48}
49
50impl_default!(GetBarcodeDataResponse);
51impl_message_from_buf!(GetBarcodeDataResponse);
52impl_var_message_ops!(GetBarcodeDataResponse, MessageType::GetBarcodeData);
53impl_response_ops!(GetBarcodeDataResponse);
54
55impl fmt::Display for GetBarcodeDataResponse {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        let stx = self.stx();
58        let seqid = self.sequence_id();
59        let len = self.data_len();
60        let status = self.response_status();
61        let ticket_status = self.ticket_status();
62        let data = self.barcode_data();
63        let crc = self.checksum();
64
65        write!(f, "STX: 0x{stx:02x} | SEQID: {seqid} | LEN: 0x{len:02x} | Response status: {status} | Barcode ticket status: {ticket_status} | Barcode data: {data:x?} | CRC-16: 0x{crc:04x}")
66    }
67}