rof_rs/object_format/property/
mod.rs1pub mod property_type;
2
3use std::fmt;
4
5use self::property_type::PropertyType;
6use super::data_value::{
7 array::DataValueArray, bool::DataValueBool, character::DataValueCharacter,
8 enum_value::DataValueEnum, float::DataValueFloat, hashmap::DataValueHashmap,
9 integer::DataValueInteger, string::DataValueString, struct_value::DataValueStruct,
10 tuple::DataValueTuple,
11};
12use super::DataValue;
13
14pub fn data_value_from_string(
15 data_type: &PropertyType,
16 serialized_string: &str,
17) -> Box<dyn DataValue> {
18 if let Some(boolean) = DataValueBool::deserialize(data_type, serialized_string) {
19 return boolean;
20 }
21
22 if let Some(character) = DataValueCharacter::deserialize(data_type, serialized_string) {
23 return character;
24 }
25
26 if let Some(integer) = DataValueInteger::deserialize(data_type, serialized_string) {
27 return integer;
28 }
29
30 if let Some(float) = DataValueFloat::deserialize(data_type, serialized_string) {
31 return float;
32 }
33
34 if let Some(string) = DataValueString::deserialize(data_type, serialized_string) {
35 return string;
36 }
37
38 if let Some(struct_value) = DataValueStruct::deserialize(data_type, serialized_string) {
39 return struct_value;
40 }
41
42 if let Some(tuple_value) = DataValueTuple::deserialize(data_type, serialized_string) {
43 return tuple_value;
44 }
45
46 if let Some(array_value) = DataValueArray::deserialize(data_type, serialized_string) {
47 return array_value;
48 }
49
50 if let Some(enum_value) = DataValueEnum::deserialize(data_type, serialized_string) {
51 return enum_value;
52 }
53
54 if let Some(hashmap_value) = DataValueHashmap::deserialize(data_type, serialized_string) {
55 return hashmap_value;
56 }
57
58 Box::new(DataValueEnum::none())
59}
60
61#[derive(Debug)]
62pub struct Property {
63 pub property_index: String,
64 pub property_value: Box<dyn DataValue>,
65}
66
67impl Clone for Property {
68 fn clone(&self) -> Self {
69 Self::new(
70 self.property_index.clone(),
71 self.property_value.clone_data_value(),
72 )
73 }
74}
75
76impl Property {
77 pub fn new(property_index: String, property_value: Box<dyn DataValue>) -> Self {
78 Self {
79 property_index,
80 property_value,
81 }
82 }
83
84 pub fn unnamed(property_value: Box<dyn DataValue>) -> Self {
85 Self::new(String::new(), property_value)
86 }
87
88 pub fn serialize(&self, pretty_print: bool, tab_index: usize) -> String {
89 match pretty_print {
90 true => match self.get_value_type().is_implicit() {
91 true => format!(
92 "{} = {}",
93 self.property_index,
94 self.property_value.serialize(pretty_print, tab_index).1
95 ),
96 false => format!(
97 "{}: {} = {}",
98 self.property_index,
99 self.get_value_type().serialize(pretty_print),
100 self.property_value.serialize(pretty_print, tab_index).1
101 ),
102 },
103 false => match self.get_value_type().is_implicit() {
104 true => format!(
105 "{}={}",
106 self.property_index,
107 self.property_value.serialize(pretty_print, tab_index).1
108 ),
109 false => format!(
110 "{}:{}={}",
111 self.property_index,
112 self.get_value_type().serialize(pretty_print),
113 self.property_value.serialize(pretty_print, tab_index).1
114 ),
115 },
116 }
117 }
118
119 pub fn deserialize(serialized_string: &str) -> Self {
120 let mut property_index: &str = "";
121
122 let mut property_value: Box<dyn DataValue> = Box::new(DataValueEnum::none());
123
124 if let Some((raw_data, raw_value)) = serialized_string.split_once("=") {
127 if let Some((raw_index, raw_type)) = raw_data.split_once(":") {
128 property_index = raw_index.trim();
131
132 property_value = data_value_from_string(
133 &PropertyType::deserialize(raw_type.trim()),
134 raw_value.trim(),
135 );
136 } else {
137 property_index = raw_data.trim();
140
141 property_value =
142 data_value_from_string(&PropertyType::implicit(), raw_value.trim());
143 }
144 }
145
146 Self {
147 property_index: property_index.to_string(),
148 property_value: property_value,
149 }
150 }
151
152 pub fn get_value_type(&self) -> PropertyType {
153 self.property_value.get_type()
154 }
155}
156
157impl fmt::Display for Property {
158 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159 write!(f, "{}", self.serialize(true, 0))
160 }
161}