odrive_messages/
sdo.rs

1#[derive(Debug, Clone)]
2pub enum ConfigValue {
3    Float(f32),
4    UInt8(u8),
5    UInt32(u32),
6    UInt64(u64),
7    Int32(i32),
8    Bool(bool),
9    List(Vec<ConfigValue>),
10    Empty,
11}
12
13impl ConfigValue {
14    pub fn to_le_byte_vec(&self) -> Vec<u8> {
15        match self {
16            ConfigValue::Float(f) => f.to_le_bytes().to_vec(),
17            ConfigValue::UInt8(v) => vec![*v],
18            ConfigValue::UInt32(v) => v.to_le_bytes().to_vec(),
19            ConfigValue::UInt64(v) => v.to_le_bytes().to_vec(),
20            ConfigValue::Int32(v) => v.to_le_bytes().to_vec(),
21            ConfigValue::Bool(b) => {
22                if *b {
23                    vec![1]
24                } else {
25                    vec![0]
26                }
27            }
28            ConfigValue::List(v) => v.iter().flat_map(|item| item.to_le_byte_vec()).collect(),
29            ConfigValue::Empty => vec![],
30        }
31    }
32
33    // Parse from u32 based on type string
34    pub fn from_le_bytes(value: &[u8], type_str: &str) -> Result<Self, String> {
35        match type_str {
36            "float" => Ok(ConfigValue::Float(f32::from_le_bytes(
37                value.try_into().map_err(|e| format!("{e}"))?,
38            ))),
39            "uint8" => Ok(ConfigValue::UInt8(value[0])),
40            "uint32" => Ok(ConfigValue::UInt32(u32::from_le_bytes(
41                value.try_into().map_err(|e| format!("{e}"))?,
42            ))),
43            "uint64" => Ok(ConfigValue::UInt64(u64::from_le_bytes(
44                value.try_into().map_err(|e| format!("{e}"))?,
45            ))),
46            "int32" => Ok(ConfigValue::Int32(i32::from_le_bytes(
47                value.try_into().map_err(|e| format!("{e}"))?,
48            ))),
49            "bool" => Ok(ConfigValue::Bool(value[0] != 0)),
50            // TODO: implement list parsing
51            _ => Err(format!("Unknown type: {}", type_str)),
52        }
53    }
54}