servicepoint/
command_code.rs1#[repr(u16)]
3#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
4#[allow(missing_docs)]
5pub enum CommandCode {
6 Clear = 0x0002,
7 Cp437Data = 0x0003,
8 CharBrightness = 0x0005,
9 Brightness = 0x0007,
10 HardReset = 0x000b,
11 FadeOut = 0x000d,
12 #[deprecated]
13 BitmapLegacy = 0x0010,
14 BitmapLinear = 0x0012,
15 BitmapLinearWinUncompressed = 0x0013,
16 BitmapLinearAnd = 0x0014,
17 BitmapLinearOr = 0x0015,
18 BitmapLinearXor = 0x0016,
19 #[cfg(feature = "compression_zlib")]
20 BitmapLinearWinZlib = 0x0017,
21 #[cfg(feature = "compression_bzip2")]
22 BitmapLinearWinBzip2 = 0x0018,
23 #[cfg(feature = "compression_lzma")]
24 BitmapLinearWinLzma = 0x0019,
25 Utf8Data = 0x0020,
26 #[cfg(feature = "compression_zstd")]
27 BitmapLinearWinZstd = 0x001A,
28}
29
30impl From<CommandCode> for u16 {
31 fn from(value: CommandCode) -> Self {
33 value as u16
34 }
35}
36
37#[derive(Debug, thiserror::Error, Eq, PartialEq)]
38#[error("The command code {0} is not known.")]
39pub struct InvalidCommandCodeError(pub u16);
40
41impl TryFrom<u16> for CommandCode {
42 type Error = InvalidCommandCodeError;
43
44 fn try_from(value: u16) -> Result<Self, Self::Error> {
46 match value {
47 value if value == CommandCode::Clear as u16 => {
48 Ok(CommandCode::Clear)
49 }
50 value if value == CommandCode::Cp437Data as u16 => {
51 Ok(CommandCode::Cp437Data)
52 }
53 value if value == CommandCode::CharBrightness as u16 => {
54 Ok(CommandCode::CharBrightness)
55 }
56 value if value == CommandCode::Brightness as u16 => {
57 Ok(CommandCode::Brightness)
58 }
59 value if value == CommandCode::HardReset as u16 => {
60 Ok(CommandCode::HardReset)
61 }
62 value if value == CommandCode::FadeOut as u16 => {
63 Ok(CommandCode::FadeOut)
64 }
65 #[allow(deprecated)]
66 value if value == CommandCode::BitmapLegacy as u16 => {
67 Ok(CommandCode::BitmapLegacy)
68 }
69 value if value == CommandCode::BitmapLinear as u16 => {
70 Ok(CommandCode::BitmapLinear)
71 }
72 value
73 if value == CommandCode::BitmapLinearWinUncompressed as u16 =>
74 {
75 Ok(CommandCode::BitmapLinearWinUncompressed)
76 }
77 value if value == CommandCode::BitmapLinearAnd as u16 => {
78 Ok(CommandCode::BitmapLinearAnd)
79 }
80 value if value == CommandCode::BitmapLinearOr as u16 => {
81 Ok(CommandCode::BitmapLinearOr)
82 }
83 value if value == CommandCode::BitmapLinearXor as u16 => {
84 Ok(CommandCode::BitmapLinearXor)
85 }
86 #[cfg(feature = "compression_zstd")]
87 value if value == CommandCode::BitmapLinearWinZstd as u16 => {
88 Ok(CommandCode::BitmapLinearWinZstd)
89 }
90 #[cfg(feature = "compression_lzma")]
91 value if value == CommandCode::BitmapLinearWinLzma as u16 => {
92 Ok(CommandCode::BitmapLinearWinLzma)
93 }
94 #[cfg(feature = "compression_zlib")]
95 value if value == CommandCode::BitmapLinearWinZlib as u16 => {
96 Ok(CommandCode::BitmapLinearWinZlib)
97 }
98 #[cfg(feature = "compression_bzip2")]
99 value if value == CommandCode::BitmapLinearWinBzip2 as u16 => {
100 Ok(CommandCode::BitmapLinearWinBzip2)
101 }
102 value if value == CommandCode::Utf8Data as u16 => {
103 Ok(CommandCode::Utf8Data)
104 }
105 _ => Err(InvalidCommandCodeError(value)),
106 }
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113 #[test]
114 fn clear() {
115 assert_eq!(CommandCode::try_from(0x0002), Ok(CommandCode::Clear));
116 assert_eq!(u16::from(CommandCode::Clear), 0x0002);
117 }
118
119 #[test]
120 fn cp437_data() {
121 assert_eq!(CommandCode::try_from(0x0003), Ok(CommandCode::Cp437Data));
122 assert_eq!(u16::from(CommandCode::Cp437Data), 0x0003);
123 }
124
125 #[test]
126 fn char_brightness() {
127 assert_eq!(
128 CommandCode::try_from(0x0005),
129 Ok(CommandCode::CharBrightness)
130 );
131 assert_eq!(u16::from(CommandCode::CharBrightness), 0x0005);
132 }
133
134 #[test]
135 fn brightness() {
136 assert_eq!(CommandCode::try_from(0x0007), Ok(CommandCode::Brightness));
137 assert_eq!(u16::from(CommandCode::Brightness), 0x0007);
138 }
139
140 #[test]
141 fn hard_reset() {
142 assert_eq!(CommandCode::try_from(0x000b), Ok(CommandCode::HardReset));
143 assert_eq!(u16::from(CommandCode::HardReset), 0x000b);
144 }
145
146 #[test]
147 fn fade_out() {
148 assert_eq!(CommandCode::try_from(0x000d), Ok(CommandCode::FadeOut));
149 assert_eq!(u16::from(CommandCode::FadeOut), 0x000d);
150 }
151
152 #[test]
153 #[allow(deprecated)]
154 fn bitmap_legacy() {
155 assert_eq!(
156 CommandCode::try_from(0x0010),
157 Ok(CommandCode::BitmapLegacy)
158 );
159 assert_eq!(u16::from(CommandCode::BitmapLegacy), 0x0010);
160 }
161
162 #[test]
163 fn linear() {
164 assert_eq!(
165 CommandCode::try_from(0x0012),
166 Ok(CommandCode::BitmapLinear)
167 );
168 assert_eq!(u16::from(CommandCode::BitmapLinear), 0x0012);
169 }
170
171 #[test]
172 fn linear_and() {
173 assert_eq!(
174 CommandCode::try_from(0x0014),
175 Ok(CommandCode::BitmapLinearAnd)
176 );
177 assert_eq!(u16::from(CommandCode::BitmapLinearAnd), 0x0014);
178 }
179
180 #[test]
181 fn linear_xor() {
182 assert_eq!(
183 CommandCode::try_from(0x0016),
184 Ok(CommandCode::BitmapLinearXor)
185 );
186 assert_eq!(u16::from(CommandCode::BitmapLinearXor), 0x0016);
187 }
188
189 #[test]
190 #[cfg(feature = "compression_zlib")]
191 fn bitmap_win_zlib() {
192 assert_eq!(
193 CommandCode::try_from(0x0017),
194 Ok(CommandCode::BitmapLinearWinZlib)
195 );
196 assert_eq!(u16::from(CommandCode::BitmapLinearWinZlib), 0x0017);
197 }
198
199 #[test]
200 #[cfg(feature = "compression_bzip2")]
201 fn bitmap_win_bzip2() {
202 assert_eq!(
203 CommandCode::try_from(0x0018),
204 Ok(CommandCode::BitmapLinearWinBzip2)
205 );
206 assert_eq!(u16::from(CommandCode::BitmapLinearWinBzip2), 0x0018);
207 }
208
209 #[test]
210 #[cfg(feature = "compression_lzma")]
211 fn bitmap_win_lzma() {
212 assert_eq!(
213 CommandCode::try_from(0x0019),
214 Ok(CommandCode::BitmapLinearWinLzma)
215 );
216 assert_eq!(u16::from(CommandCode::BitmapLinearWinLzma), 0x0019);
217 }
218
219 #[test]
220 #[cfg(feature = "compression_zstd")]
221 fn bitmap_win_zstd() {
222 assert_eq!(
223 CommandCode::try_from(0x001A),
224 Ok(CommandCode::BitmapLinearWinZstd)
225 );
226 assert_eq!(u16::from(CommandCode::BitmapLinearWinZstd), 0x001A);
227 }
228
229 #[test]
230 fn bitmap_win_uncompressed() {
231 assert_eq!(
232 CommandCode::try_from(0x0013),
233 Ok(CommandCode::BitmapLinearWinUncompressed)
234 );
235 assert_eq!(u16::from(CommandCode::BitmapLinearWinUncompressed), 0x0013);
236 }
237
238 #[test]
239 fn utf8_data() {
240 assert_eq!(CommandCode::try_from(0x0020), Ok(CommandCode::Utf8Data));
241 assert_eq!(u16::from(CommandCode::Utf8Data), 0x0020);
242 }
243
244 #[test]
245 fn linear_or() {
246 assert_eq!(
247 CommandCode::try_from(0x0015),
248 Ok(CommandCode::BitmapLinearOr)
249 );
250 assert_eq!(u16::from(CommandCode::BitmapLinearOr), 0x0015);
251 }
252}