rust_web_server/json/array/string/
mod.rs1use crate::json::array::RawUnprocessedJSONArray;
2use crate::symbol::SYMBOL;
3
4#[cfg(test)]
5mod example_list_string_with_asserts;
6#[cfg(test)]
7mod example_list_string;
8
9pub struct JSONArrayOfStrings;
10impl JSONArrayOfStrings {
11 pub fn parse_as_list_string(json : String) -> Result<Vec<String>, String> {
12 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
13 let mut list: Vec<String> = vec![];
14 for item in items {
15 let boxed_parse = item.parse::<String>();
16 let mut string: String = boxed_parse.unwrap().trim().to_string();
17 let starts_with_quotation_mark = string.chars().next().unwrap() == '"';
18 let ends_with_quotation_mark = string.chars().last().unwrap() == '"';
19 if starts_with_quotation_mark && ends_with_quotation_mark {
20 let number_of_characters = string.len() - 1;
21 string = string[1..number_of_characters].to_string();
22 } else {
23 let message = format!("not a string: {}", item.to_string());
24 return Err(message);
25 }
26 list.push(string);
27 }
28 Ok(list)
29 }
30
31 pub fn to_json_from_list_string(items : &Vec<String>) -> Result<String, String> {
32 let mut json_vec = vec![];
33 json_vec.push(SYMBOL.opening_square_bracket.to_string());
34 for (pos, item) in items.iter().enumerate() {
35 json_vec.push(SYMBOL.quotation_mark.to_string());
36 json_vec.push(item.to_string());
37 json_vec.push(SYMBOL.quotation_mark.to_string());
38 if pos != items.len() - 1 {
39 json_vec.push(SYMBOL.comma.to_string());
40 }
41 }
42 json_vec.push(SYMBOL.closing_square_bracket.to_string());
43
44 let result = json_vec.join(SYMBOL.empty_string);
45 Ok(result)
46 }
47
48}