1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use crate::{
    impl_default, impl_message_from_buf, impl_response_ops, impl_var_message_ops, len, std::fmt,
    BarcodeCharacters, BarcodeConfiguration, BarcodeEnabledStatus, BarcodeFormat,
    BarcodeHardwareStatus, MessageOps, MessageType, ResponseOps,
};

mod index {
    pub const HARDWARE_STATUS: usize = 4;
    pub const ENABLED_STATUS: usize = 5;
    pub const FORMAT: usize = 6;
    pub const CHARACTERS: usize = 7;
}

/// SetBarcodeReaderConfiguration - Response (0x24)
///
/// Represents a response to an [SetBarcodeReaderConfigurationCommand](crate::SetBarcodeReaderConfigurationCommand) message.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SetBarcodeReaderConfigurationResponse {
    buf: [u8; len::SET_BARCODE_READER_CONFIGURATION_RESPONSE],
}

impl SetBarcodeReaderConfigurationResponse {
    /// Creates a new [SetBarcodeReaderConfigurationResponse] message.
    pub fn new() -> Self {
        let mut msg = Self {
            buf: [0u8; len::SET_BARCODE_READER_CONFIGURATION_RESPONSE],
        };

        msg.init();

        msg
    }

    /// Gets the [BarcodeConfiguration].
    pub fn configuration(&self) -> BarcodeConfiguration {
        if self.response_status().is_ok() {
            self.buf[index::HARDWARE_STATUS..=index::CHARACTERS]
                .as_ref()
                .into()
        } else {
            BarcodeConfiguration::new()
        }
    }

    /// Gets the [BarcodeHardwareStatus].
    pub fn hardware_status(&self) -> BarcodeHardwareStatus {
        if self.response_status().is_ok() {
            self.buf[index::HARDWARE_STATUS].into()
        } else {
            BarcodeHardwareStatus::None
        }
    }

    /// Gets the [BarcodeEnabledStatus].
    pub fn enabled_status(&self) -> BarcodeEnabledStatus {
        if self.response_status().is_ok() {
            self.buf[index::ENABLED_STATUS].into()
        } else {
            BarcodeEnabledStatus::None
        }
    }

    /// Gets the [BarcodeFormat].
    pub fn format(&self) -> BarcodeFormat {
        if self.response_status().is_ok() {
            self.buf[index::FORMAT].into()
        } else {
            BarcodeFormat::None
        }
    }

    /// Gets the number of [BarcodeCharacters].
    pub fn num_characters(&self) -> BarcodeCharacters {
        if self.response_status().is_ok() {
            self.buf[index::CHARACTERS].into()
        } else {
            BarcodeCharacters::from(0)
        }
    }
}

impl_default!(SetBarcodeReaderConfigurationResponse);
impl_message_from_buf!(SetBarcodeReaderConfigurationResponse);
impl_var_message_ops!(
    SetBarcodeReaderConfigurationResponse,
    MessageType::SetBarcodeReaderConfiguration
);
impl_response_ops!(SetBarcodeReaderConfigurationResponse);

impl fmt::Display for SetBarcodeReaderConfigurationResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let stx = self.stx();
        let seqid = self.sequence_id();
        let len = self.data_len();
        let status = self.response_status();
        let config = self.configuration();
        let crc = self.checksum();

        write!(f, "STX: 0x{stx:02x} | SEQID: {seqid} | LEN: 0x{len:02x} | Response status: {status} | {config} | CRC-16: 0x{crc:04x}")
    }
}