rs_es/operations/
common.rs1use std::fmt;
20
21use serde::ser::{Serialize, Serializer};
22
23use crate::util::StrJoin;
24
25#[derive(Debug)]
28pub struct OptionVal(pub String);
29
30impl<'a> From<&'a str> for OptionVal {
32 fn from(from: &'a str) -> OptionVal {
33 OptionVal(from.to_owned())
34 }
35}
36
37from_exp!(String, OptionVal, from, OptionVal(from));
39from_exp!(i32, OptionVal, from, OptionVal(from.to_string()));
40from_exp!(i64, OptionVal, from, OptionVal(from.to_string()));
41from_exp!(u32, OptionVal, from, OptionVal(from.to_string()));
42from_exp!(u64, OptionVal, from, OptionVal(from.to_string()));
43from_exp!(bool, OptionVal, from, OptionVal(from.to_string()));
44
45#[derive(Default, Debug)]
47pub struct Options<'a>(pub Vec<(&'a str, OptionVal)>);
48
49impl<'a> Options<'a> {
50 pub fn new() -> Options<'a> {
51 Options(Vec::new())
52 }
53
54 pub fn is_empty(&self) -> bool {
55 self.0.is_empty()
56 }
57
58 pub fn push<O: Into<OptionVal>>(&mut self, key: &'a str, val: O) {
67 self.0.push((key, val.into()));
68 }
69}
70
71impl<'a> fmt::Display for Options<'a> {
72 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
73 if !self.is_empty() {
74 formatter.write_str("?")?;
75 formatter.write_str(
76 &self
77 .0
78 .iter()
79 .map(|&(ref k, ref v)| format!("{}={}", k, v.0))
80 .join("&"),
81 )?;
82 }
83 Ok(())
84 }
85}
86
87macro_rules! add_option {
90 ($n:ident, $e:expr) => (
91 pub fn $n<T: Into<OptionVal>>(&'a mut self, val: T) -> &'a mut Self {
92 self.options.push($e, val);
93 self
94 }
95 )
96}
97
98#[derive(Debug)]
100pub enum VersionType {
101 Internal,
102 External,
103 ExternalGt,
104 ExternalGte,
105 Force,
106}
107
108impl Serialize for VersionType {
109 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
110 where
111 S: Serializer,
112 {
113 self.to_string().serialize(serializer)
114 }
115}
116
117impl ToString for VersionType {
118 fn to_string(&self) -> String {
119 match *self {
120 VersionType::Internal => "internal",
121 VersionType::External => "external",
122 VersionType::ExternalGt => "external_gt",
123 VersionType::ExternalGte => "external_gte",
124 VersionType::Force => "force",
125 }
126 .to_owned()
127 }
128}
129
130from_exp!(VersionType, OptionVal, from, OptionVal(from.to_string()));
131
132#[derive(Debug)]
134pub enum Consistency {
135 One,
136 Quorum,
137 All,
138}
139
140impl From<Consistency> for OptionVal {
141 fn from(from: Consistency) -> OptionVal {
142 OptionVal(
143 match from {
144 Consistency::One => "one",
145 Consistency::Quorum => "quorum",
146 Consistency::All => "all",
147 }
148 .to_owned(),
149 )
150 }
151}
152
153#[derive(Debug)]
155pub enum DefaultOperator {
156 And,
157 Or,
158}
159
160impl From<DefaultOperator> for OptionVal {
161 fn from(from: DefaultOperator) -> OptionVal {
162 OptionVal(
163 match from {
164 DefaultOperator::And => "and",
165 DefaultOperator::Or => "or",
166 }
167 .to_owned(),
168 )
169 }
170}