rcfe_core/options/
get.rs

1use crate::{
2    ByteSequence, etcdserverpb,
3    etcdserverpb::range_request::{SortOrder, SortTarget},
4};
5
6/// Options for Get operations
7/// # Fields
8/// * `end_key` - Optional end key for range queries
9/// * `limit` - limit on number of results
10/// * `revision` - revision to read from
11/// * `sort_order` - sort order
12/// * `sort_target` - sort target
13/// * `serializable` - serializable read
14/// * `keys_only` - keys only flag
15/// * `count_only` - count only flag
16/// * `min_mod_revision` - minimum modification revision
17/// * `max_mod_revision` - maximum modification revision
18/// * `min_create_revision` - minimum creation revision
19/// * `max_create_revision` - maximum creation revision
20/// * `prefix` - prefix flag
21#[derive(Debug, Clone)]
22pub struct GetOptions {
23    pub end_key: Option<ByteSequence>, // Optional end key for range queries
24    pub limit: i64,                    // limit on number of results
25    pub revision: i64,                 // revision to read from
26    pub sort_order: SortOrder,         // sort order
27    pub sort_target: SortTarget,       // sort target
28    pub serializable: bool,            // serializable read
29    pub keys_only: bool,               // keys only flag
30    pub count_only: bool,              // count only flag
31    pub min_mod_revision: i64,         // minimum modification revision
32    pub max_mod_revision: i64,         // maximum modification revision
33    pub min_create_revision: i64,      // minimum creation revision
34    pub max_create_revision: i64,      // maximum creation revision
35    pub prefix: bool,                  // prefix flag
36}
37
38/// Builder for GetOptions
39/// # Examples
40/// ```rust
41/// use rcfe_core::options::kv::{GetOptions, GetOptionsBuilder};
42/// let get_options = GetOptions::builder()
43///     .limit(10)
44///     .serializable(true)
45///     .build();
46/// ```
47#[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    /// Creates a builder for GetOptions
84    /// # Examples
85    /// ```rust
86    /// use rcfe_core::options::kv::{GetOptions, GetOptionsBuilder};
87    /// let get_options = GetOptions::builder()
88    ///     .limit(10)
89    ///     .serializable(true)
90    ///     .build();
91    /// ```
92    pub fn builder() -> GetOptionsBuilder {
93        GetOptionsBuilder::default()
94    }
95
96    /// Creates a default GetOptions instance
97    /// # Examples
98    /// ```rust
99    /// use rcfe_core::options::kv::GetOptions;
100    /// let get_options = GetOptions::default();
101    /// ```
102    pub fn default() -> Self {
103        Self::new()
104    }
105
106    /// Converts GetOptions to an etcdserverpb::RangeRequest
107    /// # Arguments
108    /// * `key` - The key to get
109    /// # Returns
110    /// * `etcdserverpb::RangeRequest` - The corresponding RangeRequest
111    /// # Examples
112    /// ```rust
113    /// use rcfe_core::options::kv::{GetOptions, ByteSequence};
114    /// let get_options = GetOptions::default();
115    /// let key = ByteSequence::from("my_key");
116    /// let range_request = get_options.to_request(&key);
117    /// ```
118    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    /// Sets the end key for range queries.
143    /// # Examples
144    /// ```rust
145    /// use rcfe_core::options::kv::{GetOptionsBuilder, ByteSequence};
146    /// let get_options = GetOptionsBuilder::default()
147    ///     .end_key(ByteSequence::from("end_key"))
148    ///     .build();
149    /// ```
150    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}