rof_rs/object_format/data_value/
tuple.rs

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