wjp/
helper.rs

1use std::collections::HashMap;
2use std::error::Error;
3
4use crate::error::ParseError;
5use crate::values::Values;
6/// Helper Trait for Serializing JSON
7pub trait SerializeHelper<T> {
8    /// directly get T without any further checks
9    /// Warning: this operation calls the [`.unwrap()`] method
10    ///
11    /// [`.unwrap()`]: Option::unwrap
12    fn get_val_unsafe(&self, attr: &str, fun: fn(&Values) -> Option<T>) -> T;
13    /// get an [`Option<T>`] without the Error message why the operation maybe failed
14    ///
15    /// [`Option<T>`]: Option
16    fn get_val_opt(&self, attr: &str, fun: fn(&Values) -> Option<T>) -> Option<T>;
17    /// get a Result of T or [`ParseError`] containing Info why the operation failed.
18    /// In this case the function only takes a referenced [`Values`] object and returns an [`Option<T>`]
19    ///
20    /// [`Option<T>`]: Option
21    fn get_val_res(&self, attr: &str, fun: fn(&Values) -> Option<T>) -> Result<T, ParseError>;
22    /// get a Result of T or [`ParseError`] containing Info why the operation failed.
23    /// In this case the function only takes a [`Values`] object and returns an [`Option<T>`]
24    ///
25    /// [`Option<T>`]: Option
26    fn rm_val(&mut self, attr: &str, fun: fn(Values) -> Option<T>) -> Result<T, ParseError>;
27    /// get a Result of T or [`ParseError`] containing Info why the operation failed.
28    /// In this case the function only takes a [`Values`] object and returns an [`Result<T,ParseError>`]
29    ///
30    /// [`Result<T,ParseError>`]: Result
31    fn map_val(
32        &mut self,
33        attr: &str,
34        fun: fn(Values) -> Result<T, ParseError>,
35    ) -> Result<T, ParseError>;
36    /// get a Result of T or [`ParseError`] containing Info why the operation failed.
37    /// In this case the function only takes a referenced [`Values`] object and returns an [`Result<T,ParseError>`]
38    ///
39    /// [`Result<T,ParseError>`]: Result
40    fn map_ref_val(
41        &mut self,
42        attr: &str,
43        fun: fn(&Values) -> Result<T, ParseError>,
44    ) -> Result<T, ParseError>;
45    /// get a Result of T or [`ParseError`] containing Info why the operation failed.
46    /// In this case the function only takes a referenced [`Values`] object and returns an [`Result<T,E>`]
47    ///
48    /// [`Result<T,E>`]: Result
49    fn map_val_and_err<E: Error>(
50        &mut self,
51        attr: &str,
52        fun: fn(Values) -> Result<T, E>,
53    ) -> Result<T, ParseError>;
54    /// get a Result of [`Option<T>`] or [`ParseError`] containing Info why the operation failed.
55    /// In this case the function only takes a referenced [`Values`] object and returns an [`Result<T,ParseError>`]
56    ///
57    /// [`Result<T,ParseError>`]: Result
58    /// [`Option<T>`]: Option
59    fn map_opt_val(
60        &mut self,
61        attr: &str,
62        fun: fn(Values) -> Option<T>,
63    ) -> Result<Option<T>, ParseError>;
64}
65
66impl<T> SerializeHelper<T> for HashMap<String, Values> {
67    fn get_val_unsafe(&self, attr: &str, fun: fn(&Values) -> Option<T>) -> T {
68        SerializeHelper::get_val_res(self, attr, fun).unwrap()
69    }
70    fn get_val_opt(&self, attr: &str, fun: fn(&Values) -> Option<T>) -> Option<T> {
71        SerializeHelper::get_val_res(self, attr, fun).ok()
72    }
73    fn get_val_res(&self, attr: &str, fun: fn(&Values) -> Option<T>) -> Result<T, ParseError> {
74        self.get(&String::from(attr))
75            .map(fun)
76            .ok_or(ParseError::new())?
77            .ok_or(ParseError::new())
78    }
79    fn rm_val(&mut self, attr: &str, fun: fn(Values) -> Option<T>) -> Result<T, ParseError> {
80        self.remove(&String::from(attr))
81            .map(fun)
82            .ok_or(ParseError::new())?
83            .ok_or(ParseError::new())
84    }
85    fn map_val(
86        &mut self,
87        attr: &str,
88        fun: fn(Values) -> Result<T, ParseError>,
89    ) -> Result<T, ParseError> {
90        self.remove(&String::from(attr))
91            .map(fun)
92            .ok_or(ParseError::new())?
93    }
94    fn map_ref_val(
95        &mut self,
96        attr: &str,
97        fun: fn(&Values) -> Result<T, ParseError>,
98    ) -> Result<T, ParseError> {
99        self.get(&String::from(attr))
100            .map(fun)
101            .ok_or(ParseError::new())?
102    }
103    fn map_val_and_err<E: Error>(
104        &mut self,
105        attr: &str,
106        fun: fn(Values) -> Result<T, E>,
107    ) -> Result<T, ParseError> {
108        self.remove(&String::from(attr))
109            .map(fun)
110            .ok_or(ParseError::new())?
111            .map_err(|_err| ParseError::new())
112    }
113    fn map_opt_val(
114        &mut self,
115        attr: &str,
116        fun: fn(Values) -> Option<T>,
117    ) -> Result<Option<T>, ParseError> {
118        self.remove(attr).map(fun).ok_or(ParseError::new())
119    }
120}