rust_web_server/json/array/
mod.rs1use std::io;
2use std::io::Read;
3use crate::symbol::SYMBOL;
4
5#[cfg(test)]
6mod tests;
7
8pub mod boolean;
9pub mod string;
10pub mod null;
11pub mod float;
12pub mod integer;
13pub mod object;
14
15pub struct RawUnprocessedJSONArray;
16impl RawUnprocessedJSONArray {
17 pub fn split_into_vector_of_strings(_json_string: String) -> Result<Vec<String>, String> {
18 let mut list : Vec<String> = vec![];
19
20 let mut is_end_of_json_string = false;
22 let mut cursor = io::Cursor::new(_json_string.to_string());
23 let mut bytes_read : i128 = 0;
24 let total_bytes : i128 = _json_string.len() as i128;
25
26
27 let mut read_until_start_of_array = true;
30 while read_until_start_of_array {
31
32 if is_end_of_json_string {
33 let message = format!("not proper end of the json array: {}", _json_string.to_string());
34 return Err(message);
35 }
36
37 let byte = 0;
38 let mut char_buffer = vec![byte];
39 let length = char_buffer.len();
40 let boxed_read = cursor.read_exact(&mut char_buffer);
41 if boxed_read.is_err() {
42 let message = boxed_read.err().unwrap().to_string();
43 return Err(message);
44 }
45 boxed_read.unwrap();
46 bytes_read = bytes_read + length as i128;
47 is_end_of_json_string = total_bytes == bytes_read;
48 if is_end_of_json_string {
49 let message = format!("not proper start of the json array: {}", _json_string.to_string());
50 return Err(message);
51 }
52 let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
53
54 if !char.is_whitespace() && char != '['{
55 let message = format!("input string does not start with opening square bracket: {} in {}", char, _json_string);
56 return Err(message);
57 }
58
59 if char == '[' {
60 read_until_start_of_array = false;
61 }
62 }
63
64
65
66 let mut is_end_of_array = false;
67 let mut token;
68 while !is_end_of_array {
69
70 let byte = 0;
71 let mut char_buffer = vec![byte];
72 let length = char_buffer.len();
73 let boxed_read = cursor.read_exact(&mut char_buffer);
74 if boxed_read.is_err() {
75 let message = boxed_read.err().unwrap().to_string();
76 return Err(message);
77 }
78 boxed_read.unwrap();
79 bytes_read = bytes_read + length as i128;
80 let mut char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
81
82 if char == ']' {
83 is_end_of_array = true;
84 }
85
86 if char != ' ' && char != ']' {
87 let is_string = char == '\"';
88 if is_string {
89 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
90
91 let mut not_end_of_string_property_value = true;
93 while not_end_of_string_property_value {
94 let byte = 0;
95 char_buffer = vec![byte];
96 let boxed_read = cursor.read_exact(&mut char_buffer);
97 if boxed_read.is_err() {
98 let message = boxed_read.err().unwrap().to_string();
99 return Err(message);
100 }
101 boxed_read.unwrap();
102 let length = char_buffer.len();
103 bytes_read = bytes_read + length as i128;
104 let _char = String::from_utf8(char_buffer).unwrap();
105 let last_char_in_buffer = token.chars().last().unwrap().to_string();
106 not_end_of_string_property_value = _char != "\"" && last_char_in_buffer != "\\";
107 token = [token, _char.to_string()].join(SYMBOL.empty_string);
108 }
109 list.push(token.to_string());
110
111 let is_whitespace = char == ' ';
113 if is_whitespace {
114 let mut read_till_end_of_whitespace = true;
115 while read_till_end_of_whitespace {
116 let byte = 0;
117 let mut char_buffer = vec![byte];
118 let length = char_buffer.len();
119 let boxed_read = cursor.read_exact(&mut char_buffer);
120 if boxed_read.is_err() {
121 let message = boxed_read.err().unwrap().to_string();
122 return Err(message);
123 }
124 boxed_read.unwrap();
125 bytes_read = bytes_read + length as i128;
126 char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
127
128 if char == ',' {
129 read_till_end_of_whitespace = false
130 } else {
131 if char == ']' {
132 read_till_end_of_whitespace = false;
133 is_end_of_array = true;
134 } else {
135 let message = format!("Missing comma between array items or closing square bracket at the end of array: {}", _json_string);
136 return Err(message);
137 }
138 }
139 }
140 }
141 }
142
143 let is_null = char == 'n';
144 if is_null {
145 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
147 let byte = 0;
148 let mut char_buffer = vec![byte, byte, byte];
149 let length = char_buffer.len();
150 let boxed_read = cursor.read_exact(&mut char_buffer);
151 if boxed_read.is_err() {
152 let message = boxed_read.err().unwrap().to_string();
153 return Err(message);
154 }
155 boxed_read.unwrap();
156 bytes_read = bytes_read + length as i128;
157 let remaining_bool = String::from_utf8(char_buffer).unwrap();
158 if remaining_bool != "ull" {
159 let message = format!("Unable to parse null: {} in {}", remaining_bool, _json_string);
160 return Err(message)
161 }
162 token = [token.to_string(), remaining_bool.to_string()].join(SYMBOL.empty_string);
163 list.push(token.to_string());
164 }
165
166 let is_boolean_true = char == 't';
167 if is_boolean_true {
168 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
170 let byte = 0;
171 let mut char_buffer = vec![byte, byte, byte];
172 let length = char_buffer.len();
173 let boxed_read = cursor.read_exact(&mut char_buffer);
174 if boxed_read.is_err() {
175 let message = boxed_read.err().unwrap().to_string();
176 return Err(message);
177 }
178 boxed_read.unwrap();
179 bytes_read = bytes_read + length as i128;
180 let remaining_bool = String::from_utf8(char_buffer).unwrap();
181 if remaining_bool != "rue" {
182 let message = format!("Unable to parse true: {} in {}", remaining_bool, _json_string);
183 return Err(message)
184 }
185 token = [token.to_string(), remaining_bool.to_string()].join(SYMBOL.empty_string);
186 list.push(token.to_string());
187 }
188
189 let is_boolean_false = char == 'f';
190 if is_boolean_false {
191 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
193 let byte = 0;
194 let mut char_buffer = vec![byte, byte, byte, byte];
195 let length = char_buffer.len();
196 let boxed_read = cursor.read_exact(&mut char_buffer);
197 if boxed_read.is_err() {
198 let message = boxed_read.err().unwrap().to_string();
199 return Err(message);
200 }
201 boxed_read.unwrap();
202 bytes_read = bytes_read + length as i128;
203 let remaining_bool = String::from_utf8(char_buffer).unwrap();
204 if remaining_bool != "alse" {
205 let message = format!("Unable to parse false: {} in {}", remaining_bool, _json_string);
206 return Err(message)
207 }
208 token = [token.to_string(), remaining_bool.to_string()].join(SYMBOL.empty_string);
209 list.push(token.to_string());
210 }
211
212 let is_array = char == '[';
213 if is_array {
214 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
216 let mut number_of_open_square_brackets = 1;
217 let mut number_of_closed_square_brackets = 0;
218
219 let mut read_nested_array = true;
220 while read_nested_array {
221
222 let byte = 0;
223 let mut char_buffer = vec![byte];
224 let length = char_buffer.len();
225 let boxed_read = cursor.read_exact(&mut char_buffer);
226 if boxed_read.is_err() {
227 let message = boxed_read.err().unwrap().to_string();
228 return Err(message);
229 }
230 boxed_read.unwrap();
231 bytes_read = bytes_read + length as i128;
232 let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
233
234 let is_open_square_bracket = char == '[';
235 if is_open_square_bracket {
236 number_of_open_square_brackets = number_of_open_square_brackets + 1;
237 }
238
239
240 let is_close_square_bracket = char == ']';
241 if is_close_square_bracket {
242 number_of_closed_square_brackets = number_of_closed_square_brackets + 1;
243 }
244
245 token = [token.to_string(), char.to_string()].join(SYMBOL.empty_string);
246
247 if number_of_open_square_brackets == number_of_closed_square_brackets {
248 list.push(token.to_string());
249 read_nested_array = false;
250 }
251 }
252 }
253
254
255 let is_nested_object = char == '{';
256 if is_nested_object {
257 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
259 let mut number_of_open_curly_braces = 1;
260 let mut number_of_closed_curly_braces = 0;
261
262 let mut read_nested_object = true;
263 while read_nested_object {
264
265 let byte = 0;
266 let mut char_buffer = vec![byte];
267 let length = char_buffer.len();
268 let boxed_read = cursor.read_exact(&mut char_buffer);
269 if boxed_read.is_err() {
270 let message = boxed_read.err().unwrap().to_string();
271 return Err(message);
272 }
273 boxed_read.unwrap();
274 bytes_read = bytes_read + length as i128;
275 let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
276
277 let is_open_curly_brace = char == '{';
278 if is_open_curly_brace {
279 number_of_open_curly_braces = number_of_open_curly_braces + 1;
280 }
281
282
283 let is_close_curly_brace = char == '}';
284 if is_close_curly_brace {
285 number_of_closed_curly_braces = number_of_closed_curly_braces + 1;
286 }
287
288 token = [token.to_string(), char.to_string()].join(SYMBOL.empty_string);
289
290 if number_of_open_curly_braces == number_of_closed_curly_braces {
291 list.push(token.to_string());
292 read_nested_object = false;
293 }
294 }
295 }
296
297 let mut is_comma_separator = char == ',';
298 let is_numeric = char.is_numeric();
299 let is_minus = char == '-';
300
301 let is_number =
302 !is_string &&
303 !is_null &&
304 !is_boolean_true &&
305 !is_boolean_false &&
306 !is_array &&
307 !is_nested_object &&
308 !is_comma_separator &&
309 (is_numeric || is_minus);
310 if is_number {
311 token = "".to_string();
312 if char != ',' {
314 token = ["".to_string(), char.to_string()].join(SYMBOL.empty_string);
315 }
316
317 let mut _is_point_symbol_already_used = false;
318 let mut _is_exponent_symbol_already_used = false;
319 let mut _is_minus_symbol_already_used = false;
320 if char == '-' {
321 _is_minus_symbol_already_used = true;
322 }
323
324 let mut read_number = true;
325 while read_number {
326
327 let byte = 0;
328 let mut char_buffer = vec![byte];
329 let length = char_buffer.len();
330 let boxed_read = cursor.read_exact(&mut char_buffer);
331 if boxed_read.is_err() {
332 let message = boxed_read.err().unwrap().to_string();
333 return Err(message);
334 }
335 boxed_read.unwrap();
336 bytes_read = bytes_read + length as i128;
337 char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
338
339 let is_numeric = char.is_numeric();
340
341 let is_point_symbol = char == '.';
342 if is_point_symbol && _is_point_symbol_already_used {
343 _is_point_symbol_already_used = true;
344 let message = format!("unable to parse number: {} in {}", token, _json_string);
345 return Err(message)
346 }
347 if is_point_symbol {
348 _is_point_symbol_already_used = true;
349 }
350
351 let is_exponent_symbol = char == 'e';
352 if is_exponent_symbol && _is_exponent_symbol_already_used {
353 _is_exponent_symbol_already_used = true;
354 let message = format!("unable to parse number: {} in {}", token, _json_string);
355 return Err(message)
356 }
357 if is_exponent_symbol {
358 _is_exponent_symbol_already_used = true;
359 }
360
361 let is_minus_symbol = char == '-';
362 if is_minus_symbol && _is_minus_symbol_already_used {
363 _is_minus_symbol_already_used = true;
364 let message = format!("unable to parse number: {} in {}", token, _json_string);
365 return Err(message)
366 }
367
368 let char_is_part_of_number = is_numeric || is_point_symbol || is_exponent_symbol || is_minus_symbol;
369 let is_whitespace = char == ' ';
371 if is_whitespace {
372 let mut read_till_end_of_whitespace = true;
373 while read_till_end_of_whitespace {
374 let byte = 0;
375 let mut char_buffer = vec![byte];
376 let length = char_buffer.len();
377 let boxed_read = cursor.read_exact(&mut char_buffer);
378 if boxed_read.is_err() {
379 let message = boxed_read.err().unwrap().to_string();
380 return Err(message);
381 }
382 boxed_read.unwrap();
383 bytes_read = bytes_read + length as i128;
384 char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
385
386 if char == ',' {
387 read_till_end_of_whitespace = false
388 } else {
389 if char == ']' {
390 read_till_end_of_whitespace = false;
391 is_end_of_array = true;
392 } else {
393 let message = format!("Missing comma between array items or closing square bracket at the end of array: {}", _json_string);
394 return Err(message);
395 }
396 }
397 }
398 }
399
400 if char_is_part_of_number {
401 token = [token, char.to_string()].join(SYMBOL.empty_string);
402 } else {
403 read_number = false;
404 if char != ',' && char != ']' {
406 let message = format!("unable to parse number: {} in {}", char, _json_string);
407 return Err(message)
408 }
409
410 }
411 }
412 list.push(token.to_string());
413 }
414
415 is_comma_separator = char == ',';
416 let is_ascii_control = char.is_ascii_control();
417 let is_carriage_return = char == '\r';
418 let is_newline = char == '\n';
419 let is_not_supported_type =
420 !is_string &&
421 !is_null &&
422 !is_boolean_true &&
423 !is_boolean_false &&
424 !is_array &&
425 !is_nested_object &&
426 !is_comma_separator &&
427 !is_carriage_return &&
428 !is_newline &&
429 !is_ascii_control &&
430 !is_numeric;
431 if is_not_supported_type {
432 let message = format!("unknown type: {} in {}", char, _json_string);
433 return Err(message);
434 }
435
436 }
437
438
439 if !is_end_of_array {
440 is_end_of_array = char == ']';
441 }
442
443 }
444
445
446 is_end_of_json_string = total_bytes == bytes_read;
447 let mut read_after_end_of_array = !is_end_of_json_string;
448 while read_after_end_of_array {
449
450 let byte = 0;
451 let mut char_buffer = vec![byte];
452 let length = char_buffer.len();
453 let boxed_read = cursor.read_exact(&mut char_buffer);
454 if boxed_read.is_err() {
455 let message = boxed_read.err().unwrap().to_string();
456 return Err(message);
457 }
458 boxed_read.unwrap();
459 bytes_read = bytes_read + length as i128;
460 let char = String::from_utf8(char_buffer).unwrap().chars().last().unwrap();
461
462 if !char.is_whitespace(){
463 let message = format!("after array there are some characters: {} in {}", char, _json_string);
464 return Err(message);
465 }
466
467 if bytes_read == total_bytes {
468 read_after_end_of_array = false;
469 }
470 }
471
472 Ok(list)
473 }
474}
475
476
477
478
479
480
481
482
483
484
485