rof_rs/object_format/data_value/
array.rs

1// Array
2
3use crate::object_format::{
4    ignore_str_split::{ignoring_compliant_split_str, SplitIgnoreRule, SplitIgnoreRuleType},
5    property::{data_value_from_string, property_type::PropertyType, Property},
6    DataValue,
7};
8
9#[derive(Debug)]
10pub struct DataValueArray {
11    property_type: PropertyType,
12    properties: Vec<Property>,
13}
14
15impl DataValueArray {
16    pub fn new(property_type: PropertyType, properties: Vec<Property>) -> Self {
17        Self {
18            property_type,
19            properties,
20        }
21    }
22}
23
24impl DataValue for DataValueArray {
25    fn serialize(
26        &self,
27        pretty_print: bool,
28        tab_index: usize,
29    ) -> (
30        crate::object_format::property::property_type::PropertyType, /* serialized type */
31        String,                                                      /* serialized value */
32    ) {
33        (
34            PropertyType::complex(String::from("array"), vec![self.property_type.clone()]),
35            format!(
36                "[{}]",
37                self.properties
38                    .iter()
39                    .map(|property| property.property_value.serialize(pretty_print, tab_index).1)
40                    .collect::<Vec<String>>()
41                    .join(match pretty_print {
42                        true => ", ",
43                        false => ",",
44                    })
45            ),
46        )
47    }
48
49    fn deserialize(
50        serialized_type: &PropertyType,
51        serialized_value: &str,
52    ) -> Option<Box<dyn DataValue>>
53    where
54        Self: Sized,
55    {
56        if serialized_type.get_base_type() != "array" {
57            return None;
58        }
59
60        if !(serialized_value.starts_with("[") && serialized_value.ends_with("]")) {
61            return None;
62        }
63
64        let property_type = serialized_type.get_sub_types().get(0).unwrap().clone();
65
66        let mut properties: Vec<Property> = Vec::new();
67
68        for serialized_property in ignoring_compliant_split_str(
69            &serialized_value[1..serialized_value.len() - 1].trim(),
70            ',',
71            true,
72            vec![
73                SplitIgnoreRule::new(SplitIgnoreRuleType::PAIR('"')).set_ecapsulates_raw_text(true),
74                SplitIgnoreRule::new(SplitIgnoreRuleType::PAIR('\''))
75                    .set_ecapsulates_raw_text(true),
76                SplitIgnoreRule::new(SplitIgnoreRuleType::NEST('[', ']')),
77                SplitIgnoreRule::new(SplitIgnoreRuleType::NEST('<', '>')),
78            ],
79        ) {
80            if serialized_property.trim().len() < 1 {
81                continue;
82            }
83
84            properties.push(Property::unnamed(data_value_from_string(
85                &property_type,
86                serialized_property.trim(),
87            )));
88        }
89
90        Some(Box::new(Self::new(property_type, properties)))
91    }
92
93    fn clone_data_value(&self) -> Box<dyn DataValue> {
94        Box::new(Self::new(
95            self.property_type.clone(),
96            self.properties.clone(),
97        ))
98    }
99
100    // Conversions
101
102    fn as_vec(&self) -> Vec<Box<dyn DataValue>> {
103        self.properties
104            .iter()
105            .map(|property| property.property_value.clone_data_value())
106            .collect()
107    }
108}