svd_rs/
datatype.rs

1/// Register data type
2#[cfg_attr(
3    feature = "serde",
4    derive(serde::Deserialize, serde::Serialize),
5    serde(rename_all = "snake_case")
6)]
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum DataType {
9    /// unsigned byte
10    U8,
11    /// unsigned half word
12    U16,
13    /// unsigned word
14    U32,
15    /// unsigned double word
16    U64,
17    /// signed byte
18    I8,
19    /// signed half word
20    I16,
21    /// signed world
22    I32,
23    /// signed double word
24    I64,
25    /// pointer to unsigned byte
26    U8Ptr,
27    /// pointer to unsigned half word
28    U16Ptr,
29    /// pointer to unsigned word
30    U32Ptr,
31    /// pointer to unsigned double word
32    U64Ptr,
33    /// pointer to signed byte
34    I8Ptr,
35    /// pointer to signed half word
36    I16Ptr,
37    /// pointer to signed world
38    I32Ptr,
39    /// pointer to signed double word
40    I64Ptr,
41}
42
43impl DataType {
44    /// Parse a string into an [`DataType`] value, returning [`Option::None`] if the string is not valid.
45    pub fn parse_str(s: &str) -> Option<Self> {
46        match s {
47            "uint8_t" => Some(Self::U8),
48            "uint16_t" => Some(Self::U16),
49            "uint32_t" => Some(Self::U32),
50            "uint64_t" => Some(Self::U64),
51            "int8_t" => Some(Self::I8),
52            "int16_t" => Some(Self::I16),
53            "int32_t" => Some(Self::I32),
54            "int64_t" => Some(Self::I64),
55            "uint8_t *" => Some(Self::U8Ptr),
56            "uint16_t *" => Some(Self::U16Ptr),
57            "uint32_t *" => Some(Self::U32Ptr),
58            "uint64_t *" => Some(Self::U64Ptr),
59            "int8_t *" => Some(Self::I8Ptr),
60            "int16_t *" => Some(Self::I16Ptr),
61            "int32_t *" => Some(Self::I32Ptr),
62            "int64_t *" => Some(Self::I64Ptr),
63            _ => None,
64        }
65    }
66
67    /// Convert this [`DataType`] into a static string.
68    pub const fn as_str(self) -> &'static str {
69        match self {
70            Self::U8 => "uint8_t",
71            Self::U16 => "uint16_t",
72            Self::U32 => "uint32_t",
73            Self::U64 => "uint64_t",
74            Self::I8 => "int8_t",
75            Self::I16 => "int16_t",
76            Self::I32 => "int32_t",
77            Self::I64 => "int64_t",
78            Self::U8Ptr => "uint8_t *",
79            Self::U16Ptr => "uint16_t *",
80            Self::U32Ptr => "uint32_t *",
81            Self::U64Ptr => "uint64_t *",
82            Self::I8Ptr => "int8_t *",
83            Self::I16Ptr => "int16_t *",
84            Self::I32Ptr => "int32_t *",
85            Self::I64Ptr => "int64_t *",
86        }
87    }
88}