array_object/
bitfield.rs

1use crate::DataType;
2
3pub const TYPE_MASK: u8 = 0b_111_0_0000;
4
5pub const SHORT_UNSIGNED_INTEGER: u8 = 0b_000_0_0000;
6pub const SHORT_SIGNED_INTEGER: u8 = 0b_001_0_0000;
7pub const UNSIGNED_INTEGER: u8 = 0b_010_0_0000;
8pub const SIGNED_INTEGER: u8 = 0b_011_0_0000;
9pub const REAL: u8 = 0b_100_0_0000;
10pub const COMPLEX: u8 = 0b_101_0_0000;
11pub const STRING: u8 = 0b_110_0_0000;
12
13pub const SHORTDATA_MASK: u8 = 0b_000_1_1111;
14
15pub const DIMENSION_MASK: u8 = 0b_000_0_1111;
16
17pub const FORMAT_MASK: u8 = 0b_000_1_0000;
18
19pub const FIXED_LENGTH: u8 = 0b_000_0_0000;
20pub const VARIABLE_LENGTH: u8 = 0b_000_1_0000;
21
22pub const JOINED: u8 = 0b_000_0_0000;
23pub const DICTIONARY: u8 = 0b_000_1_0000;
24
25impl DataType {
26    /// Reads the last byte of binary data and describes the data type.
27    pub fn describe_footer(binary: &Vec<u8>) -> String {
28        let footer = binary.last().unwrap();
29        match footer & TYPE_MASK {
30            SHORT_UNSIGNED_INTEGER => "Short unsigned integer".to_string(),
31            UNSIGNED_INTEGER => {
32                if footer & FORMAT_MASK == VARIABLE_LENGTH {
33                    format!(
34                        "{}-dimensional variable length unsigned integer",
35                        footer & DIMENSION_MASK
36                    )
37                } else {
38                    format!(
39                        "{}-dimensional fixed length unsigned integer",
40                        footer & DIMENSION_MASK
41                    )
42                }
43            }
44            SHORT_SIGNED_INTEGER => "Short signed integer".to_string(),
45            SIGNED_INTEGER => {
46                if footer & FORMAT_MASK == VARIABLE_LENGTH {
47                    format!(
48                        "{}-dimensional variable length signed integer",
49                        footer & DIMENSION_MASK
50                    )
51                } else {
52                    format!(
53                        "{}-dimensional fixed length signed integer",
54                        footer & DIMENSION_MASK
55                    )
56                }
57            }
58            REAL => {
59                if footer & FORMAT_MASK == VARIABLE_LENGTH {
60                    format!(
61                        "{}-dimensional variable length real number",
62                        footer & DIMENSION_MASK
63                    )
64                } else {
65                    format!(
66                        "{}-dimensional fixed length real number",
67                        footer & DIMENSION_MASK
68                    )
69                }
70            }
71            COMPLEX => {
72                if footer & FORMAT_MASK == VARIABLE_LENGTH {
73                    format!(
74                        "{}-dimensional variable length complex number",
75                        footer & DIMENSION_MASK
76                    )
77                } else {
78                    format!(
79                        "{}-dimensional fixed length complex number",
80                        footer & DIMENSION_MASK
81                    )
82                }
83            }
84            STRING => {
85                if footer & FORMAT_MASK == DICTIONARY {
86                    format!(
87                        "{}-dimensional compressed string with dictionary",
88                        footer & DIMENSION_MASK
89                    )
90                } else {
91                    format!("{}-dimensional joined string", footer & DIMENSION_MASK)
92                }
93            }
94            _ => {
95                panic!();
96            }
97        }
98    }
99}