falkordb 0.10.3

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
/*
 * Copyright FalkorDB Ltd. 2023 - present
 * Licensed under the MIT License.
 */

use crate::{FalkorDBError, FalkorResult};
use graph_entities::{Edge, Node};
use path::Path;
use point::Point;
use std::{collections::HashMap, fmt::Debug};
use temporal::{Date, DateTime, Duration, Time};
use vec32::Vec32;

pub(crate) mod config;
pub(crate) mod from_value;
pub(crate) mod graph_entities;
pub(crate) mod param;
pub(crate) mod path;
pub(crate) mod point;
pub(crate) mod temporal;
pub(crate) mod vec32;

#[cfg(feature = "serde")]
mod de;

#[cfg(all(test, feature = "serde"))]
mod de_proptest;

#[cfg(test)]
mod param_proptest;

pub use param::{to_cypher_param, FalkorParams, IntoFalkorParam, IntoFalkorParams, RawParam};

pub use from_value::FromFalkorValue;

#[cfg(feature = "serde")]
pub use de::{from_falkor_row, from_falkor_value, FalkorValueDeserializer};

/// An enum of all the supported Falkor types
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum FalkorValue {
    /// See [`Node`]
    Node(Node),
    /// See [`Edge`]
    Edge(Edge),
    /// A [`Vec`] of other [`FalkorValue`]
    Array(Vec<FalkorValue>),
    /// A [`HashMap`] of [`String`] as keys, and other [`FalkorValue`] as values
    Map(HashMap<String, FalkorValue>),
    /// A vector of float values used for vector search see [`Vec32`]
    Vec32(Vec32),
    /// Plain old string
    String(String),
    /// A boolean value
    Bool(bool),
    /// An [`i64`] value, Falkor only supports signed integers
    I64(i64),
    /// An [`f64`] value, Falkor only supports double precisions when not in Vectors
    F64(f64),
    /// See [`Point`]
    Point(Point),
    /// See [`Path`]
    Path(Path),
    /// A FalkorDB `datetime` value, see [`DateTime`]
    DateTime(DateTime),
    /// A FalkorDB `date` value, see [`Date`]
    Date(Date),
    /// A FalkorDB `time`/`localtime` value, see [`Time`]
    Time(Time),
    /// A FalkorDB `duration` value, see [`Duration`]
    Duration(Duration),
    /// A NULL type
    None,
    /// Failed parsing this value
    Unparseable(String),
}

macro_rules! impl_to_falkordb_value {
    ($t:ty, $falkordbtype:expr) => {
        impl From<$t> for FalkorValue {
            fn from(value: $t) -> Self {
                $falkordbtype(value as _)
            }
        }
    };
}

impl_to_falkordb_value!(i8, Self::I64);
impl_to_falkordb_value!(i32, Self::I64);
impl_to_falkordb_value!(i64, Self::I64);

impl_to_falkordb_value!(u8, Self::I64);
impl_to_falkordb_value!(u32, Self::I64);
impl_to_falkordb_value!(u64, Self::I64);

impl_to_falkordb_value!(f32, Self::F64);
impl_to_falkordb_value!(f64, Self::F64);

impl_to_falkordb_value!(String, Self::String);

impl From<&str> for FalkorValue {
    fn from(value: &str) -> Self {
        Self::String(value.to_string())
    }
}

