montycat 0.1.7

Rust Client for Montycat - High-Performance NoSQL Database. The Fastest, Safest, and Most Elegant Database Client Ever Built in Rust.
Documentation
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, hash::Hash};

/// Represents a limit with start and stop values.
///
/// # Fields
/// - `start: usize` : The starting index of the limit.
/// - `stop: usize` : The stopping index of the limit.
///
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct Limit {
    pub start: usize,
    pub stop: usize,
}

impl Limit {
    /// Creates a default limit with start and stop set to 0.
    ///
    /// # Returns
    /// - `Self` : A new instance of `Limit` with default values.
    ///
    pub(crate) fn default_limit() -> Self {
        Self { start: 0, stop: 0 }
    }

    /// Converts the limit into a HashMap representation.
    ///
    /// # Returns
    /// - `HashMap<String, usize>` : A HashMap with "start" and "stop" keys.
    ///
    pub(crate) fn to_map(&self) -> HashMap<String, usize> {
        let mut map: HashMap<String, usize> = HashMap::new();
        map.insert("start".to_string(), self.start);
        map.insert("stop".to_string(), self.stop);
        map
    }

    /// Creates a new limit with specified start and stop values.
    ///
    /// # Arguments
    /// - `start: usize` : The starting index of the limit.
    /// - `stop: usize` : The stopping index of the limit.
    ///
    /// # Returns
    /// - `Self` : A new instance of `Limit` with the specified values.
    ///
    pub fn new(start: usize, stop: usize) -> Self {
        Self { start, stop }
    }
}

/// Represents a pointer with keyspace and key.
///
/// # Fields
/// - `keyspace: String` : The keyspace of the pointer.
/// - `key: String` : The key of the pointer.
///
/// # Examples
/// ```rust, ignore
/// let pointer = Pointer::new("my_keyspace", "my_key");
/// ```
///
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct Pointer {
    pub keyspace: String,
    pub key: String,
}

impl Pointer {
    /// Creates a new pointer with specified keyspace and key values.
    ///
    /// # Arguments
    /// - `keyspace: &str` : The keyspace of the pointer.
    /// - `key: &str` : The key of the pointer.
    ///
    /// # Returns
    /// - `Self` : A new instance of `Pointer` with the specified values.
    ///
    pub fn new(keyspace: &str, key: &str) -> Self {
        Self {
            keyspace: keyspace.to_owned(),
            key: key.to_owned(),
        }
    }

    /// Sets the pointer values and returns them as a tuple.
    ///
    /// # Arguments
    /// - `keyspace: &str` : The keyspace of the pointer.
    /// - `key: &str` : The key of the pointer.
    ///
    /// # Returns
    /// - `(String, String)` : A tuple containing the keyspace and key.
    ///
    /// # Examples
    /// ```rust, ignore
    /// let (keyspace, key) = Pointer::set_pointer("my_keyspace", "my_key");
    /// ```
    ///
    /// # Notes
    /// Method to be used when only the tuple representation is needed such as in update operations, lookups, etc.
    ///
    pub fn using(keyspace: &str, key: &str) -> (String, String) {
        (keyspace.to_owned(), key.to_owned())
    }
}

/// Represents a timestamp with an optional timestamp string.
///
/// # Fields
/// - `timestamp: Option<String>` : The timestamp string.
///
/// # Examples
/// ```rust, ignore
/// let ts = Timestamp::new("2024-01-01T00:00:00Z");
/// ```
///
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct Timestamp {
    timestamp: Option<String>,
}

impl Timestamp {
    /// Creates a new timestamp with the specified timestamp string.
    ///
    /// # Arguments
    /// - `timestamp: &str` : The timestamp string.
    ///
    /// # Returns
    /// - `Self` : A new instance of `Timestamp` with the specified value.
    ///
    pub fn new(timestamp: &str) -> Self {
        Self {
            timestamp: Some(timestamp.to_owned()),
        }
    }

    /// Sets the timestamp value and returns it as a string.
    ///
    /// # Arguments
    /// - `timestamp: &str` : The timestamp string.
    ///
    /// # Returns
    /// - `String` : The timestamp string.
    ///
    /// # Examples
    /// ```rust, ignore
    /// let ts_str = Timestamp::using("2024-01-01T00:00:00Z");
    /// ```
    ///
    /// # Notes
    /// Method to be used when only the string representation is needed such as in update operations.
    ///
    pub fn using(timestamp: &str) -> String {
        timestamp.to_owned()
    }

