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