1#[derive(Debug, Default, Clone, PartialEq, Eq)]
2pub struct QueryWriter {
3 pairs: Vec<(String, String)>,
4}
5
6impl QueryWriter {
7 pub fn push<T>(&mut self, key: &'static str, value: T)
8 where
9 T: ToString,
10 {
11 self.pairs.push((key.to_owned(), value.to_string()));
12 }
13
14 pub fn push_opt<T>(&mut self, key: &'static str, value: Option<T>)
15 where
16 T: ToString,
17 {
18 if let Some(value) = value {
19 self.push(key, value);
20 }
21 }
22
23 pub fn push_csv<I, T>(&mut self, key: &'static str, values: I)
24 where
25 I: IntoIterator<Item = T>,
26 T: ToString,
27 {
28 let value = values
29 .into_iter()
30 .map(|value| value.to_string())
31 .collect::<Vec<_>>()
32 .join(",");
33
34 if !value.is_empty() {
35 self.push(key, value);
36 }
37 }
38
39 #[must_use]
40 pub fn finish(self) -> Vec<(String, String)> {
41 self.pairs
42 }
43}