rust_web_server/json/array/integer/
mod.rs1use std::num::ParseIntError;
2use crate::json::array::RawUnprocessedJSONArray;
3use crate::symbol::SYMBOL;
4
5#[cfg(test)]
6mod example_list_i128;
7#[cfg(test)]
8mod example_list_i128_with_asserts;
9#[cfg(test)]
10mod example_list_i64_with_asserts;
11#[cfg(test)]
12mod example_list_i64;
13#[cfg(test)]
14mod example_list_i32_with_asserts;
15#[cfg(test)]
16mod example_list_i32;
17#[cfg(test)]
18mod example_list_i16_with_asserts;
19#[cfg(test)]
20mod example_list_i16;
21#[cfg(test)]
22mod example_list_i8_with_asserts;
23#[cfg(test)]
24mod example_list_i8;
25#[cfg(test)]
26mod example_list_u128_with_asserts;
27#[cfg(test)]
28mod example_list_u128;
29#[cfg(test)]
30mod example_list_u64_with_asserts;
31#[cfg(test)]
32mod example_list_u64;
33#[cfg(test)]
34mod example_list_u32_with_asserts;
35#[cfg(test)]
36mod example_list_u32;
37#[cfg(test)]
38mod example_list_u16_with_asserts;
39#[cfg(test)]
40mod example_list_u16;
41#[cfg(test)]
42mod example_list_u8_with_asserts;
43#[cfg(test)]
44mod example_list_u8;
45
46pub struct JSONArrayOfIntegers;
47impl JSONArrayOfIntegers {
48 pub fn parse_as_list_i128(json : String) -> Result<Vec<i128>, String> {
49 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
50 let mut list: Vec<i128> = vec![];
51 for item in items {
52 let boxed_parse = item.parse::<i128>();
53 if boxed_parse.is_err() {
54 let message = boxed_parse.err().unwrap().to_string();
55 return Err(message);
56 }
57 let num : i128 = boxed_parse.unwrap();
58 list.push(num);
59 }
60 Ok(list)
61 }
62
63 pub fn to_json_from_list_i128(items : &Vec<i128>) -> Result<String, String> {
64 let mut json_vec = vec![];
65 json_vec.push(SYMBOL.opening_square_bracket.to_string());
66 for (pos, item) in items.iter().enumerate() {
67 json_vec.push(item.to_string());
68 if pos != items.len() - 1 {
69 json_vec.push(SYMBOL.comma.to_string());
70 }
71 }
72 json_vec.push(SYMBOL.closing_square_bracket.to_string());
73
74 let result = json_vec.join(SYMBOL.empty_string);
75 Ok(result)
76 }
77
78 pub fn parse_as_list_i64(json : String) -> Result<Vec<i64>, String> {
79 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
80 let mut list: Vec<i64> = vec![];
81 for item in items {
82 let boxed_parse = item.parse::<i64>();
83 if boxed_parse.is_err() {
84 let message = boxed_parse.err().unwrap().to_string();
85 return Err(message);
86 }
87 let num : i64 = boxed_parse.unwrap();
88 list.push(num);
89 }
90 Ok(list)
91 }
92
93 pub fn to_json_from_list_i64(items : &Vec<i64>) -> Result<String, String> {
94 let mut json_vec = vec![];
95 json_vec.push(SYMBOL.opening_square_bracket.to_string());
96 for (pos, item) in items.iter().enumerate() {
97 json_vec.push(item.to_string());
98 if pos != items.len() - 1 {
99 json_vec.push(SYMBOL.comma.to_string());
100 }
101 }
102 json_vec.push(SYMBOL.closing_square_bracket.to_string());
103
104 let result = json_vec.join(SYMBOL.empty_string);
105 Ok(result)
106 }
107
108 pub fn parse_as_list_i32(json : String) -> Result<Vec<i32>, String> {
109 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
110 let mut list: Vec<i32> = vec![];
111 for item in items {
112 let boxed_parse = item.parse::<i32>();
113 if boxed_parse.is_err() {
114 let message = boxed_parse.err().unwrap().to_string();
115 return Err(message);
116 }
117 let num : i32 = boxed_parse.unwrap();
118 list.push(num);
119 }
120 Ok(list)
121 }
122
123 pub fn to_json_from_list_i32(items : &Vec<i32>) -> Result<String, String> {
124 let mut json_vec = vec![];
125 json_vec.push(SYMBOL.opening_square_bracket.to_string());
126 for (pos, item) in items.iter().enumerate() {
127 json_vec.push(item.to_string());
128 if pos != items.len() - 1 {
129 json_vec.push(SYMBOL.comma.to_string());
130 }
131 }
132 json_vec.push(SYMBOL.closing_square_bracket.to_string());
133
134 let result = json_vec.join(SYMBOL.empty_string);
135 Ok(result)
136 }
137
138 pub fn parse_as_list_i16(json : String) -> Result<Vec<i16>, String> {
139 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
140 let mut list: Vec<i16> = vec![];
141 for item in items {
142 let boxed_parse = item.parse::<i16>();
143 if boxed_parse.is_err() {
144 let message = boxed_parse.err().unwrap().to_string();
145 return Err(message);
146 }
147 let num : i16 = boxed_parse.unwrap();
148 list.push(num);
149 }
150 Ok(list)
151 }
152
153 pub fn to_json_from_list_i16(items : &Vec<i16>) -> Result<String, String> {
154 let mut json_vec = vec![];
155 json_vec.push(SYMBOL.opening_square_bracket.to_string());
156 for (pos, item) in items.iter().enumerate() {
157 json_vec.push(item.to_string());
158 if pos != items.len() - 1 {
159 json_vec.push(SYMBOL.comma.to_string());
160 }
161 }
162 json_vec.push(SYMBOL.closing_square_bracket.to_string());
163
164 let result = json_vec.join(SYMBOL.empty_string);
165 Ok(result)
166 }
167
168 pub fn parse_as_list_i8(json : String) -> Result<Vec<i8>, String> {
169 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
170 let mut list: Vec<i8> = vec![];
171 for item in items {
172 let boxed_parse = item.parse::<i8>();
173 if boxed_parse.is_err() {
174 let message = boxed_parse.err().unwrap().to_string();
175 return Err(message);
176 }
177 let num : i8 = boxed_parse.unwrap();
178 list.push(num);
179 }
180 Ok(list)
181 }
182
183 pub fn to_json_from_list_i8(items : &Vec<i8>) -> Result<String, String> {
184 let mut json_vec = vec![];
185 json_vec.push(SYMBOL.opening_square_bracket.to_string());
186 for (pos, item) in items.iter().enumerate() {
187 json_vec.push(item.to_string());
188 if pos != items.len() - 1 {
189 json_vec.push(SYMBOL.comma.to_string());
190 }
191 }
192 json_vec.push(SYMBOL.closing_square_bracket.to_string());
193
194 let result = json_vec.join(SYMBOL.empty_string);
195 Ok(result)
196 }
197
198 pub fn parse_as_list_u128(json : String) -> Result<Vec<u128>, String> {
199 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
200 let mut list: Vec<u128> = vec![];
201 for item in items {
202 let boxed_parse = item.parse::<u128>();
203 if boxed_parse.is_err() {
204 let message = boxed_parse.err().unwrap().to_string();
205 return Err(message);
206 }
207 let num : u128 = boxed_parse.unwrap();
208 list.push(num);
209 }
210 Ok(list)
211 }
212
213 pub fn to_json_from_list_u128(items : &Vec<u128>) -> Result<String, String> {
214 let mut json_vec = vec![];
215 json_vec.push(SYMBOL.opening_square_bracket.to_string());
216 for (pos, item) in items.iter().enumerate() {
217 json_vec.push(item.to_string());
218 if pos != items.len() - 1 {
219 json_vec.push(SYMBOL.comma.to_string());
220 }
221 }
222 json_vec.push(SYMBOL.closing_square_bracket.to_string());
223
224 let result = json_vec.join(SYMBOL.empty_string);
225 Ok(result)
226 }
227
228 pub fn parse_as_list_u64(json : String) -> Result<Vec<u64>, String> {
229 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
230 let mut list: Vec<u64> = vec![];
231 for item in items {
232 let boxed_parse : Result<u64, ParseIntError> = item.parse();
233 if boxed_parse.is_err() {
234 let message = boxed_parse.err().unwrap().to_string();
235 return Err(message);
236 }
237 let num : u64 = boxed_parse.unwrap();
238 list.push(num);
239 }
240 Ok(list)
241 }
242
243 pub fn to_json_from_list_u64(items : &Vec<u64>) -> Result<String, String> {
244 let mut json_vec = vec![];
245 json_vec.push(SYMBOL.opening_square_bracket.to_string());
246 for (pos, item) in items.iter().enumerate() {
247 json_vec.push(item.to_string());
248 if pos != items.len() - 1 {
249 json_vec.push(SYMBOL.comma.to_string());
250 }
251 }
252 json_vec.push(SYMBOL.closing_square_bracket.to_string());
253
254 let result = json_vec.join(SYMBOL.empty_string);
255 Ok(result)
256 }
257
258 pub fn parse_as_list_u32(json : String) -> Result<Vec<u32>, String> {
259 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
260 let mut list: Vec<u32> = vec![];
261 for item in items {
262 let boxed_parse : Result<u32, ParseIntError> = item.parse();
263 if boxed_parse.is_err() {
264 let message = boxed_parse.err().unwrap().to_string();
265 return Err(message);
266 }
267 let num : u32 = boxed_parse.unwrap();
268 list.push(num);
269 }
270 Ok(list)
271 }
272
273 pub fn to_json_from_list_u32(items : &Vec<u32>) -> Result<String, String> {
274 let mut json_vec = vec![];
275 json_vec.push(SYMBOL.opening_square_bracket.to_string());
276 for (pos, item) in items.iter().enumerate() {
277 json_vec.push(item.to_string());
278 if pos != items.len() - 1 {
279 json_vec.push(SYMBOL.comma.to_string());
280 }
281 }
282 json_vec.push(SYMBOL.closing_square_bracket.to_string());
283
284 let result = json_vec.join(SYMBOL.empty_string);
285 Ok(result)
286 }
287
288 pub fn parse_as_list_u16(json : String) -> Result<Vec<u16>, String> {
289 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
290 let mut list: Vec<u16> = vec![];
291 for item in items {
292 let boxed_parse : Result<u16, ParseIntError> = item.parse();
293 if boxed_parse.is_err() {
294 let message = boxed_parse.err().unwrap().to_string();
295 return Err(message);
296 }
297 let num : u16 = boxed_parse.unwrap();
298 list.push(num);
299 }
300 Ok(list)
301 }
302
303 pub fn to_json_from_list_u16(items : &Vec<u16>) -> Result<String, String> {
304 let mut json_vec = vec![];
305 json_vec.push(SYMBOL.opening_square_bracket.to_string());
306 for (pos, item) in items.iter().enumerate() {
307 json_vec.push(item.to_string());
308 if pos != items.len() - 1 {
309 json_vec.push(SYMBOL.comma.to_string());
310 }
311 }
312 json_vec.push(SYMBOL.closing_square_bracket.to_string());
313
314 let result = json_vec.join(SYMBOL.empty_string);
315 Ok(result)
316 }
317
318 pub fn parse_as_list_u8(json : String) -> Result<Vec<u8>, String> {
319 let items = RawUnprocessedJSONArray::split_into_vector_of_strings(json).unwrap();
320 let mut list: Vec<u8> = vec![];
321 for item in items {
322 let boxed_parse : Result<u8, ParseIntError> = item.parse();
323 if boxed_parse.is_err() {
324 let message = boxed_parse.err().unwrap().to_string();
325 return Err(message);
326 }
327 let num : u8 = boxed_parse.unwrap();
328 list.push(num);
329 }
330 Ok(list)
331 }
332
333 pub fn to_json_from_list_u8(items : &Vec<u8>) -> Result<String, String> {
334 let mut json_vec = vec![];
335 json_vec.push(SYMBOL.opening_square_bracket.to_string());
336 for (pos, item) in items.iter().enumerate() {
337 json_vec.push(item.to_string());
338 if pos != items.len() - 1 {
339 json_vec.push(SYMBOL.comma.to_string());
340 }
341 }
342 json_vec.push(SYMBOL.closing_square_bracket.to_string());
343
344 let result = json_vec.join(SYMBOL.empty_string);
345 Ok(result)
346 }
347}