falkordb 0.9.0

A FalkorDB Rust client
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright FalkorDB Ltd. 2023 - present
 * Licensed under the MIT License.
 */

//! FalkorDB temporal scalar values.
//!
//! FalkorDB encodes `datetime`, `date`, `time` and `duration` values in the compact protocol as a
//! single signed-integer scalar (type markers 13–16). These newtypes preserve that scalar verbatim
//! — the client performs no lossy calendar conversion — so callers keep full fidelity and can
//! interpret the value with whatever date/time library they prefer.
//!
//! Each value exposes its scalar as a typed [`Seconds`] (rather than a bare `i64`), and the instant
//! types support a small, type-safe algebra: subtracting two [`DateTime`]s yields a [`Duration`],
//! and a [`DateTime`] can be shifted by a [`Duration`]. Nonsensical combinations (e.g. adding two
//! instants) simply have no impl and fail to compile.
//!
//! As observed from the server: `date` is seconds since the Unix epoch at UTC midnight (negative
//! before 1970), `time`/`localtime` is a number of seconds, and `duration` is a span in seconds.

use crate::{parser::redis_value_as_int, FalkorResult};
use std::fmt;
use std::ops::{Add, Neg, Sub};

/// A signed number of seconds — the scalar carried by every FalkorDB temporal value.
///
/// This newtype keeps a temporal scalar from being silently mixed with an arbitrary integer; call
/// [`Seconds::get`] (or use the [`From`] conversions) for the underlying `i64`. The plain `+`/`-`
/// operators follow `i64` semantics (they panic on overflow in debug builds); the `checked_*`
/// methods return [`None`] on overflow instead.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Seconds(i64);

impl Seconds {
    /// Wraps a raw number of seconds.
    pub const fn new(value: i64) -> Self {
        Self(value)
    }

    /// Returns the underlying number of seconds.
    pub const fn get(self) -> i64 {
        self.0
    }

    /// Adds two second counts, returning [`None`] on `i64` overflow.
    pub fn checked_add(
        self,
        rhs: Seconds,
    ) -> Option<Seconds> {
        self.0.checked_add(rhs.0).map(Seconds)
    }

    /// Subtracts two second counts, returning [`None`] on `i64` overflow.
    pub fn checked_sub(
        self,
        rhs: Seconds,
    ) -> Option<Seconds> {
        self.0.checked_sub(rhs.0).map(Seconds)
    }

    /// Negates this second count, returning [`None`] on `i64` overflow (i.e. `i64::MIN`).
    pub fn checked_neg(self) -> Option<Seconds> {
        self.0.checked_neg().map(Seconds)
    }
}

impl fmt::Display for Seconds {
    fn fmt(
        &self,
        f: &mut fmt::Formatter<'_>,
    ) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<i64> for Seconds {
    fn from(value: i64) -> Self {
        Seconds(value)
    }
}

impl From<Seconds> for i64 {
    fn from(value: Seconds) -> Self {
        value.0
    }
}

impl Add for Seconds {
    type Output = Seconds;
    fn add(
        self,
        rhs: Seconds,
    ) -> Seconds {
        Seconds(self.0 + rhs.0)
    }
}

impl Sub for Seconds {
    type Output = Seconds;
    fn sub(
        self,
        rhs: Seconds,
    ) -> Seconds {
        Seconds(self.0 - rhs.0)
    }
}

impl Neg for Seconds {
    type Output = Seconds;
    fn neg(self) -> Seconds {
        Seconds(-self.0)
    }
}

macro_rules! temporal_scalar {
    ($(#[$meta:meta])* $name:ident, $kind:literal, $marker:literal) => {
        $(#[$meta])*
        #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
        pub struct $name {
            secs: i64,
        }

        impl $name {
            #[doc = concat!(
                "Wraps the raw FalkorDB scalar (compact type marker ", $marker, ") of a `", $kind,
                "` value, in seconds."
            )]
            pub const fn new(seconds: i64) -> Self {
                Self { secs: seconds }
            }

            #[doc = concat!(
                "Returns this `", $kind, "` value's scalar as a typed [`Seconds`]."
            )]
            pub const fn seconds(self) -> Seconds {
                Seconds::new(self.secs)
            }

            pub(crate) fn parse(value: redis::Value) -> FalkorResult<Self> {
                redis_value_as_int(value).map(Self::new)
            }
        }

        impl fmt::Display for $name {
            fn fmt(
                &self,
                f: &mut fmt::Formatter<'_>,
            ) -> fmt::Result {
                write!(f, "{}", self.secs)
            }
        }
    };
}

temporal_scalar!(
    /// A FalkorDB `datetime` value (compact type marker 13): seconds since the Unix epoch (UTC).
    ///
    /// Supports type-safe arithmetic: subtract two `DateTime`s for a [`Duration`], or add/subtract a
    /// [`Duration`] to shift the instant.
    DateTime,
    "datetime",
    "13"
);

