xio_base_datatypes/
data_type.rs1use std::fmt;
2use try_from::TryFrom;
3
4#[repr(u8)]
5#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7#[cfg_attr(
8 feature = "serde",
9 serde(rename_all = "lowercase", deny_unknown_fields)
10)]
11pub enum DataType {
12 Boolean = 0x00,
13 Int8 = 0x01,
14 Int16 = 0x02,
15 Int32 = 0x03,
16 Int64 = 0x04,
17 UInt8 = 0x05,
18 UInt16 = 0x06,
19 UInt32 = 0x07,
20 UInt64 = 0x08,
21 ParameterMask = 0x09,
22 Invalid = 0xFF,
23}
24
25pub trait HasDataType {
26 fn data_type(&self) -> DataType;
27}
28
29impl TryFrom<u8> for DataType {
30 type Err = std::io::Error;
31 fn try_from(value: u8) -> std::io::Result<Self> {
32 use DataType::*;
33 match value {
34 0x00 => Ok(Boolean),
35 0x01 => Ok(Int8),
36 0x02 => Ok(Int16),
37 0x03 => Ok(Int32),
38 0x04 => Ok(Int64),
39 0x05 => Ok(UInt8),
40 0x06 => Ok(UInt16),
41 0x07 => Ok(UInt32),
42 0x08 => Ok(UInt64),
43 0x09 => Ok(ParameterMask),
44 _ => Err(std::io::Error::new(
45 std::io::ErrorKind::InvalidData,
46 format!("Invalid DataType enum value {}", value),
47 )),
48 }
49 }
50}
51
52impl fmt::Display for DataType {
53 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54 write!(f, "{:?}", self)
55 }
56}