1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::collections::BTreeMap;
pub type Params = BTreeMap<String, Option<String>>;
pub trait ServiceParams {
fn put<T: ToParam>(&mut self, key: &str, val: T);
fn put_key(&mut self, key: &str);
}
impl ServiceParams for Params {
fn put<T: ToParam>(&mut self, key: &str, val: T) {
self.insert(key.to_owned(), Some(val.to_param()));
}
fn put_key(&mut self, key: &str) {
self.insert(key.to_owned(), None);
}
}
pub trait ToParam {
fn to_param(&self) -> String;
}
macro_rules! to_string_param {
( $($t:ty)* ) => {
$(
impl ToParam for $t {
fn to_param(&self) -> String {
self.to_string()
}
}
)*
}
}
to_string_param!( u8 bool f32 f64 i64 );
impl<'a, T> ToParam for &'a T
where
T: ToParam + ?Sized,
{
fn to_param(&self) -> String {
ToParam::to_param(&**self)
}
}
impl ToParam for str {
fn to_param(&self) -> String {
String::from(self)
}
}
impl ToParam for String {
fn to_param(&self) -> String {
self.clone()
}
}