temporal_scalar!(
    /// A FalkorDB `date` value (compact type marker 14).
    ///
    /// FalkorDB sends this as seconds since the Unix epoch at UTC midnight; it is negative for
    /// dates before 1970.
    Date,
    "date",
    "14"
);

temporal_scalar!(
    /// A FalkorDB `time` / `localtime` value (compact type marker 15), expressed in seconds.
    Time,
    "time",
    "15"
);

temporal_scalar!(
    /// A FalkorDB `duration` value (compact type marker 16): a span expressed in seconds.
    ///
    /// Supports type-safe arithmetic: add or subtract `Duration`s, negate one, or shift a
    /// [`DateTime`] by it.
    Duration,
    "duration",
    "16"
);

impl DateTime {
    /// Returns the [`Duration`] elapsed from `earlier` until `self`, or [`None`] on `i64` overflow.
    pub fn checked_duration_since(
        self,
        earlier: DateTime,
    ) -> Option<Duration> {
        self.secs.checked_sub(earlier.secs).map(Duration::new)
    }

    /// Shifts this instant forward by `rhs`, returning [`None`] on `i64` overflow.
    pub fn checked_add(
        self,
        rhs: Duration,
    ) -> Option<DateTime> {
        self.secs.checked_add(rhs.secs).map(DateTime::new)
    }

    /// Shifts this instant backward by `rhs`, returning [`None`] on `i64` overflow.
    pub fn checked_sub(
        self,
        rhs: Duration,
    ) -> Option<DateTime> {
        self.secs.checked_sub(rhs.secs).map(DateTime::new)
    }
}

/// `DateTime - DateTime` yields the [`Duration`] between the two instants.
impl Sub<DateTime> for DateTime {
    type Output = Duration;
    fn sub(
        self,
        rhs: DateTime,
    ) -> Duration {
        Duration::new(self.secs - rhs.secs)
    }
}

/// `DateTime + Duration` shifts the instant forward.
impl Add<Duration> for DateTime {
    type Output = DateTime;
    fn add(
        self,
        rhs: Duration,
    ) -> DateTime {
        DateTime::new(self.secs + rhs.secs)
    }
}

/// `DateTime - Duration` shifts the instant backward.
impl Sub<Duration> for DateTime {
    type Output = DateTime;
    fn sub(
        self,
        rhs: Duration,
    ) -> DateTime {
        DateTime::new(self.secs - rhs.secs)
    }
}

/// `Duration + DateTime` shifts the instant forward (commutative with `DateTime + Duration`).
impl Add<DateTime> for Duration {
    type Output = DateTime;
    fn add(
        self,
        rhs: DateTime,
    ) -> DateTime {
        DateTime::new(self.secs + rhs.secs)
    }
}

impl Duration {
    /// Returns this duration as a [`std::time::Duration`], or [`None`] if it is negative — a
    /// [`std::time::Duration`] cannot represent a negative span.
    pub fn as_std_duration(self) -> Option<std::time::Duration> {
        u64::try_from(self.secs)
            .ok()
            .map(std::time::Duration::from_secs)
    }

    /// Adds another [`Duration`], returning [`None`] on `i64` overflow.
    pub fn checked_add(
        self,
        rhs: Duration,
    ) -> Option<Duration> {
        self.secs.checked_add(rhs.secs).map(Duration::new)
    }

    /// Subtracts another [`Duration`], returning [`None`] on `i64` overflow.
    pub fn checked_sub(
        self,
        rhs: Duration,
    ) -> Option<Duration> {
        self.secs.checked_sub(rhs.secs).map(Duration::new)
    }

    /// Negates this duration, returning [`None`] on `i64` overflow (i.e. `i64::MIN`).
    pub fn checked_neg(self) -> Option<Duration> {
        self.secs.checked_neg().map(Duration::new)
    }
}

impl Add for Duration {
    type Output = Duration;
    fn add(
        self,
        rhs: Duration,
    ) -> Duration {
        Duration::new(self.secs + rhs.secs)
    }
}

impl Sub for Duration {
    type Output = Duration;
    fn sub(
        self,
        rhs: Duration,
    ) -> Duration {
        Duration::new(self.secs - rhs.secs)
    }
}

