array_object/
bitfield.rs

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
use crate::DataType;

pub const TYPE_MASK: u8 = 0b_111_0_0000;

pub const SHORT_UNSIGNED_INTEGER: u8 = 0b_000_0_0000;
pub const SHORT_SIGNED_INTEGER: u8 = 0b_001_0_0000;
pub const UNSIGNED_INTEGER: u8 = 0b_010_0_0000;
pub const SIGNED_INTEGER: u8 = 0b_011_0_0000;
pub const REAL: u8 = 0b_100_0_0000;
pub const COMPLEX: u8 = 0b_101_0_0000;
pub const STRING: u8 = 0b_110_0_0000;

pub const SHORTDATA_MASK: u8 = 0b_000_1_1111;

pub const DIMENSION_MASK: u8 = 0b_000_0_1111;

pub const FORMAT_MASK: u8 = 0b_000_1_0000;

pub const FIXED_LENGTH: u8 = 0b_000_0_0000;
pub const VARIABLE_LENGTH: u8 = 0b_000_1_0000;

pub const JOINED: u8 = 0b_000_0_0000;
pub const DICTIONARY: u8 = 0b_000_1_0000;

impl DataType {
    /// Reads the last byte of binary data and describes the data type.
    pub fn describe_footer(binary: &Vec<u8>) -> String {
        let footer = binary.last().unwrap();
        match footer & TYPE_MASK {
            SHORT_UNSIGNED_INTEGER => "Short unsigned integer".to_string(),
            UNSIGNED_INTEGER => {
                if footer & FORMAT_MASK == VARIABLE_LENGTH {
                    format!(
                        "{}-dimensional variable length unsigned integer",
                        footer & DIMENSION_MASK
                    )
                } else {
                    format!(
                        "{}-dimensional fixed length unsigned integer",
                        footer & DIMENSION_MASK
                    )
                }
            }
            SHORT_SIGNED_INTEGER => "Short signed integer".to_string(),
            SIGNED_INTEGER => {
                if footer & FORMAT_MASK == VARIABLE_LENGTH {
                    format!(
                        "{}-dimensional variable length signed integer",
                        footer & DIMENSION_MASK
                    )
                } else {
                    format!(
                        "{}-dimensional fixed length signed integer",
                        footer & DIMENSION_MASK
                    )
                }
            }
            REAL => {
                if footer & FORMAT_MASK == VARIABLE_LENGTH {
                    format!(
                        "{}-dimensional variable length real number",
                        footer & DIMENSION_MASK
                    )
                } else {
                    format!(
                        "{}-dimensional fixed length real number",
                        footer & DIMENSION_MASK
                    )
                }
            }
            COMPLEX => {
                if footer & FORMAT_MASK == VARIABLE_LENGTH {
                    format!(
                        "{}-dimensional variable length complex number",
                        footer & DIMENSION_MASK
                    )
                } else {
                    format!(
                        "{}-dimensional fixed length complex number",
                        footer & DIMENSION_MASK
                    )
                }
            }
            STRING => {
                if footer & FORMAT_MASK == DICTIONARY {
                    format!(
                        "{}-dimensional compressed string with dictionary",
                        footer & DIMENSION_MASK
                    )
                } else {
                    format!("{}-dimensional joined string", footer & DIMENSION_MASK)
                }
            }
            _ => {
                panic!();
            }
        }
    }
}