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
use std::fmt;
use try_from::TryFrom;

#[repr(u8)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(rename_all = "lowercase", deny_unknown_fields)
)]
pub enum DataType {
    Boolean = 0x00,
    Int8 = 0x01,
    Int16 = 0x02,
    Int32 = 0x03,
    Int64 = 0x04,
    UInt8 = 0x05,
    UInt16 = 0x06,
    UInt32 = 0x07,
    UInt64 = 0x08,
    ParameterMask = 0x09,
    Invalid = 0xFF,
}

pub trait HasDataType {
    fn data_type(&self) -> DataType;
}

impl TryFrom<u8> for DataType {
    type Err = std::io::Error;
    fn try_from(value: u8) -> std::io::Result<Self> {
        use DataType::*;
        match value {
            0x00 => Ok(Boolean),
            0x01 => Ok(Int8),
            0x02 => Ok(Int16),
            0x03 => Ok(Int32),
            0x04 => Ok(Int64),
            0x05 => Ok(UInt8),
            0x06 => Ok(UInt16),
            0x07 => Ok(UInt32),
            0x08 => Ok(UInt64),
            0x09 => Ok(ParameterMask),
            _ => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Invalid DataType enum value {}", value),
            )),
        }
    }
}

impl fmt::Display for DataType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}