Skip to main content

mint_cli/layout/
value.rs

1use super::conversions::convert_value_to_bytes;
2use super::entry::ScalarType;
3use super::error::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, Clone, Deserialize)]
15#[serde(untagged)]
16pub enum DataValue {
17    Bool(bool),
18    U64(u64),
19    I64(i64),
20    F64(f64),
21    Str(String),
22}
23
24impl DataValue {
25    pub fn to_bytes(
26        &self,
27        scalar_type: ScalarType,
28        endianness: &Endianness,
29        strict: bool,
30    ) -> Result<Vec<u8>, LayoutError> {
31        convert_value_to_bytes(self, scalar_type, endianness, strict)
32    }
33
34    pub fn string_to_bytes(&self) -> Result<Vec<u8>, LayoutError> {
35        match self {
36            DataValue::Str(val) => Ok(val.as_bytes().to_vec()),
37            _ => Err(LayoutError::DataValueExportFailed(
38                "String expected for string type.".to_string(),
39            )),
40        }
41    }
42}