impl Neg for Duration {
    type Output = Duration;
    fn neg(self) -> Duration {
        Duration::new(-self.secs)
    }
}

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

    #[test]
    fn test_new_seconds_and_display() {
        let dt = DateTime::new(1_700_000_000);
        assert_eq!(dt.seconds(), Seconds::new(1_700_000_000));
        assert_eq!(dt.to_string(), "1700000000");

        let date = Date::new(-697_161_600);
        assert_eq!(date.seconds().get(), -697_161_600);
        assert_eq!(date.to_string(), "-697161600");

        let time = Time::new(3600);
        assert_eq!(time.seconds(), Seconds::new(3600));
        assert_eq!(time.to_string(), "3600");

        let dur = Duration::new(259_200);
        assert_eq!(dur.seconds(), Seconds::new(259_200));
        assert_eq!(dur.to_string(), "259200");
    }

    #[test]
    fn test_parse_reads_int() {
        assert_eq!(
            DateTime::parse(redis::Value::Int(42)).unwrap(),
            DateTime::new(42)
        );
        assert_eq!(Date::parse(redis::Value::Int(-1)).unwrap(), Date::new(-1));
        assert_eq!(Time::parse(redis::Value::Int(7)).unwrap(), Time::new(7));
        assert_eq!(
            Duration::parse(redis::Value::Int(259_200)).unwrap(),
            Duration::new(259_200)
        );
    }

    #[test]
    fn test_parse_rejects_non_int() {
        let err = Duration::parse(redis::Value::SimpleString("nope".to_string())).unwrap_err();
        assert_eq!(err, FalkorDBError::ParsingI64);
    }

    #[test]
    fn test_ordering_and_default() {
        assert_eq!(Date::default(), Date::new(0));
        assert!(Time::new(1) < Time::new(2));
    }

    #[test]
    fn test_seconds_scalar() {
        let s = Seconds::new(10);
        assert_eq!(s.get(), 10);
        assert_eq!(s.to_string(), "10");
        assert_eq!(Seconds::from(5), Seconds::new(5));
        assert_eq!(i64::from(Seconds::new(8)), 8);
        assert_eq!(Seconds::default(), Seconds::new(0));

        // Operators.
        assert_eq!(Seconds::new(3) + Seconds::new(4), Seconds::new(7));
        assert_eq!(Seconds::new(7) - Seconds::new(4), Seconds::new(3));
        assert_eq!(-Seconds::new(3), Seconds::new(-3));

        // Checked arithmetic.
        assert_eq!(
            Seconds::new(10).checked_add(Seconds::new(5)),
            Some(Seconds::new(15))
        );
        assert_eq!(Seconds::new(i64::MAX).checked_add(Seconds::new(1)), None);
        assert_eq!(
            Seconds::new(10).checked_sub(Seconds::new(4)),
            Some(Seconds::new(6))
        );
        assert_eq!(Seconds::new(i64::MIN).checked_sub(Seconds::new(1)), None);
        assert_eq!(Seconds::new(3).checked_neg(), Some(Seconds::new(-3)));
        assert_eq!(Seconds::new(i64::MIN).checked_neg(), None);
    }

    #[test]
    fn test_datetime_duration_algebra() {
        // instant - instant = span
        assert_eq!(DateTime::new(100) - DateTime::new(40), Duration::new(60));
        // instant ± span = instant
        assert_eq!(DateTime::new(100) + Duration::new(5), DateTime::new(105));
        assert_eq!(DateTime::new(100) - Duration::new(5), DateTime::new(95));
        // span + instant = instant (commutative)
        assert_eq!(Duration::new(5) + DateTime::new(100), DateTime::new(105));
        // span arithmetic
        assert_eq!(Duration::new(3) + Duration::new(4), Duration::new(7));
        assert_eq!(Duration::new(7) - Duration::new(4), Duration::new(3));
        assert_eq!(-Duration::new(3), Duration::new(-3));
    }

    #[test]
    fn test_datetime_checked_arithmetic() {
        assert_eq!(
            DateTime::new(10).checked_duration_since(DateTime::new(4)),
            Some(Duration::new(6))
        );
        assert_eq!(
            DateTime::new(i64::MIN).checked_duration_since(DateTime::new(1)),
            None
        );
        assert_eq!(
            DateTime::new(10).checked_add(Duration::new(5)),
            Some(DateTime::new(15))
        );
        assert_eq!(DateTime::new(i64::MAX).checked_add(Duration::new(1)), None);
        assert_eq!(
            DateTime::new(10).checked_sub(Duration::new(3)),
            Some(DateTime::new(7))
        );
        assert_eq!(DateTime::new(i64::MIN).checked_sub(Duration::new(1)), None);
    }

    #[test]
    fn test_duration_helpers_and_checked() {
        let three_days = Duration::new(259_200);
        assert_eq!(three_days.seconds().get(), 259_200);
        assert_eq!(
            three_days.as_std_duration(),
            Some(std::time::Duration::from_secs(259_200))
        );
        // A negative span has no std::time::Duration representation.
        assert_eq!(Duration::new(-1).as_std_duration(), None);

        assert_eq!(
            Duration::new(3).checked_add(Duration::new(4)),
            Some(Duration::new(7))
        );
        assert_eq!(Duration::new(i64::MAX).checked_add(Duration::new(1)), None);
        assert_eq!(
            Duration::new(7).checked_sub(Duration::new(4)),
            Some(Duration::new(3))
        );
        assert_eq!(Duration::new(i64::MIN).checked_sub(Duration::new(1)), None);
        assert_eq!(Duration::new(3).checked_neg(), Some(Duration::new(-3)));
        assert_eq!(Duration::new(i64::MIN).checked_neg(), None);
    }
}