rust_web_server/json/property/
mod.rs1use crate::json::{JSON_TYPE};
2use std::fmt::{Display, Formatter};
3use crate::core::New;
4use crate::null::Null;
5use crate::symbol::SYMBOL;
6
7#[cfg(test)]
8mod tests;
9
10pub struct JSONProperty {
11 pub property_name: String,
12 pub property_type: String,
13}
14
15pub struct JSONValue {
16 pub f64: Option<f64>,
17 pub i128: Option<i128>,
18 pub string: Option<String>,
19 pub object: Option<String>,
20 pub array: Option<String>,
21 pub bool: Option<bool>,
22 pub null: Option<Null>,
23}
24
25impl New for JSONValue {
26 fn new() -> JSONValue {
27 JSONValue {
28 f64: None,
29 i128: None,
30 string: None,
31 object: None,
32 array: None,
33 bool: None,
34 null: None,
35 }
36 }
37}
38
39impl JSONValue {
40 pub fn float_number_with_precision(&self, number_of_digits: u8) -> String {
41 let number = self.f64.as_ref().unwrap();
42 let formatted = format!("{0:.1$}", number, number_of_digits as usize);
43 formatted.to_string()
44 }
45}
46
47impl Display for JSONValue {
48 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49 if self.f64.is_some() {
50 let f64 = self.f64.unwrap();
51 let formatted : String = format!("{:.13}", f64);
52 return f.write_str(formatted.as_str());
53 }
54
55 if self.i128.is_some() {
56 let formatted = self.i128.unwrap().to_string();
57 return f.write_str(formatted.as_str());
58 }
59
60 if self.string.is_some() {
61 let formatted = self.string.as_ref().unwrap();
62 return f.write_str(formatted.as_str());
63 }
64
65 if self.array.is_some() {
66 let formatted = self.array.as_ref().unwrap();
67 return f.write_str(formatted.as_str());
68 }
69
70 if self.null.is_some() {
71 return f.write_str("null");
72 }
73
74 if self.object.is_some() {
75 let formatted = self.object.as_ref().unwrap();
76 return f.write_str(formatted.as_str());
77 }
78
79 if self.bool.is_some() {
80 let formatted = self.bool.as_ref().unwrap();
81 return f.write_str(formatted.to_string().as_str());
82 }
83
84 f.write_str("Something Went Wrong. There is no value for any type.")
85
86 }
87}
88
89impl JSONProperty {
90 pub fn parse(raw_string: &str) -> Result<(JSONProperty, JSONValue), String> {
91 let mut property = JSONProperty { property_name: "".to_string(), property_type: "".to_string() };
92 let mut value = JSONValue {
93 f64: None,
94 i128: None,
95 string: None,
96 object: None,
97 array: None,
98 bool: None,
99 null: None,
100 };
101
102 let boxed_split = raw_string.trim().split_once(SYMBOL.colon);
103 if boxed_split.is_none() {
104 let message = format!("Not a valid string as a key-value: {}", raw_string);
105 return Err(message);
106 }
107
108 let (mut _key, mut _value) = boxed_split.unwrap();
109 _key = _key.trim();
110 _value = _value.trim();
111
112 let is_null = _value == "null";
113 let is_string = _value.starts_with(SYMBOL.quotation_mark) && _value.ends_with(SYMBOL.quotation_mark);
114 let is_array = _value.starts_with(SYMBOL.opening_square_bracket) && _value.ends_with(SYMBOL.closing_square_bracket);
115 let is_object = _value.starts_with(SYMBOL.opening_curly_bracket) && _value.ends_with(SYMBOL.closing_curly_bracket);
116 let is_boolean = (_value == "true") || (_value == "false");
117 let is_number = !is_string && !is_null && !is_array && !is_object && !is_boolean;
118
119 if !is_null && !is_string && !is_array && !is_object && !is_number && !is_boolean {
120 let message = format!("Is not valid key value pair: {} {}", _key, _value);
121 return Err(message);
122 }
123
124
125
126 if is_null {
127 property.property_type = JSON_TYPE.string.to_string();
128 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
129 value.null = Some(Null{});
130
131 }
132
133 if is_string {
134 property.property_type = JSON_TYPE.string.to_string();
135 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
136 value.string = Some(_value.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string());
137 }
138
139 if is_number {
140 let boxed_i128_parse = _value.parse::<i128>();
141 if boxed_i128_parse.is_err() {
142 let boxed_f64_parse = _value.parse::<f64>();
143 if boxed_f64_parse.is_err() {
144 let message = format!("unable to parse number: {}: {}", _key, _value);
145 return Err(message);
146 } else {
147 property.property_type = JSON_TYPE.number.to_string();
148 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
149 let f64 = boxed_f64_parse.unwrap();
150 value.f64 = Some(f64);
151 }
152 } else {
153 property.property_type = JSON_TYPE.integer.to_string();
154 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
155 let i128 = boxed_i128_parse.unwrap();
156 value.i128 = Some(i128);
157 }
158 }
159
160 if is_array {
161 property.property_type = JSON_TYPE.array.to_string();
162 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
163 value.array = Some(_value.to_string());
164 }
165
166 if is_object {
167 property.property_type = JSON_TYPE.object.to_string();
168 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
169 value.object = Some(_value.to_string());
170 }
171
172 if is_boolean {
173 let is_true = _value == "true";
174 property.property_type = JSON_TYPE.boolean.to_string();
175 property.property_name = _key.replace(SYMBOL.quotation_mark, SYMBOL.empty_string).to_string();
176 value.bool = Some(is_true);
177 }
178
179 Ok((property, value))
180 }
181}
182
183
184