impl FalkorValue {
    /// Returns a reference to the internal [`Vec`] if this is an Array variant.
    ///
    /// # Returns
    /// A reference to the internal [`Vec`]
    pub fn as_vec(&self) -> Option<&Vec<Self>> {
        match self {
            FalkorValue::Array(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a reference to the internal [`String`] if this is an String variant.
    ///
    /// # Returns
    /// A reference to the internal [`String`]
    pub fn as_string(&self) -> Option<&String> {
        match self {
            FalkorValue::String(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a reference to the internal [`Edge`] if this is an FEdge variant.
    ///
    /// # Returns
    /// A reference to the internal [`Edge`]
    pub fn as_edge(&self) -> Option<&Edge> {
        match self {
            FalkorValue::Edge(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a reference to the internal [`Node`] if this is an FNode variant.
    ///
    /// # Returns
    /// A reference to the internal [`Node`]
    pub fn as_node(&self) -> Option<&Node> {
        match self {
            FalkorValue::Node(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a reference to the internal [`Path`] if this is an Path variant.
    ///
    /// # Returns
    /// A reference to the internal [`Path`]
    pub fn as_path(&self) -> Option<&Path> {
        match self {
            FalkorValue::Path(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a reference to the internal [`HashMap`] if this is an Map variant.
    ///
    /// # Returns
    /// A reference to the internal [`HashMap`]
    pub fn as_map(&self) -> Option<&HashMap<String, FalkorValue>> {
        match self {
            FalkorValue::Map(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a reference to the internal [`Point`] if this is an FPoint variant.
    ///
    /// # Returns
    /// A reference to the internal [`Point`]
    pub fn as_point(&self) -> Option<&Point> {
        match self {
            FalkorValue::Point(val) => Some(val),
            _ => None,
        }
    }

    /// Returns a copy of the inner [`DateTime`] if this is a `DateTime` variant.
    ///
    /// # Returns
    /// A copy of the inner [`DateTime`]
    pub fn as_datetime(&self) -> Option<DateTime> {
        match self {
            FalkorValue::DateTime(val) => Some(*val),
            _ => None,
        }
    }

    /// Returns a copy of the inner [`Date`] if this is a `Date` variant.
    ///
    /// # Returns
    /// A copy of the inner [`Date`]
    pub fn as_date(&self) -> Option<Date> {
        match self {
            FalkorValue::Date(val) => Some(*val),
            _ => None,
        }
    }

    /// Returns a copy of the inner [`Time`] if this is a `Time` variant.
    ///
    /// # Returns
    /// A copy of the inner [`Time`]
    pub fn as_time(&self) -> Option<Time> {
        match self {
            FalkorValue::Time(val) => Some(*val),
            _ => None,
        }
    }

    /// Returns a copy of the inner [`Duration`] if this is a `Duration` variant.
    ///
    /// # Returns
    /// A copy of the inner [`Duration`]
    pub fn as_duration(&self) -> Option<Duration> {
        match self {
            FalkorValue::Duration(val) => Some(*val),
            _ => None,
        }
    }

    /// Returns a Copy of the inner [`i64`] if this is an Int64 variant
    ///
    /// # Returns
    /// A copy of the inner [`i64`]
    pub fn to_i64(&self) -> Option<i64> {
        match self {
            FalkorValue::I64(val) => Some(*val),
            _ => None,
        }
    }

    /// Returns a Copy of the inner [`bool`] if this is an FBool variant
    ///
    /// # Returns
    /// A copy of the inner [`bool`]
    pub fn to_bool(&self) -> Option<bool> {
        match self {
            FalkorValue::Bool(val) => Some(*val),
            FalkorValue::String(bool_str) => match bool_str.as_str() {
                "true" => Some(true),
                "false" => Some(false),
                _ => None,
            },
            _ => None,
        }
    }

    /// Returns a Copy of the inner [`f64`] if this is an F64 variant
    ///
    /// # Returns
    /// A copy of the inner [`f64`]
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            FalkorValue::F64(val) => Some(*val),
            _ => None,
        }
    }

    /// Consumes itself and returns the inner [`Vec`] if this is an Array variant
    ///
    /// # Returns
    /// The inner [`Vec`]
    pub fn into_vec(self) -> FalkorResult<Vec<Self>> {
        match self {
            FalkorValue::Array(array) => Ok(array),
            _ => Err(FalkorDBError::ParsingArray),
        }
    }

    /// Consumes itself and returns the inner [`String`] if this is an String variant
    ///
    /// # Returns
    /// The inner [`String`]
    pub fn into_string(self) -> FalkorResult<String> {
        match self {
            FalkorValue::String(string) => Ok(string),
            _ => Err(FalkorDBError::ParsingString),
        }
    }
    /// Consumes itself and returns the inner [`HashMap`] if this is a Map variant
    ///
    /// # Returns
    /// The inner [`HashMap`]
    pub fn into_map(self) -> FalkorResult<HashMap<String, FalkorValue>> {
        match self {
            FalkorValue::Map(map) => Ok(map),
            _ => Err(FalkorDBError::ParsingMap),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{collections::HashMap, f64::consts::PI};

    #[test]
    fn test_as_vec() {
        let vec_val = FalkorValue::Array(vec![FalkorValue::I64(1), FalkorValue::I64(2)]);
        assert_eq!(vec_val.as_vec().unwrap().len(), 2);

        let non_vec_val = FalkorValue::I64(42);
        assert!(non_vec_val.as_vec().is_none());
    }

    #[test]
    fn test_as_string() {
        let string_val = FalkorValue::String(String::from("hello"));
        assert_eq!(string_val.as_string().unwrap(), "hello");

        let non_string_val = FalkorValue::I64(42);
        assert!(non_string_val.as_string().is_none());
    }

    #[test]
    fn test_as_edge() {
        let edge = Edge::default(); // Assuming Edge::new() is a valid constructor
        let edge_val = FalkorValue::Edge(edge);
        assert!(edge_val.as_edge().is_some());

        let non_edge_val = FalkorValue::I64(42);
        assert!(non_edge_val.as_edge().is_none());
    }

    #[test]
    fn test_as_node() {
        let node = Node::default(); // Assuming Node::new() is a valid constructor
        let node_val = FalkorValue::Node(node);
        assert!(node_val.as_node().is_some());

        let non_node_val = FalkorValue::I64(42);
        assert!(non_node_val.as_node().is_none());
    }

    #[test]
    fn test_as_path() {
        let path = Path::default(); // Assuming Path::new() is a valid constructor
        let path_val = FalkorValue::Path(path);
        assert!(path_val.as_path().is_some());

        let non_path_val = FalkorValue::I64(42);
        assert!(non_path_val.as_path().is_none());
    }

    #[test]
    fn test_as_map() {
        let mut map = HashMap::new();
        map.insert(String::from("key"), FalkorValue::I64(42));
        let map_val = FalkorValue::Map(map);
        assert!(map_val.as_map().is_some());

        let non_map_val = FalkorValue::I64(42);
        assert!(non_map_val.as_map().is_none());
    }

    #[test]
    fn test_as_point() {
        let point = Point::default(); // Assuming Point::new() is a valid constructor
        let point_val = FalkorValue::Point(point);
        assert!(point_val.as_point().is_some());

        let non_point_val = FalkorValue::I64(42);
        assert!(non_point_val.as_point().is_none());
    }

    #[test]
    fn test_temporal_accessors() {
        let datetime = FalkorValue::DateTime(DateTime::new(1_700_000_000));
        assert_eq!(datetime.as_datetime(), Some(DateTime::new(1_700_000_000)));
        assert!(datetime.as_date().is_none());

        let date = FalkorValue::Date(Date::new(-697_161_600));
        assert_eq!(date.as_date(), Some(Date::new(-697_161_600)));
        assert!(date.as_time().is_none());

        let time = FalkorValue::Time(Time::new(3600));
        assert_eq!(time.as_time(), Some(Time::new(3600)));
        assert!(time.as_duration().is_none());

        let duration = FalkorValue::Duration(Duration::new(259_200));
        assert_eq!(duration.as_duration(), Some(Duration::new(259_200)));
        assert!(duration.as_datetime().is_none());
    }

    #[test]
    fn test_to_i64() {
        let int_val = FalkorValue::I64(42);
        assert_eq!(int_val.to_i64().unwrap(), 42);

        let non_int_val = FalkorValue::String(String::from("hello"));
        assert!(non_int_val.to_i64().is_none());
    }

    #[test]
    fn test_to_bool() {
        let bool_val = FalkorValue::Bool(true);
        assert!(bool_val.to_bool().unwrap());

        let bool_str_val = FalkorValue::String(String::from("false"));
        assert!(!bool_str_val.to_bool().unwrap());

        let invalid_bool_str_val = FalkorValue::String(String::from("notabool"));
        assert!(invalid_bool_str_val.to_bool().is_none());

        let non_bool_val = FalkorValue::I64(42);
        assert!(non_bool_val.to_bool().is_none());
    }

    #[test]
    fn test_to_f64() {
        let float_val = FalkorValue::F64(PI);
        assert_eq!(float_val.to_f64().unwrap(), PI);

        let non_float_val = FalkorValue::String(String::from("hello"));
        assert!(non_float_val.to_f64().is_none());
    }

    #[test]
    fn test_into_vec() {
        let vec_val = FalkorValue::Array(vec![FalkorValue::I64(1), FalkorValue::I64(2)]);
        assert_eq!(vec_val.into_vec().unwrap().len(), 2);

        let non_vec_val = FalkorValue::I64(42);
        assert!(non_vec_val.into_vec().is_err());
    }

    #[test]
    fn test_into_string() {
        let string_val = FalkorValue::String(String::from("hello"));
        assert_eq!(string_val.into_string().unwrap(), "hello");

        let non_string_val = FalkorValue::I64(42);
        assert!(non_string_val.into_string().is_err());
    }
}