rust_web_server/json/array/null/
mod.rs

1use crate::json::array::RawUnprocessedJSONArray;
2use crate::null::Null;
3use crate::symbol::SYMBOL;
4
5#[cfg(test)]
6mod example_list_null_with_asserts;
7#[cfg(test)]
8mod example_list_null;
9
10// hard to think about real use case
11pub struct JSONArrayOfNulls;
12impl JSONArrayOfNulls {
13    pub fn parse_as_list_null(json : String) -> Result<Vec<Null>, String> {
14        let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
15        let mut list: Vec<Null> = vec![];
16        for item in items {
17            let boxed_parse = item.parse::<Null>();
18            if boxed_parse.is_err() {
19                let message = boxed_parse.err().unwrap().message;
20                return Err(message);
21            }
22            let num : Null = boxed_parse.unwrap();
23            list.push(num);
24        }
25        Ok(list)
26    }
27
28    pub fn to_json_from_list_null(items : &Vec<&Null>) -> Result<String, String> {
29        let mut json_vec = vec![];
30        json_vec.push(SYMBOL.opening_square_bracket.to_string());
31        for (pos, item) in items.iter().enumerate() {
32            json_vec.push(item.to_string());
33            if pos != items.len() - 1 {
34                json_vec.push(SYMBOL.comma.to_string());
35            }
36        }
37        json_vec.push(SYMBOL.closing_square_bracket.to_string());
38
39        let result = json_vec.join(SYMBOL.empty_string);
40        Ok(result)
41    }
42
43}