1use std::collections::HashMap;
7
8use crate::{
9 AsciiChar,
10 Byte,
11};
12
13pub struct AsciiTable {
51 table: HashMap<Byte, AsciiChar>,
52}
53
54impl AsciiTable {
55 #[must_use]
82 #[allow(clippy::too_many_lines)]
83 pub fn new() -> Self {
84 let mut table = HashMap::new();
85
86 {
87 table.insert(
88 Byte::from(0),
89 AsciiChar::new(Byte::from(0), "CNUL", "Null character", "\\000"),
90 );
91
92 table.insert(
93 Byte::from(1),
94 AsciiChar::new(Byte::from(1), "CSOH", "Start of heading", "\\001"),
95 );
96 table.insert(
97 Byte::from(2),
98 AsciiChar::new(Byte::from(2), "CSTX", "Start of text", "\\002"),
99 );
100 table.insert(
101 Byte::from(3),
102 AsciiChar::new(Byte::from(3), "CETX", "End of text", "\\003"),
103 );
104 table.insert(
105 Byte::from(4),
106 AsciiChar::new(Byte::from(4), "CEOT", "End of transmission", "\\004"),
107 );
108 table.insert(
109 Byte::from(5),
110 AsciiChar::new(Byte::from(5), "CENQ", "Enquiry", "\\005"),
111 );
112 table.insert(
113 Byte::from(6),
114 AsciiChar::new(Byte::from(6), "CACK", "Acknowledge", "\\006"),
115 );
116 table.insert(
117 Byte::from(7),
118 AsciiChar::new(Byte::from(7), "CBEL", "Bell", "\\007"),
119 );
120 table.insert(
121 Byte::from(8),
122 AsciiChar::new(Byte::from(8), "CBS", "Backspace", "\\008"),
123 );
124 table.insert(
125 Byte::from(9),
126 AsciiChar::new(Byte::from(9), "CTAB", "Horizontal tab", "\\009"),
127 );
128 table.insert(
129 Byte::from(10),
130 AsciiChar::new(Byte::from(10), "CLF", "Line feed", "\\010"),
131 );
132 table.insert(
133 Byte::from(11),
134 AsciiChar::new(Byte::from(11), "CVT", "Vertical tab", "\\011"),
135 );
136 table.insert(
137 Byte::from(12),
138 AsciiChar::new(Byte::from(12), "CFF", "Form feed", "\\012"),
139 );
140 table.insert(
141 Byte::from(13),
142 AsciiChar::new(Byte::from(13), "CCR", "Carriage return", "\\013"),
143 );
144 table.insert(
145 Byte::from(14),
146 AsciiChar::new(Byte::from(14), "CSO", "Shift out", "\\014"),
147 );
148 table.insert(
149 Byte::from(15),
150 AsciiChar::new(Byte::from(15), "CSI", "Shift in", "\\015"),
151 );
152 table.insert(
153 Byte::from(16),
154 AsciiChar::new(Byte::from(16), "CDLE", "Data link escape", "\\016"),
155 );
156 table.insert(
157 Byte::from(17),
158 AsciiChar::new(Byte::from(17), "CDC1", "Device control 1", "\\017"),
159 );
160 table.insert(
161 Byte::from(18),
162 AsciiChar::new(Byte::from(18), "CDC2", "Device control 2", "\\018"),
163 );
164 table.insert(
165 Byte::from(19),
166 AsciiChar::new(Byte::from(19), "CDC3", "Device control 3", "\\019"),
167 );
168 table.insert(
169 Byte::from(20),
170 AsciiChar::new(Byte::from(20), "CDC4", "Device control 4", "\\020"),
171 );
172 table.insert(
173 Byte::from(21),
174 AsciiChar::new(Byte::from(21), "CNAK", "Negative acknowledge", "\\021"),
175 );
176 table.insert(
177 Byte::from(22),
178 AsciiChar::new(Byte::from(22), "CSYN", "Synchronous idle", "\\022"),
179 );
180 table.insert(
181 Byte::from(23),
182 AsciiChar::new(Byte::from(23), "CETB", "End of transmission block", "\\023"),
183 );
184 table.insert(
185 Byte::from(24),
186 AsciiChar::new(Byte::from(24), "CCAN", "Cancel", "\\024"),
187 );
188 table.insert(
189 Byte::from(25),
190 AsciiChar::new(Byte::from(25), "CEM", "End of medium", "\\025"),
191 );
192 table.insert(
193 Byte::from(26),
194 AsciiChar::new(Byte::from(26), "CSUB", "Substitute", "\\026"),
195 );
196 table.insert(
197 Byte::from(27),
198 AsciiChar::new(Byte::from(27), "CESC", "Escape", "\\027"),
199 );
200 table.insert(
201 Byte::from(28),
202 AsciiChar::new(Byte::from(28), "CFS", "File separator", "\\028"),
203 );
204 table.insert(
205 Byte::from(29),
206 AsciiChar::new(Byte::from(29), "CGS", "Group separator", "\\029"),
207 );
208 table.insert(
209 Byte::from(30),
210 AsciiChar::new(Byte::from(30), "CRS", "Record separator", "\\030"),
211 );
212 table.insert(
213 Byte::from(31),
214 AsciiChar::new(Byte::from(31), "CUS", "Unit separator", "\\031"),
215 );
216 table.insert(
217 Byte::from(32),
218 AsciiChar::new(Byte::from(32), "WSP", "Space", " "),
219 );
220 table.insert(
221 Byte::from(33),
222 AsciiChar::new(Byte::from(33), "SBANG", "Exclamation mark", "!"),
223 );
224 table.insert(
225 Byte::from(34),
226 AsciiChar::new(Byte::from(34), "SDBLQ", "Double quote", "\""),
227 );
228 table.insert(
229 Byte::from(35),
230 AsciiChar::new(Byte::from(35), "SHASH", "Hash", "#"),
231 );
232 table.insert(
233 Byte::from(36),
234 AsciiChar::new(Byte::from(36), "SDOLL", "Dollar sign", "$"),
235 );
236 table.insert(
237 Byte::from(37),
238 AsciiChar::new(Byte::from(37), "SPERC", "Percent", "%"),
239 );
240 table.insert(
241 Byte::from(38),
242 AsciiChar::new(Byte::from(38), "SAMP", "Ampersand", "&"),
243 );
244 table.insert(
245 Byte::from(39),
246 AsciiChar::new(Byte::from(39), "SSQT", "Single quote", "'"),
247 );
248 table.insert(
249 Byte::from(40),
250 AsciiChar::new(Byte::from(40), "SOPAR", "Open parenthesis", "("),
251 );
252 table.insert(
253 Byte::from(41),
254 AsciiChar::new(Byte::from(41), "SCPAR", "Close parenthesis", ")"),
255 );
256 table.insert(
257 Byte::from(42),
258 AsciiChar::new(Byte::from(42), "SSTAR", "Asterisk", "*"),
259 );
260 table.insert(
261 Byte::from(43),
262 AsciiChar::new(Byte::from(43), "SPLUS", "Plus", "+"),
263 );
264 table.insert(
265 Byte::from(44),
266 AsciiChar::new(Byte::from(44), "SCOM", "Comma", ","),
267 );
268 table.insert(
269 Byte::from(45),
270 AsciiChar::new(Byte::from(45), "SDASH", "Dash", "-"),
271 );
272 table.insert(
273 Byte::from(46),
274 AsciiChar::new(Byte::from(46), "SDOT", "Period", "."),
275 );
276 table.insert(
277 Byte::from(47),
278 AsciiChar::new(Byte::from(47), "SSLASH", "Slash", "/"),
279 );
280 table.insert(
281 Byte::from(48),
282 AsciiChar::new(Byte::from(48), "DIG0", "Zero", "0"),
283 );
284 table.insert(
285 Byte::from(49),
286 AsciiChar::new(Byte::from(49), "DIG1", "One", "1"),
287 );
288 table.insert(
289 Byte::from(50),
290 AsciiChar::new(Byte::from(50), "DIG2", "Two", "2"),
291 );
292 table.insert(
293 Byte::from(51),
294 AsciiChar::new(Byte::from(51), "DIG3", "Three", "3"),
295 );
296 table.insert(
297 Byte::from(52),
298 AsciiChar::new(Byte::from(52), "DIG4", "Four", "4"),
299 );
300 table.insert(
301 Byte::from(53),
302 AsciiChar::new(Byte::from(53), "DIG5", "Five", "5"),
303 );
304 table.insert(
305 Byte::from(54),
306 AsciiChar::new(Byte::from(54), "DIG6", "Six", "6"),
307 );
308 table.insert(
309 Byte::from(55),
310 AsciiChar::new(Byte::from(55), "DIG7", "Seven", "7"),
311 );
312 table.insert(
313 Byte::from(56),
314 AsciiChar::new(Byte::from(56), "DIG8", "Eight", "8"),
315 );
316 table.insert(
317 Byte::from(57),
318 AsciiChar::new(Byte::from(57), "DIG9", "Nine", "9"),
319 );
320 table.insert(
321 Byte::from(58),
322 AsciiChar::new(Byte::from(58), "SCOL", "Colon", ":"),
323 );
324 table.insert(
325 Byte::from(59),
326 AsciiChar::new(Byte::from(59), "SSCL", "Semicolon", ";"),
327 );
328 table.insert(
329 Byte::from(60),
330 AsciiChar::new(Byte::from(60), "SLT", "Less than", "<"),
331 );
332 table.insert(
333 Byte::from(61),
334 AsciiChar::new(Byte::from(61), "SEQ", "Equals", "="),
335 );
336 table.insert(
337 Byte::from(62),
338 AsciiChar::new(Byte::from(62), "SGT", "Greater than", ">"),
339 );
340 table.insert(
341 Byte::from(63),
342 AsciiChar::new(Byte::from(63), "SQUES", "Question mark", "?"),
343 );
344 table.insert(
345 Byte::from(64),
346 AsciiChar::new(Byte::from(64), "SAT", "At sign", "@"),
347 );
348 table.insert(
349 Byte::from(65),
350 AsciiChar::new(Byte::from(65), "UCLA", "Uppercase Letter A", "A"),
351 );
352 table.insert(
353 Byte::from(66),
354 AsciiChar::new(Byte::from(66), "UCLB", "Uppercase Letter B", "B"),
355 );
356 table.insert(
357 Byte::from(67),
358 AsciiChar::new(Byte::from(67), "UCLC", "Uppercase Letter C", "C"),
359 );
360 table.insert(
361 Byte::from(68),
362 AsciiChar::new(Byte::from(68), "UCLD", "Uppercase Letter D", "D"),
363 );
364 table.insert(
365 Byte::from(69),
366 AsciiChar::new(Byte::from(69), "UCLE", "Uppercase Letter E", "E"),
367 );
368 table.insert(
369 Byte::from(70),
370 AsciiChar::new(Byte::from(70), "UCLF", "Uppercase Letter F", "F"),
371 );
372 table.insert(
373 Byte::from(71),
374 AsciiChar::new(Byte::from(71), "UCLG", "Uppercase Letter G", "G"),
375 );
376 table.insert(
377 Byte::from(72),
378 AsciiChar::new(Byte::from(72), "UCLH", "Uppercase Letter H", "H"),
379 );
380 table.insert(
381 Byte::from(73),
382 AsciiChar::new(Byte::from(73), "UCLI", "Uppercase Letter I", "I"),
383 );
384 table.insert(
385 Byte::from(74),
386 AsciiChar::new(Byte::from(74), "UCLJ", "Uppercase Letter J", "J"),
387 );
388 table.insert(
389 Byte::from(75),
390 AsciiChar::new(Byte::from(75), "UCLK", "Uppercase Letter K", "K"),
391 );
392 table.insert(
393 Byte::from(76),
394 AsciiChar::new(Byte::from(76), "UCLL", "Uppercase Letter L", "L"),
395 );
396 table.insert(
397 Byte::from(77),
398 AsciiChar::new(Byte::from(77), "UCLM", "Uppercase Letter M", "M"),
399 );
400 table.insert(
401 Byte::from(78),
402 AsciiChar::new(Byte::from(78), "UCLN", "Uppercase Letter N", "N"),
403 );
404 table.insert(
405 Byte::from(79),
406 AsciiChar::new(Byte::from(79), "UCLO", "Uppercase Letter O", "O"),
407 );
408 table.insert(
409 Byte::from(80),
410 AsciiChar::new(Byte::from(80), "UCLP", "Uppercase Letter P", "P"),
411 );
412 table.insert(
413 Byte::from(81),
414 AsciiChar::new(Byte::from(81), "UCLQ", "Uppercase Letter Q", "Q"),
415 );
416 table.insert(
417 Byte::from(82),
418 AsciiChar::new(Byte::from(82), "UCLR", "Uppercase Letter R", "R"),
419 );
420 table.insert(
421 Byte::from(83),
422 AsciiChar::new(Byte::from(83), "UCLS", "Uppercase Letter S", "S"),
423 );
424 table.insert(
425 Byte::from(84),
426 AsciiChar::new(Byte::from(84), "UCLT", "Uppercase Letter T", "T"),
427 );
428 table.insert(
429 Byte::from(85),
430 AsciiChar::new(Byte::from(85), "UCLU", "Uppercase Letter U", "U"),
431 );
432 table.insert(
433 Byte::from(86),
434 AsciiChar::new(Byte::from(86), "UCLV", "Uppercase Letter V", "V"),
435 );
436 table.insert(
437 Byte::from(87),
438 AsciiChar::new(Byte::from(87), "UCLW", "Uppercase Letter W", "W"),
439 );
440 table.insert(
441 Byte::from(88),
442 AsciiChar::new(Byte::from(88), "UCLX", "Uppercase Letter X", "X"),
443 );
444 table.insert(
445 Byte::from(89),
446 AsciiChar::new(Byte::from(89), "UCLY", "Uppercase Letter Y", "Y"),
447 );
448 table.insert(
449 Byte::from(90),
450 AsciiChar::new(Byte::from(90), "UCLZ", "Uppercase Letter Z", "Z"),
451 );
452 table.insert(
453 Byte::from(91),
454 AsciiChar::new(Byte::from(91), "SOSB", "Open square bracket", "["),
455 );
456 table.insert(
457 Byte::from(92),
458 AsciiChar::new(Byte::from(92), "SBKS", "Backslash", "\\"),
459 );
460 table.insert(
461 Byte::from(93),
462 AsciiChar::new(Byte::from(93), "SCSB", "Close square bracket", "]"),
463 );
464 table.insert(
465 Byte::from(94),
466 AsciiChar::new(Byte::from(94), "SCAR", "Caret", "^"),
467 );
468 table.insert(
469 Byte::from(95),
470 AsciiChar::new(Byte::from(95), "SUSC", "Underscore", "_"),
471 );
472 table.insert(
473 Byte::from(96),
474 AsciiChar::new(Byte::from(96), "SBTK", "Backtick", "`"),
475 );
476 table.insert(
477 Byte::from(97),
478 AsciiChar::new(Byte::from(97), "LCLA", "Lowercase Letter a", "a"),
479 );
480 table.insert(
481 Byte::from(98),
482 AsciiChar::new(Byte::from(98), "LCLB", "Lowercase Letter b", "b"),
483 );
484 table.insert(
485 Byte::from(99),
486 AsciiChar::new(Byte::from(99), "LCLC", "Lowercase Letter c", "c"),
487 );
488 table.insert(
489 Byte::from(100),
490 AsciiChar::new(Byte::from(100), "LCLD", "Lowercase Letter d", "d"),
491 );
492 table.insert(
493 Byte::from(101),
494 AsciiChar::new(Byte::from(101), "LCLE", "Lowercase Letter e", "e"),
495 );
496 table.insert(
497 Byte::from(102),
498 AsciiChar::new(Byte::from(102), "LCLF", "Lowercase Letter f", "f"),
499 );
500 table.insert(
501 Byte::from(103),
502 AsciiChar::new(Byte::from(103), "LCLG", "Lowercase Letter g", "g"),
503 );
504 table.insert(
505 Byte::from(104),
506 AsciiChar::new(Byte::from(104), "LCLH", "Lowercase Letter h", "h"),
507 );
508 table.insert(
509 Byte::from(105),
510 AsciiChar::new(Byte::from(105), "LCLI", "Lowercase Letter i", "i"),
511 );
512 table.insert(
513 Byte::from(106),
514 AsciiChar::new(Byte::from(106), "LCLJ", "Lowercase Letter j", "j"),
515 );
516 table.insert(
517 Byte::from(107),
518 AsciiChar::new(Byte::from(107), "LCLK", "Lowercase Letter k", "k"),
519 );
520 table.insert(
521 Byte::from(108),
522 AsciiChar::new(Byte::from(108), "LCLL", "Lowercase Letter l", "l"),
523 );
524 table.insert(
525 Byte::from(109),
526 AsciiChar::new(Byte::from(109), "LCLM", "Lowercase Letter m", "m"),
527 );
528 table.insert(
529 Byte::from(110),
530 AsciiChar::new(Byte::from(110), "LCLN", "Lowercase Letter n", "n"),
531 );
532 table.insert(
533 Byte::from(111),
534 AsciiChar::new(Byte::from(111), "LCLO", "Lowercase Letter o", "o"),
535 );
536 table.insert(
537 Byte::from(112),
538 AsciiChar::new(Byte::from(112), "LCLP", "Lowercase Letter p", "p"),
539 );
540 table.insert(
541 Byte::from(113),
542 AsciiChar::new(Byte::from(113), "LCLQ", "Lowercase Letter q", "q"),
543 );
544 table.insert(
545 Byte::from(114),
546 AsciiChar::new(Byte::from(114), "LCLR", "Lowercase Letter r", "r"),
547 );
548 table.insert(
549 Byte::from(115),
550 AsciiChar::new(Byte::from(115), "LCLS", "Lowercase Letter s", "s"),
551 );
552 table.insert(
553 Byte::from(116),
554 AsciiChar::new(Byte::from(116), "LCLT", "Lowercase Letter t", "t"),
555 );
556 table.insert(
557 Byte::from(117),
558 AsciiChar::new(Byte::from(117), "LCLU", "Lowercase Letter u", "u"),
559 );
560 table.insert(
561 Byte::from(118),
562 AsciiChar::new(Byte::from(118), "LCLV", "Lowercase Letter v", "v"),
563 );
564 table.insert(
565 Byte::from(119),
566 AsciiChar::new(Byte::from(119), "LCLW", "Lowercase Letter w", "w"),
567 );
568 table.insert(
569 Byte::from(120),
570 AsciiChar::new(Byte::from(120), "LCLX", "Lowercase Letter x", "x"),
571 );
572 table.insert(
573 Byte::from(121),
574 AsciiChar::new(Byte::from(121), "LCLY", "Lowercase Letter y", "y"),
575 );
576 table.insert(
577 Byte::from(122),
578 AsciiChar::new(Byte::from(122), "LCLZ", "Lowercase Letter z", "z"),
579 );
580 table.insert(
581 Byte::from(123),
582 AsciiChar::new(Byte::from(123), "SOCB", "Open curly brace", "{"),
583 );
584 table.insert(
585 Byte::from(124),
586 AsciiChar::new(Byte::from(124), "SVBAR", "Vertical bar", "|"),
587 );
588 table.insert(
589 Byte::from(125),
590 AsciiChar::new(Byte::from(125), "SCCB", "Close curly brace", "}"),
591 );
592 table.insert(
593 Byte::from(126),
594 AsciiChar::new(Byte::from(126), "STLD", "Tilde", "~"),
595 );
596 table.insert(
597 Byte::from(127),
598 AsciiChar::new(Byte::from(127), "CDEL", "Delete", "\\127"),
599 );
600 }
601
602 Self { table }
603 }
604
605 #[must_use]
643 pub fn get(&self, byte: Byte) -> Option<&AsciiChar> {
644 self.table.get(&byte)
645 }
646}
647
648impl Default for AsciiTable {
649 fn default() -> Self {
650 Self::new()
651 }
652}
653
654#[cfg(test)]
655mod test {
656 use super::*;
657
658 #[test]
659 fn test_ascii_table_new() {
660 let ascii_table = AsciiTable::new();
661
662 assert_eq!(
663 ascii_table.get(Byte::from(0)).unwrap().character_code(),
664 "CNUL"
665 );
666 assert_eq!(
667 ascii_table.get(Byte::from(1)).unwrap().character_code(),
668 "CSOH"
669 );
670 assert_eq!(
671 ascii_table.get(Byte::from(2)).unwrap().character_code(),
672 "CSTX"
673 );
674 }
675
676 #[test]
677 fn test_ascii_table_get() {
678 let ascii_table = AsciiTable::new();
679
680 assert_eq!(
683 ascii_table.get(Byte::from(97)).unwrap().character_code(),
684 "LCLA",
685 "Character code for Byte value 97 should be 'LCLA'"
686 );
687 assert_eq!(
688 ascii_table.get(Byte::from(98)).unwrap().character_code(),
689 "LCLB",
690 "Character code for Byte value 98 should be 'LCLB'"
691 );
692 assert_eq!(
693 ascii_table.get(Byte::from(99)).unwrap().character_code(),
694 "LCLC",
695 "Character code for Byte value 99 should be 'LCLC'"
696 );
697
698 assert_eq!(
701 ascii_table.get(Byte::from(128)),
702 None,
703 "There should be no AsciiChar for Byte value 128"
704 );
705 }
706
707 #[test]
708 fn test_ascii_table_default() {
709 let ascii_table = AsciiTable::default();
710
711 assert_eq!(
714 ascii_table.get(Byte::from(97)).unwrap().character_code(),
715 "LCLA",
716 "Character code for Byte value 97 should be 'LCLA'"
717 );
718 assert_eq!(
719 ascii_table.get(Byte::from(98)).unwrap().character_code(),
720 "LCLB",
721 "Character code for Byte value 98 should be 'LCLB'"
722 );
723 assert_eq!(
724 ascii_table.get(Byte::from(99)).unwrap().character_code(),
725 "LCLC",
726 "Character code for Byte value 99 should be 'LCLC'"
727 );
728
729 assert_eq!(
732 ascii_table.get(Byte::from(128)),
733 None,
734 "There should be no AsciiChar for Byte value 128"
735 );
736 }
737}