    /// Creates a HashMap representing an "after" timestamp criteria.
    ///
    /// # Arguments
    /// - `after: &str` : The "after" timestamp string.
    ///
    /// # Returns
    /// - `HashMap<String, String>` : A HashMap with the "after" key and its corresponding value.
    ///
    /// # Examples
    /// ```rust, ignore
    /// let after_map = Timestamp::after("2024-01-01T00:00:00Z");
    /// ```
    ///
    /// # Notes
    /// Method to be used when only the HashMap representation is needed such as in lookups.
    ///
    pub fn after(after: &str) -> HashMap<String, String> {
        let mut map: HashMap<String, String> = HashMap::with_capacity(1);
        map.insert("after".to_string(), after.to_owned());
        map
    }

    /// Creates a HashMap representing a "before" timestamp criteria.
    ///
    /// # Arguments
    /// - `before: &str` : The "before" timestamp string.
    ///
    /// # Returns
    /// - `HashMap<String, String>` : A HashMap with the "before" key and its corresponding value.
    ///
    /// # Examples
    /// ```rust, ignore
    /// let before_map = Timestamp::before("2024-01-01T00:00:00Z");
    /// ```
    /// # Notes
    /// Method to be used when only the HashMap representation is needed such as in lookups.
    ///
    pub fn before(before: &str) -> HashMap<String, String> {
        let mut map: HashMap<String, String> = HashMap::with_capacity(1);
        map.insert("before".to_string(), before.to_owned());
        map
    }

    /// Creates a HashMap representing a range of timestamps.
    ///
    /// # Arguments
    /// - `start: &str` : The starting timestamp string.
    /// - `stop: &str` : The stopping timestamp string.
    ///
    /// # Returns
    /// - `HashMap<String, Vec<String>>` : A HashMap with the "range_timestamp" key and its corresponding start and stop values.
    ///
    /// # Examples
    /// ```rust, ignore
    /// let range_map = Timestamp::range("2024-01-01T00:00:00Z", "2024-12-31T23:59:59Z");
    /// ```
    ///
    /// # Notes
    /// Method to be used when only the HashMap representation is needed such as in lookups.
    ///
    pub fn range(start: &str, stop: &str) -> HashMap<String, Vec<String>> {
        let mut map: HashMap<String, Vec<String>> = HashMap::with_capacity(1);
        map.insert(
            "range_timestamp".to_string(),
            vec![start.to_owned(), stop.to_owned()],
        );
        map
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ===== Limit Tests =====

    #[test]
    fn test_limit_new() {
        let limit = Limit::new(10, 20);
        assert_eq!(limit.start, 10);
        assert_eq!(limit.stop, 20);
    }

    #[test]
    fn test_limit_default_limit() {
        let limit = Limit::default_limit();
        assert_eq!(limit.start, 0);
        assert_eq!(limit.stop, 0);
    }

    #[test]
    fn test_limit_to_map() {
        let limit = Limit::new(5, 15);
        let map = limit.to_map();
        assert_eq!(map.get("start"), Some(&5));
        assert_eq!(map.get("stop"), Some(&15));
        assert_eq!(map.len(), 2);
    }

    #[test]
    fn test_limit_serialization() {
        let limit = Limit::new(1, 100);
        let serialized = serde_json::to_string(&limit).unwrap();
        let deserialized: Limit = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.start, 1);
        assert_eq!(deserialized.stop, 100);
    }

    #[test]
    fn test_limit_equality() {
        let limit1 = Limit::new(10, 20);
        let limit2 = Limit::new(10, 20);
        let limit3 = Limit::new(10, 21);
        assert_eq!(limit1, limit2);
        assert_ne!(limit1, limit3);
    }

    #[test]
    fn test_limit_default_trait() {
        let limit: Limit = Default::default();
        assert_eq!(limit.start, 0);
        assert_eq!(limit.stop, 0);
    }

    // ===== Pointer Tests =====

    #[test]
    fn test_pointer_new() {
        let pointer = Pointer::new("users", "user123");
        assert_eq!(pointer.keyspace, "users");
        assert_eq!(pointer.key, "user123");
    }

