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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::fmt;
use serde::ser::{Serialize, Serializer};
use util::StrJoin;
pub struct OptionVal(pub String);
impl<'a> From<&'a str> for OptionVal {
fn from(from: &'a str) -> OptionVal {
OptionVal(from.to_owned())
}
}
from_exp!(String, OptionVal, from, OptionVal(from));
from_exp!(i32, OptionVal, from, OptionVal(from.to_string()));
from_exp!(i64, OptionVal, from, OptionVal(from.to_string()));
from_exp!(u32, OptionVal, from, OptionVal(from.to_string()));
from_exp!(u64, OptionVal, from, OptionVal(from.to_string()));
from_exp!(bool, OptionVal, from, OptionVal(from.to_string()));
pub struct Options<'a>(pub Vec<(&'a str, OptionVal)>);
impl<'a> Options<'a> {
pub fn new() -> Options<'a> {
Options(Vec::new())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn push<O: Into<OptionVal>>(&mut self, key: &'a str, val: O) {
self.0.push((key, val.into()));
}
}
impl<'a> fmt::Display for Options<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
if !self.is_empty() {
try!(formatter.write_str("?"));
try!(formatter.write_str(&self.0.iter().map(|&(ref k, ref v)| {
format!("{}={}", k, v.0)
}).join("&")));
}
Ok(())
}
}
macro_rules! add_option {
($n:ident, $e:expr) => (
pub fn $n<T: Into<OptionVal>>(&'a mut self, val: T) -> &'a mut Self {
self.options.push($e, val);
self
}
)
}
pub enum VersionType {
Internal,
External,
ExternalGt,
ExternalGte,
Force
}
impl Serialize for VersionType {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
self.to_string().serialize(serializer)
}
}
impl ToString for VersionType {
fn to_string(&self) -> String {
match *self {
VersionType::Internal => "internal",
VersionType::External => "external",
VersionType::ExternalGt => "external_gt",
VersionType::ExternalGte => "external_gte",
VersionType::Force => "force"
}.to_owned()
}
}
from_exp!(VersionType, OptionVal, from, OptionVal(from.to_string()));
pub enum Consistency {
One,
Quorum,
All
}
impl From<Consistency> for OptionVal {
fn from(from: Consistency) -> OptionVal {
OptionVal(match from {
Consistency::One => "one",
Consistency::Quorum => "quorum",
Consistency::All => "all"
}.to_owned())
}
}
pub enum DefaultOperator {
And,
Or
}
impl From<DefaultOperator> for OptionVal {
fn from(from: DefaultOperator) -> OptionVal {
OptionVal(match from {
DefaultOperator::And => "and",
DefaultOperator::Or => "or"
}.to_owned())
}
}