1use crate::{
2 ByteSequence, etcdserverpb,
3 etcdserverpb::range_request::{SortOrder, SortTarget},
4};
5
6#[derive(Debug, Clone)]
22pub struct GetOptions {
23 pub end_key: Option<ByteSequence>, pub limit: i64, pub revision: i64, pub sort_order: SortOrder, pub sort_target: SortTarget, pub serializable: bool, pub keys_only: bool, pub count_only: bool, pub min_mod_revision: i64, pub max_mod_revision: i64, pub min_create_revision: i64, pub max_create_revision: i64, pub prefix: bool, }
37
38#[derive(Debug, Clone, Default)]
48pub struct GetOptionsBuilder {
49 end_key: Option<ByteSequence>,
50 limit: Option<i64>,
51 revision: Option<i64>,
52 sort_order: Option<SortOrder>,
53 sort_target: Option<SortTarget>,
54 serializable: Option<bool>,
55 keys_only: Option<bool>,
56 count_only: Option<bool>,
57 min_mod_revision: Option<i64>,
58 max_mod_revision: Option<i64>,
59 min_create_revision: Option<i64>,
60 max_create_revision: Option<i64>,
61 prefix: Option<bool>,
62}
63
64impl GetOptions {
65 fn new() -> Self {
66 GetOptions {
67 end_key: None,
68 limit: 0,
69 revision: 0,
70 sort_order: SortOrder::None,
71 sort_target: SortTarget::Key,
72 serializable: false,
73 keys_only: false,
74 count_only: false,
75 min_mod_revision: 0,
76 max_mod_revision: 0,
77 min_create_revision: 0,
78 max_create_revision: 0,
79 prefix: false,
80 }
81 }
82
83 pub fn builder() -> GetOptionsBuilder {
93 GetOptionsBuilder::default()
94 }
95
96 pub fn default() -> Self {
103 Self::new()
104 }
105
106 pub fn to_request(self, key: &ByteSequence) -> etcdserverpb::RangeRequest {
119 let end_key = match self.prefix {
120 true => key.next(),
121 false => self.end_key.unwrap_or_else(|| ByteSequence::empty()),
122 };
123 etcdserverpb::RangeRequest {
124 key: key.as_bytes().to_vec(),
125 range_end: end_key.bytes,
126 limit: self.limit,
127 revision: self.revision,
128 sort_order: self.sort_order as i32,
129 sort_target: self.sort_target as i32,
130 serializable: self.serializable,
131 keys_only: self.keys_only,
132 count_only: self.count_only,
133 min_mod_revision: self.min_mod_revision,
134 max_mod_revision: self.max_mod_revision,
135 min_create_revision: self.min_create_revision,
136 max_create_revision: self.max_create_revision,
137 }
138 }
139}
140
141impl GetOptionsBuilder {
142 pub fn end_key(mut self, end_key: ByteSequence) -> Self {
151 self.end_key = Some(end_key);
152 self
153 }
154
155 pub fn limit(mut self, limit: i64) -> Self {
156 self.limit = Some(limit);
157 self
158 }
159
160 pub fn revision(mut self, revision: i64) -> Self {
161 self.revision = Some(revision);
162 self
163 }
164
165 pub fn sort_order(mut self, sort_order: SortOrder) -> Self {
166 self.sort_order = Some(sort_order);
167 self
168 }
169
170 pub fn sort_target(mut self, sort_target: SortTarget) -> Self {
171 self.sort_target = Some(sort_target);
172 self
173 }
174
175 pub fn serializable(mut self, serializable: bool) -> Self {
176 self.serializable = Some(serializable);
177 self
178 }
179
180 pub fn keys_only(mut self, keys_only: bool) -> Self {
181 self.keys_only = Some(keys_only);
182 self
183 }
184
185 pub fn count_only(mut self, count_only: bool) -> Self {
186 self.count_only = Some(count_only);
187 self
188 }
189
190 pub fn min_mod_revision(mut self, min_mod_revision: i64) -> Self {
191 self.min_mod_revision = Some(min_mod_revision);
192 self
193 }
194
195 pub fn max_mod_revision(mut self, max_mod_revision: i64) -> Self {
196 self.max_mod_revision = Some(max_mod_revision);
197 self
198 }
199
200 pub fn min_create_revision(mut self, min_create_revision: i64) -> Self {
201 self.min_create_revision = Some(min_create_revision);
202 self
203 }
204
205 pub fn max_create_revision(mut self, max_create_revision: i64) -> Self {
206 self.max_create_revision = Some(max_create_revision);
207 self
208 }
209
210 pub fn prefix(mut self, prefix: bool) -> Self {
211 self.prefix = Some(prefix);
212 self
213 }
214
215 pub fn build(self) -> GetOptions {
216 let mut options = GetOptions::new();
217
218 if let Some(end_key) = self.end_key {
219 options.end_key = Some(end_key);
220 }
221
222 if let Some(limit) = self.limit {
223 options.limit = limit;
224 }
225
226 if let Some(revision) = self.revision {
227 options.revision = revision;
228 }
229
230 if let Some(sort_order) = self.sort_order {
231 options.sort_order = sort_order;
232 }
233
234 if let Some(sort_target) = self.sort_target {
235 options.sort_target = sort_target;
236 }
237
238 if let Some(serializable) = self.serializable {
239 options.serializable = serializable;
240 }
241
242 if let Some(keys_only) = self.keys_only {
243 options.keys_only = keys_only;
244 }
245
246 if let Some(count_only) = self.count_only {
247 options.count_only = count_only;
248 }
249
250 if let Some(min_mod_revision) = self.min_mod_revision {
251 options.min_mod_revision = min_mod_revision;
252 }
253
254 if let Some(max_mod_revision) = self.max_mod_revision {
255 options.max_mod_revision = max_mod_revision;
256 }
257
258 if let Some(min_create_revision) = self.min_create_revision {
259 options.min_create_revision = min_create_revision;
260 }
261
262 if let Some(max_create_revision) = self.max_create_revision {
263 options.max_create_revision = max_create_revision;
264 }
265
266 if let Some(prefix) = self.prefix {
267 options.prefix = prefix;
268 }
269
270 options
271 }
272}