evergreen/osrf/
params.rs

1use crate::{EgResult, EgValue};
2use json::JsonValue;
3
4/// Generic container for translating various data types into a `Vec<JsonValue>`.
5///
6/// NOTE: `Into<ApiParams>` values that are Vec/&Vec's are treated as a
7/// list of individual API call parameters.  To pass a single parameter
8/// that is itself a list, pass the value as either a JsonValue::Array
9/// or as a (e.g.) vec![vec![1,2,3]].
10pub struct ApiParams {
11    params: Vec<EgValue>,
12}
13
14impl ApiParams {
15    /// Consumes the stored parameters
16    pub fn serialize(mut self) -> Vec<JsonValue> {
17        let mut arr: Vec<JsonValue> = Vec::new();
18        while !self.params.is_empty() {
19            arr.push(self.params.remove(0).into_json_value());
20        }
21
22        arr
23    }
24
25    pub fn take_params(&mut self) -> Vec<EgValue> {
26        std::mem::take(&mut self.params)
27    }
28
29    pub fn params(&self) -> &Vec<EgValue> {
30        &self.params
31    }
32
33    pub fn params_mut(&mut self) -> &mut Vec<EgValue> {
34        &mut self.params
35    }
36
37    /// Add a json value to the list of params
38    pub fn add(&mut self, v: EgValue) {
39        self.params.push(v)
40    }
41
42    pub fn from_json_value(v: JsonValue) -> EgResult<ApiParams> {
43        Ok(ApiParams::from(EgValue::from_json_value(v)?))
44    }
45}
46
47impl From<Vec<EgValue>> for ApiParams {
48    fn from(v: Vec<EgValue>) -> ApiParams {
49        ApiParams { params: v }
50    }
51}
52
53impl From<EgValue> for ApiParams {
54    fn from(v: EgValue) -> ApiParams {
55        ApiParams::from(vec![v])
56    }
57}
58
59impl From<Option<EgValue>> for ApiParams {
60    fn from(v: Option<EgValue>) -> ApiParams {
61        ApiParams {
62            params: match v {
63                Some(v) => vec![v],
64                None => Vec::new(),
65            },
66        }
67    }
68}
69
70impl From<&str> for ApiParams {
71    fn from(v: &str) -> ApiParams {
72        ApiParams::from(EgValue::from(v))
73    }
74}
75
76impl From<String> for ApiParams {
77    fn from(v: String) -> ApiParams {
78        ApiParams::from(EgValue::from(v))
79    }
80}
81
82impl From<i32> for ApiParams {
83    fn from(v: i32) -> ApiParams {
84        ApiParams::from(EgValue::from(v))
85    }
86}
87
88impl From<i64> for ApiParams {
89    fn from(v: i64) -> ApiParams {
90        ApiParams::from(EgValue::from(v))
91    }
92}
93
94impl From<u32> for ApiParams {
95    fn from(v: u32) -> ApiParams {
96        ApiParams::from(EgValue::from(v))
97    }
98}
99
100impl From<u64> for ApiParams {
101    fn from(v: u64) -> ApiParams {
102        ApiParams::from(EgValue::from(v))
103    }
104}
105
106impl From<u8> for ApiParams {
107    fn from(v: u8) -> ApiParams {
108        ApiParams::from(EgValue::from(v))
109    }
110}
111
112impl From<i8> for ApiParams {
113    fn from(v: i8) -> ApiParams {
114        ApiParams::from(EgValue::from(v))
115    }
116}
117
118impl From<usize> for ApiParams {
119    fn from(v: usize) -> ApiParams {
120        ApiParams::from(EgValue::from(v))
121    }
122}
123
124impl From<&Vec<&str>> for ApiParams {
125    fn from(v: &Vec<&str>) -> ApiParams {
126        ApiParams {
127            params: v.iter().map(|j| EgValue::from(*j)).collect(),
128        }
129    }
130}
131
132impl From<Vec<&str>> for ApiParams {
133    fn from(v: Vec<&str>) -> ApiParams {
134        ApiParams::from(&v)
135    }
136}
137
138impl From<&Vec<u8>> for ApiParams {
139    fn from(v: &Vec<u8>) -> ApiParams {
140        ApiParams {
141            params: v.iter().map(|j| EgValue::from(*j)).collect(),
142        }
143    }
144}
145
146impl From<Vec<u8>> for ApiParams {
147    fn from(v: Vec<u8>) -> ApiParams {
148        ApiParams::from(&v)
149    }
150}
151
152impl From<&Vec<i64>> for ApiParams {
153    fn from(v: &Vec<i64>) -> ApiParams {
154        ApiParams {
155            params: v.iter().map(|j| EgValue::from(*j)).collect(),
156        }
157    }
158}
159
160impl From<Vec<i64>> for ApiParams {
161    fn from(v: Vec<i64>) -> ApiParams {
162        ApiParams::from(&v)
163    }
164}
165
166impl From<&Vec<u64>> for ApiParams {
167    fn from(v: &Vec<u64>) -> ApiParams {
168        ApiParams {
169            params: v.iter().map(|j| EgValue::from(*j)).collect(),
170        }
171    }
172}
173
174impl From<Vec<u64>> for ApiParams {
175    fn from(v: Vec<u64>) -> ApiParams {
176        ApiParams::from(&v)
177    }
178}
179
180impl From<&Vec<String>> for ApiParams {
181    fn from(v: &Vec<String>) -> ApiParams {
182        ApiParams {
183            params: v.iter().map(|s| EgValue::from(s.as_str())).collect(),
184        }
185    }
186}
187
188impl From<Vec<String>> for ApiParams {
189    fn from(v: Vec<String>) -> ApiParams {
190        ApiParams::from(&v)
191    }
192}