    #[test]
    fn test_pointer_using() {
        let (keyspace, key) = Pointer::using("products", "prod456");
        assert_eq!(keyspace, "products");
        assert_eq!(key, "prod456");
    }

    #[test]
    fn test_pointer_serialization() {
        let pointer = Pointer::new("orders", "order789");
        let serialized = serde_json::to_string(&pointer).unwrap();
        let deserialized: Pointer = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.keyspace, "orders");
        assert_eq!(deserialized.key, "order789");
    }

    #[test]
    fn test_pointer_equality() {
        let pointer1 = Pointer::new("test", "key1");
        let pointer2 = Pointer::new("test", "key1");
        let pointer3 = Pointer::new("test", "key2");
        assert_eq!(pointer1, pointer2);
        assert_ne!(pointer1, pointer3);
    }

    #[test]
    fn test_pointer_default() {
        let pointer: Pointer = Default::default();
        assert_eq!(pointer.keyspace, "");
        assert_eq!(pointer.key, "");
    }

    #[test]
    fn test_pointer_with_empty_strings() {
        let pointer = Pointer::new("", "");
        assert_eq!(pointer.keyspace, "");
        assert_eq!(pointer.key, "");
    }

    #[test]
    fn test_pointer_with_special_characters() {
        let pointer = Pointer::new("my-keyspace_123", "key:with:colons");
        assert_eq!(pointer.keyspace, "my-keyspace_123");
        assert_eq!(pointer.key, "key:with:colons");
    }

    // ===== Timestamp Tests =====

    #[test]
    fn test_timestamp_new() {
        let ts = Timestamp::new("2024-01-01T00:00:00Z");
        assert_eq!(ts.timestamp, Some("2024-01-01T00:00:00Z".to_string()));
    }

    #[test]
    fn test_timestamp_using() {
        let ts_str = Timestamp::using("2024-12-31T23:59:59Z");
        assert_eq!(ts_str, "2024-12-31T23:59:59Z");
    }

    #[test]
    fn test_timestamp_after() {
        let map = Timestamp::after("2024-06-01T00:00:00Z");
        assert_eq!(map.get("after"), Some(&"2024-06-01T00:00:00Z".to_string()));
        assert_eq!(map.len(), 1);
    }

    #[test]
    fn test_timestamp_before() {
        let map = Timestamp::before("2024-06-30T23:59:59Z");
        assert_eq!(map.get("before"), Some(&"2024-06-30T23:59:59Z".to_string()));
        assert_eq!(map.len(), 1);
    }

    #[test]
    fn test_timestamp_range() {
        let map = Timestamp::range("2024-01-01T00:00:00Z", "2024-12-31T23:59:59Z");
        let range = map.get("range_timestamp").unwrap();
        assert_eq!(range.len(), 2);
        assert_eq!(range[0], "2024-01-01T00:00:00Z");
        assert_eq!(range[1], "2024-12-31T23:59:59Z");
    }

    #[test]
    fn test_timestamp_serialization() {
        let ts = Timestamp::new("2024-03-15T12:30:00Z");
        let serialized = serde_json::to_string(&ts).unwrap();
        let deserialized: Timestamp = serde_json::from_str(&serialized).unwrap();
        assert_eq!(
            deserialized.timestamp,
            Some("2024-03-15T12:30:00Z".to_string())
        );
    }

    #[test]
    fn test_timestamp_equality() {
        let ts1 = Timestamp::new("2024-01-01T00:00:00Z");
        let ts2 = Timestamp::new("2024-01-01T00:00:00Z");
        let ts3 = Timestamp::new("2024-01-02T00:00:00Z");
        assert_eq!(ts1, ts2);
        assert_ne!(ts1, ts3);
    }

    #[test]
    fn test_timestamp_default() {
        let ts: Timestamp = Default::default();
        assert_eq!(ts.timestamp, None);
    }

    #[test]
    fn test_timestamp_with_different_formats() {
        let ts1 = Timestamp::new("2024-01-01");
        let ts2 = Timestamp::new("1704067200");
        let ts3 = Timestamp::new("2024-01-01T00:00:00.000Z");

        assert_eq!(ts1.timestamp, Some("2024-01-01".to_string()));
        assert_eq!(ts2.timestamp, Some("1704067200".to_string()));
        assert_eq!(ts3.timestamp, Some("2024-01-01T00:00:00.000Z".to_string()));
    }
}