tdms_rs/model/
datatypes.rs

1use crate::error::{Result, TdmsError};
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum DataType {
6    I8 = 1,
7    I16 = 2,
8    I32 = 3,
9    I64 = 4,
10    U8 = 5,
11    U16 = 6,
12    U32 = 7,
13    U64 = 8,
14    Float = 9,
15    Double = 10,
16    String = 32,
17    Boolean = 33,
18    TimeStamp = 68,
19}
20
21impl DataType {
22    pub fn from_u32(val: u32) -> Result<Self> {
23        match val {
24            1 => Ok(DataType::I8),
25            2 => Ok(DataType::I16),
26            3 => Ok(DataType::I32),
27            4 => Ok(DataType::I64),
28            5 => Ok(DataType::U8),
29            6 => Ok(DataType::U16),
30            7 => Ok(DataType::U32),
31            8 => Ok(DataType::U64),
32            9 => Ok(DataType::Float),
33            10 => Ok(DataType::Double),
34            32 => Ok(DataType::String),
35            33 => Ok(DataType::Boolean),
36            68 => Ok(DataType::TimeStamp),
37            0 => Err(TdmsError::NotImplemented(
38                "Void DataType not supported in public API".to_string(),
39            )),
40            _ => Err(TdmsError::NotImplemented(format!("DataType {}", val))),
41        }
42    }
43
44    pub fn to_u32(&self) -> u32 {
45        match self {
46            DataType::I8 => 1,
47            DataType::I16 => 2,
48            DataType::I32 => 3,
49            DataType::I64 => 4,
50            DataType::U8 => 5,
51            DataType::U16 => 6,
52            DataType::U32 => 7,
53            DataType::U64 => 8,
54            DataType::Float => 9,
55            DataType::Double => 10,
56            DataType::String => 32,
57            DataType::Boolean => 33,
58            DataType::TimeStamp => 68,
59        }
60    }
61
62    pub fn itemsize(&self) -> usize {
63        match self {
64            DataType::I8 | DataType::U8 | DataType::Boolean => 1,
65            DataType::I16 | DataType::U16 => 2,
66            DataType::I32 | DataType::U32 | DataType::Float => 4,
67            DataType::I64 | DataType::U64 | DataType::Double => 8,
68            DataType::TimeStamp => 16,
69            DataType::String => 0,
70        }
71    }
72}
73
74#[derive(Debug, Clone, PartialEq)]
75pub enum PropertyValue {
76    I8(i8),
77    I16(i16),
78    I32(i32),
79    I64(i64),
80    U8(u8),
81    U16(u16),
82    U32(u32),
83    U64(u64),
84    Float(f32),
85    Double(f64),
86    String(String),
87    Boolean(bool),
88    TimeStamp((i64, u64)),
89}
90
91impl Display for PropertyValue {
92    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
93        match self {
94            PropertyValue::String(s) => write!(f, "\"{}\"", s),
95            PropertyValue::Double(d) => write!(f, "{:.6}", d),
96            PropertyValue::Float(fl) => write!(f, "{:.6}", fl),
97            PropertyValue::I8(i) => write!(f, "{}", i),
98            PropertyValue::I16(i) => write!(f, "{}", i),
99            PropertyValue::I32(i) => write!(f, "{}", i),
100            PropertyValue::I64(i) => write!(f, "{}", i),
101            PropertyValue::U8(u) => write!(f, "{}", u),
102            PropertyValue::U16(u) => write!(f, "{}", u),
103            PropertyValue::U32(u) => write!(f, "{}", u),
104            PropertyValue::U64(u) => write!(f, "{}", u),
105            PropertyValue::Boolean(b) => write!(f, "{}", b),
106            PropertyValue::TimeStamp((s, frac)) => write!(f, "{}.{:019}", s, frac),
107        }
108    }
109}
110
111impl From<(i64, u64)> for PropertyValue {
112    fn from((seconds, fraction): (i64, u64)) -> Self {
113        PropertyValue::TimeStamp((seconds, fraction))
114    }
115}
116
117// Additional From implementations for common literal types
118impl From<&String> for PropertyValue {
119    fn from(s: &String) -> Self {
120        PropertyValue::String(s.clone())
121    }
122}
123
124impl From<&str> for PropertyValue {
125    fn from(s: &str) -> Self {
126        PropertyValue::String(s.to_string())
127    }
128}
129
130impl From<String> for PropertyValue {
131    fn from(s: String) -> Self {
132        PropertyValue::String(s)
133    }
134}
135
136impl From<f64> for PropertyValue {
137    fn from(f: f64) -> Self {
138        PropertyValue::Double(f)
139    }
140}
141
142impl From<f32> for PropertyValue {
143    fn from(f: f32) -> Self {
144        PropertyValue::Float(f)
145    }
146}
147
148impl From<i32> for PropertyValue {
149    fn from(i: i32) -> Self {
150        PropertyValue::I32(i)
151    }
152}
153
154impl From<bool> for PropertyValue {
155    fn from(b: bool) -> Self {
156        PropertyValue::Boolean(b)
157    }
158}