rof_rs/object_format/data_value/
string.rs1use crate::object_format::{property::property_type::PropertyType, string_escaper::escape_string};
2
3use super::super::DataValue;
4
5#[derive(Debug)]
8pub struct DataValueString {
9 inner: String,
10}
11
12impl DataValueString {
13 pub fn new(inner_value: String) -> Self {
14 Self { inner: inner_value }
15 }
16}
17
18impl DataValue for DataValueString {
19 fn serialize(
20 &self,
21 _: bool,
22 _: usize,
23 ) -> (
24 PropertyType, String, ) {
27 (
28 PropertyType::simple(String::from("string")),
29 format!("\"{}\"", escape_string(&self.inner, &['"'])),
30 )
31 }
32
33 fn deserialize(
34 serialized_type: &crate::object_format::property::property_type::PropertyType,
35 serialized_value: &str,
36 ) -> Option<Box<dyn DataValue>>
37 where
38 Self: Sized,
39 {
40 if serialized_type.get_base_type() != "" && serialized_type.get_base_type() != "string"
41 || serialized_type.sub_types_included()
42 {
43 return None;
44 }
45
46 if serialized_value.starts_with("\"")
47 && serialized_value.ends_with("\"")
48 && serialized_value.chars().count() > 1
49 {
50 return Some(Box::new(Self::new(
51 serialized_value
52 .strip_prefix(|_| true)
53 .unwrap_or(serialized_value)
54 .strip_suffix(|_| true)
55 .unwrap_or(serialized_value)
56 .to_string()
57 .replace("\\", ""),
58 )));
59 }
60
61 None
62 }
63
64 fn clone_data_value(&self) -> Box<dyn DataValue> {
65 Box::new(Self::new(self.inner.clone()))
66 }
67
68 fn as_string(&self) -> String {
69 self.inner.clone()
70 }
71}