mint_cli/layout/
value.rs

1use super::conversions::convert_value_to_bytes;
2use super::entry::ScalarType;
3use super::errors::LayoutError;
4use super::settings::Endianness;
5use serde::Deserialize;
6
7#[derive(Debug, Deserialize)]
8#[serde(untagged)]
9pub enum ValueSource {
10    Single(DataValue),
11    Array(Vec<DataValue>),
12}
13
14#[derive(Debug, Deserialize)]
15#[serde(untagged)]
16pub enum DataValue {
17    U64(u64),
18    I64(i64),
19    F64(f64),
20    Str(String),
21}
22
23impl DataValue {
24    pub fn to_bytes(
25        &self,
26        scalar_type: ScalarType,
27        endianness: &Endianness,
28        strict: bool,
29    ) -> Result<Vec<u8>, LayoutError> {
30        convert_value_to_bytes(self, scalar_type, endianness, strict)
31    }
32
33    pub fn string_to_bytes(&self) -> Result<Vec<u8>, LayoutError> {
34        match self {
35            DataValue::Str(val) => Ok(val.as_bytes().to_vec()),
36            _ => Err(LayoutError::DataValueExportFailed(
37                "String expected for string type.".to_string(),
38            )),
39        }
40    }
41}