rsmgclient 3.0.0

Memgraph database adapter for Rust programming language.
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
// Copyright (c) 2016-2022 Memgraph Ltd. [https://memgraph.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::bindings;
use chrono::{
    Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, Utc,
};
use chrono_tz::Tz;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::ffi::{CStr, CString};
use std::fmt;
use std::fmt::Formatter;
use std::num::TryFromIntError;
use std::os::raw::c_char;
use std::slice;

/// Representation of Point2D spatial data type.
#[derive(Debug, PartialEq, Clone)]
pub struct Point2D {
    pub srid: u16,
    pub x_longitude: f64,
    pub y_latitude: f64,
}

impl fmt::Display for Point2D {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Point2D({{ srid:{}, x:{}, y:{} }})",
            self.srid, self.x_longitude, self.y_latitude
        )
    }
}

/// Representation of Point3D spatial data type.
#[derive(Debug, PartialEq, Clone)]
pub struct Point3D {
    pub srid: u16,
    pub x_longitude: f64,
    pub y_latitude: f64,
    pub z_height: f64,
}

impl fmt::Display for Point3D {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Point3D({{ srid:{}, x:{}, y:{}, z:{} }})",
            self.srid, self.x_longitude, self.y_latitude, self.z_height
        )
    }
}

/// Representation of parameter value used in query.
pub enum QueryParam {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    Date(NaiveDate),
    LocalTime(NaiveTime),
    LocalDateTime(NaiveDateTime),
    Duration(Duration),
    Point2D(Point2D),
    Point3D(Point3D),
    List(Vec<QueryParam>),
    Map(HashMap<String, QueryParam>),
}

impl QueryParam {
    fn to_c_mg_value(&self) -> *mut bindings::mg_value {
        unsafe {
            match self {
                QueryParam::Null => bindings::mg_value_make_null(),
                QueryParam::Bool(x) => {
                    let ptr = bindings::mg_value_make_bool(match *x {
                        false => 0,
                        true => 1,
                    });
                    if ptr.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Int(x) => {
                    let ptr = bindings::mg_value_make_integer(*x);
                    if ptr.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Float(x) => {
                    let ptr = bindings::mg_value_make_float(*x);
                    if ptr.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::String(x) => {
                    // String parameter may contain null bytes - return null on error
                    let c_string = match CString::new(x.as_str()) {
                        Ok(s) => s,
                        Err(_) => return bindings::mg_value_make_null(),
                    };
                    let ptr = bindings::mg_value_make_string(c_string.as_ptr());
                    if ptr.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Date(x) => {
                    let mg_date = naive_date_to_mg_date(x);
                    if mg_date.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_date(mg_date);
                    if ptr.is_null() {
                        bindings::mg_date_destroy(mg_date);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::LocalTime(x) => {
                    let mg_local_time = naive_local_time_to_mg_local_time(x);
                    if mg_local_time.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_local_time(mg_local_time);
                    if ptr.is_null() {
                        bindings::mg_local_time_destroy(mg_local_time);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::LocalDateTime(x) => {
                    let mg_local_date_time = naive_local_date_time_to_mg_local_date_time(x);
                    if mg_local_date_time.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_local_date_time(mg_local_date_time);
                    if ptr.is_null() {
                        bindings::mg_local_date_time_destroy(mg_local_date_time);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Duration(x) => {
                    let mg_duration = duration_to_mg_duration(x);
                    if mg_duration.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_duration(mg_duration);
                    if ptr.is_null() {
                        bindings::mg_duration_destroy(mg_duration);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Point2D(x) => {
                    let mg_point_2d = point2d_to_mg_point_2d(x);
                    if mg_point_2d.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_point_2d(mg_point_2d);
                    if ptr.is_null() {
                        bindings::mg_point_2d_destroy(mg_point_2d);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Point3D(x) => {
                    let mg_point_3d = point3d_to_mg_point_3d(x);
                    if mg_point_3d.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_point_3d(mg_point_3d);
                    if ptr.is_null() {
                        bindings::mg_point_3d_destroy(mg_point_3d);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::List(x) => {
                    let mg_list = vector_to_mg_list(x);
                    if mg_list.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_list(mg_list);
                    if ptr.is_null() {
                        bindings::mg_list_destroy(mg_list);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
                QueryParam::Map(x) => {
                    let mg_map = hash_map_to_mg_map(x);
                    if mg_map.is_null() {
                        return bindings::mg_value_make_null();
                    }
                    let ptr = bindings::mg_value_make_map(mg_map);
                    if ptr.is_null() {
                        bindings::mg_map_destroy(mg_map);
                        return bindings::mg_value_make_null();
                    }
                    ptr
                }
            }
        }
    }
}

/// Representation of a DateTime value with timezone support.
///
/// Contains date, time, and timezone information including timezone ID and offset.
#[derive(Debug, PartialEq, Clone)]
pub struct DateTime {
    pub year: i32,
    pub month: u32,
    pub day: u32,
    pub hour: u32,
    pub minute: u32,
    pub second: u32,
    pub nanosecond: u32,
    pub time_zone_offset_seconds: i32,
    pub time_zone_id: Option<String>,
}

/// Representation of node value from a labeled property graph.
///
/// Consists of a unique identifier(within the scope of its origin graph), a list
/// of labels and a map of properties.
///
/// Maximum possible number of labels allowed by Bolt protocol is UINT32_MAX
#[derive(Debug, PartialEq, Clone)]
pub struct Node {
    pub id: i64,
    pub label_count: u32,
    pub labels: Vec<String>,
    pub properties: HashMap<String, Value>,
}

/// Representation of relationship value from a labeled property graph.
///
/// Consists of a unique identifier(within the scope of its origin graph),
/// identifiers for the start and end nodes of that relationship, a type and
/// a map of properties.
#[derive(Debug, PartialEq, Clone)]
pub struct Relationship {
    pub id: i64,
    pub start_id: i64,
    pub end_id: i64,
    pub type_: String,
    pub properties: HashMap<String, Value>,
}

/// Representation of relationship from a labeled property graph.
///
/// Relationship without start and end nodes. Mainly used as a supporting type
/// for Path.
#[derive(Debug, PartialEq, Clone)]
pub struct UnboundRelationship {
    pub id: i64,
    pub type_: String,
    pub properties: HashMap<String, Value>,
}

/// Representation of sequence of alternating nodes and relationships corresponding
/// to a walk in a labeled property graph.
///
/// Path always begins and ends with a node. It consists of a list of distinct
/// nodes, a list of distinct relationships and a sequence of integers
/// describing the path traversal.
#[derive(Debug, PartialEq, Clone)]
pub struct Path {
    pub node_count: u32,
    pub relationship_count: u32,
    pub nodes: Vec<Node>,
    pub relationships: Vec<UnboundRelationship>,
}

/// Representation of Bolt value returned by database.
///
/// Value is can be any of the types specified by Bolt protocol.
#[derive(Debug, PartialEq, Clone)]
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    List(Vec<Value>),
    Date(NaiveDate),
    LocalTime(NaiveTime),
    LocalDateTime(NaiveDateTime),
    DateTime(DateTime),
    Duration(Duration),
    Point2D(Point2D),
    Point3D(Point3D),
    Map(HashMap<String, Value>),
    Node(Node),
    Relationship(Relationship),
    UnboundRelationship(UnboundRelationship),
    Path(Path),
}

/// Representation of a single row returned by database.
pub struct Record {
    pub values: Vec<Value>,
}

fn mg_value_list_to_vec(mg_value: *const bindings::mg_value) -> Vec<Value> {
    unsafe {
        let mg_list = bindings::mg_value_list(mg_value);
        mg_list_to_vec(mg_list)
    }
}

fn mg_value_bool(mg_value: *const bindings::mg_value) -> bool {
    !matches!(unsafe { bindings::mg_value_bool(mg_value) }, 0)
}

fn mg_value_int(mg_value: *const bindings::mg_value) -> i64 {
    unsafe { bindings::mg_value_integer(mg_value) }
}

fn mg_value_float(mg_value: *const bindings::mg_value) -> f64 {
    unsafe { bindings::mg_value_float(mg_value) }
}

pub(crate) unsafe fn c_string_to_string(c_str: *const c_char, size: Option<u32>) -> String {
    // https://github.com/rust-lang/rust/blob/master/library/std/src/ffi/c_str.rs#L1230
    let c_str = match size {
        Some(x) => unsafe {
            CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(
                c_str as *const u8,
                (x + 1) as usize,
            ))
        },
        None => unsafe { CStr::from_ptr(c_str) },
    };
    // Convert to string, using lossy conversion if UTF-8 is invalid
    c_str.to_str().unwrap_or("").to_string()
}

fn mg_string_to_string(mg_string: *const bindings::mg_string) -> String {
    let c_str = unsafe { bindings::mg_string_data(mg_string) };
    unsafe { c_string_to_string(c_str, Some(bindings::mg_string_size(mg_string))) }
}

pub(crate) fn mg_value_string(mg_value: *const bindings::mg_value) -> String {
    let c_str = unsafe { bindings::mg_value_string(mg_value) };
    mg_string_to_string(c_str)
}

fn days_as_seconds(days: i64) -> i64 {
    hours_as_seconds(days * 24)
}

fn hours_as_seconds(hours: i64) -> i64 {
    minutes_as_seconds(hours * 60)
}

fn minutes_as_seconds(minutes: i64) -> i64 {
    minutes * 60
}

const NSEC_IN_SEC: i64 = 1_000_000_000;

pub(crate) fn mg_value_naive_date(mg_value: *const bindings::mg_value) -> Result<NaiveDate, ()> {
    let c_date = unsafe { bindings::mg_value_date(mg_value) };
    let c_delta_days = unsafe { bindings::mg_date_days(c_date) };
    // Unix epoch date is a known valid date
    let epoch_date = NaiveDate::from_ymd_opt(1970, 1, 1).expect("Unix epoch is a valid date");
    let delta_days = Duration::days(c_delta_days);
    epoch_date.checked_add_signed(delta_days).ok_or(())
}

pub(crate) fn mg_value_naive_local_time(
    mg_value: *const bindings::mg_value,
) -> Result<NaiveTime, TryFromIntError> {
    let c_local_time = unsafe { bindings::mg_value_local_time(mg_value) };
    let c_nanoseconds = unsafe { bindings::mg_local_time_nanoseconds(c_local_time) };
    let seconds = u32::try_from(c_nanoseconds / NSEC_IN_SEC)?;
    let nanoseconds = u32::try_from(c_nanoseconds % NSEC_IN_SEC)?;
    NaiveTime::from_num_seconds_from_midnight_opt(seconds, nanoseconds)
        .ok_or_else(|| u32::try_from(-1).unwrap_err())
}

pub(crate) fn mg_value_naive_local_date_time(
    mg_value: *const bindings::mg_value,
) -> Result<NaiveDateTime, TryFromIntError> {
    let c_local_date_time = unsafe { bindings::mg_value_local_date_time(mg_value) };
    let c_seconds = unsafe { bindings::mg_local_date_time_seconds(c_local_date_time) };
    let c_nanoseconds = unsafe { bindings::mg_local_date_time_nanoseconds(c_local_date_time) };
    let nanoseconds = u32::try_from(c_nanoseconds)?;
    Utc.timestamp_opt(c_seconds, nanoseconds)
        .single()
        .map(|dt| dt.naive_utc())
        .ok_or_else(|| u32::try_from(-1).unwrap_err())
}

fn mg_value_datetime_zone_id(
    c_datetime_zone_id: *const bindings::mg_date_time_zone_id,
) -> Result<DateTime, crate::error::MgError> {
    let c_seconds = unsafe { bindings::mg_date_time_zone_id_seconds(c_datetime_zone_id) };
    let c_nanoseconds = unsafe { bindings::mg_date_time_zone_id_nanoseconds(c_datetime_zone_id) };
    let c_timezone_name_ptr =
        unsafe { bindings::mg_date_time_zone_id_timezone_name(c_datetime_zone_id) };

    // Create NaiveDateTime from timestamp
    let naive_datetime = match Utc.timestamp_opt(c_seconds, c_nanoseconds as u32) {
        chrono::LocalResult::Single(dt) => dt.naive_utc(),
        _ => {
            return Err(crate::error::MgError::InvalidTimestamp);
        }
    };

    // Extract timezone name from mg_string
    let timezone_name = if c_timezone_name_ptr.is_null() {
        "UTC".to_string()
    } else {
        mg_string_to_string(c_timezone_name_ptr)
    };

    // Compute timezone offset from timezone name and timestamp
    let time_zone_offset_seconds = timezone_name
        .parse::<Tz>()
        .ok()
        .and_then(|tz| {
            // Convert UTC timestamp to the specified timezone
            let utc_datetime = Utc
                .timestamp_opt(c_seconds, c_nanoseconds as u32)
                .single()?;
            let zoned_datetime = utc_datetime.with_timezone(&tz);
            Some(zoned_datetime.offset().fix().local_minus_utc())
        })
        .unwrap_or(0); // Default to 0 if timezone parsing fails

    // Extract individual date/time fields
    let date = naive_datetime.date();
    let time = naive_datetime.time();

    Ok(DateTime {
        year: date.year(),
        month: date.month(),
        day: date.day(),
        hour: time.hour(),
        minute: time.minute(),
        second: time.second(),
        nanosecond: time.nanosecond(),
        time_zone_offset_seconds,
        time_zone_id: Some(timezone_name),
    })
}

pub(crate) fn mg_value_duration(mg_value: *const bindings::mg_value) -> Duration {
    let c_duration = unsafe { bindings::mg_value_duration(mg_value) };
    let days = unsafe { bindings::mg_duration_days(c_duration) };
    let seconds = unsafe { bindings::mg_duration_seconds(c_duration) };
    let nanoseconds = unsafe { bindings::mg_duration_nanoseconds(c_duration) };
    Duration::days(days) + Duration::seconds(seconds) + Duration::nanoseconds(nanoseconds)
}

pub(crate) fn mg_value_point2d(mg_value: *const bindings::mg_value) -> Point2D {
    let c_point2d = unsafe { bindings::mg_value_point_2d(mg_value) };
    let srid = unsafe { bindings::mg_point_2d_srid(c_point2d) } as u16;
    let x_longitude = unsafe { bindings::mg_point_2d_x(c_point2d) };
    let y_latitude = unsafe { bindings::mg_point_2d_y(c_point2d) };
    Point2D {
        srid,
        x_longitude,
        y_latitude,
    }
}

pub(crate) fn mg_value_point3d(mg_value: *const bindings::mg_value) -> Point3D {
    let c_point3d = unsafe { bindings::mg_value_point_3d(mg_value) };
    let srid = unsafe { bindings::mg_point_3d_srid(c_point3d) } as u16;
    let x_longitude = unsafe { bindings::mg_point_3d_x(c_point3d) };
    let y_latitude = unsafe { bindings::mg_point_3d_y(c_point3d) };
    let z_height = unsafe { bindings::mg_point_3d_z(c_point3d) };
    Point3D {
        srid,
        x_longitude,
        y_latitude,
        z_height,
    }
}

pub(crate) fn mg_map_to_hash_map(mg_map: *const bindings::mg_map) -> HashMap<String, Value> {
    unsafe {
        let size = bindings::mg_map_size(mg_map);
        let mut hash_map: HashMap<String, Value> = HashMap::new();
        for i in 0..size {
            let mg_string = bindings::mg_map_key_at(mg_map, i);
            let key = mg_string_to_string(mg_string);
            let map_value = bindings::mg_map_value_at(mg_map, i);
            hash_map.insert(key, Value::from_mg_value(map_value));
        }

        hash_map
    }
}

fn mg_value_map(mg_value: *const bindings::mg_value) -> HashMap<String, Value> {
    unsafe {
        let mg_map = bindings::mg_value_map(mg_value);
        mg_map_to_hash_map(mg_map)
    }
}

fn c_mg_node_to_mg_node(c_mg_node: *const bindings::mg_node) -> Node {
    let id = unsafe { bindings::mg_node_id(c_mg_node) };
    let label_count = unsafe { bindings::mg_node_label_count(c_mg_node) };
    let mut labels: Vec<String> = Vec::new();
    for i in 0..label_count {
        let label = unsafe { bindings::mg_node_label_at(c_mg_node, i) };
        labels.push(mg_string_to_string(label));
    }

    let properties_map = unsafe { bindings::mg_node_properties(c_mg_node) };
    let properties: HashMap<String, Value> = mg_map_to_hash_map(properties_map);

    Node {
        id,
        label_count,
        labels,
        properties,
    }
}

fn mg_value_node(mg_value: *const bindings::mg_value) -> Node {
    let c_mg_node = unsafe { bindings::mg_value_node(mg_value) };
    c_mg_node_to_mg_node(c_mg_node)
}

fn mg_value_relationship(mg_value: *const bindings::mg_value) -> Relationship {
    let c_mg_relationship = unsafe { bindings::mg_value_relationship(mg_value) };

    let id = unsafe { bindings::mg_relationship_id(c_mg_relationship) };
    let start_id = unsafe { bindings::mg_relationship_start_id(c_mg_relationship) };
    let end_id = unsafe { bindings::mg_relationship_end_id(c_mg_relationship) };
    let type_mg_string = unsafe { bindings::mg_relationship_type(c_mg_relationship) };
    let type_ = mg_string_to_string(type_mg_string);
    let properties_mg_map = unsafe { bindings::mg_relationship_properties(c_mg_relationship) };
    let properties = mg_map_to_hash_map(properties_mg_map);

    Relationship {
        id,
        start_id,
        end_id,
        type_,
        properties,
    }
}

fn c_mg_unbound_relationship_to_mg_unbound_relationship(
    c_mg_unbound_relationship: *const bindings::mg_unbound_relationship,
) -> UnboundRelationship {
    let id = unsafe { bindings::mg_unbound_relationship_id(c_mg_unbound_relationship) };
    let type_mg_string =
        unsafe { bindings::mg_unbound_relationship_type(c_mg_unbound_relationship) };
    let type_ = mg_string_to_string(type_mg_string);
    let properties_mg_map =
        unsafe { bindings::mg_unbound_relationship_properties(c_mg_unbound_relationship) };
    let properties = mg_map_to_hash_map(properties_mg_map);

    UnboundRelationship {
        id,
        type_,
        properties,
    }
}

fn mg_value_unbound_relationship(mg_value: *const bindings::mg_value) -> UnboundRelationship {
    let c_mg_unbound_relationship = unsafe { bindings::mg_value_unbound_relationship(mg_value) };
    c_mg_unbound_relationship_to_mg_unbound_relationship(c_mg_unbound_relationship)
}

fn mg_value_path(mg_value: *const bindings::mg_value) -> Path {
    let c_mg_path = unsafe { bindings::mg_value_path(mg_value) };
    let mut node_count = 0;
    let mut relationship_count = 0;
    let mut nodes: Vec<Node> = Vec::new();
    let mut relationships: Vec<UnboundRelationship> = Vec::new();
    loop {
        let c_mg_node = unsafe { bindings::mg_path_node_at(c_mg_path, node_count) };
        if c_mg_node.is_null() {
            break;
        }
        node_count += 1;
        nodes.push(c_mg_node_to_mg_node(c_mg_node));
    }
    loop {
        let c_mg_unbound_relationship =
            unsafe { bindings::mg_path_relationship_at(c_mg_path, relationship_count) };
        if c_mg_unbound_relationship.is_null() {
            break;
        }
        relationship_count += 1;
        relationships.push(c_mg_unbound_relationship_to_mg_unbound_relationship(
            c_mg_unbound_relationship,
        ));
    }
    Path {
        node_count,
        relationship_count,
        nodes,
        relationships,
    }
}

pub(crate) unsafe fn mg_list_to_vec(mg_list: *const bindings::mg_list) -> Vec<Value> {
    let size = unsafe { bindings::mg_list_size(mg_list) };
    let mut mg_values: Vec<Value> = Vec::new();
    for i in 0..size {
        let mg_value = unsafe { bindings::mg_list_at(mg_list, i) };
        mg_values.push(unsafe { Value::from_mg_value(mg_value) });
    }

    mg_values
}

pub(crate) fn hash_map_to_mg_map(hash_map: &HashMap<String, QueryParam>) -> *mut bindings::mg_map {
    let size = hash_map.len() as u32;
    let mg_map = unsafe { bindings::mg_map_make_empty(size) };
    if mg_map.is_null() {
        return std::ptr::null_mut();
    }
    for (key, val) in hash_map {
        // Skip keys with null bytes - they cannot be converted to C strings
        if let Ok(c_key) = CString::new(key.as_str()) {
            let mg_value = val.to_c_mg_value();
            if mg_value.is_null() {
                // If value allocation fails, destroy the map and return null
                unsafe { bindings::mg_map_destroy(mg_map) };
                return std::ptr::null_mut();
            }
            unsafe {
                bindings::mg_map_insert(mg_map, c_key.as_ptr(), mg_value);
            };
        }
    }
    mg_map
}

pub(crate) fn naive_date_to_mg_date(input: &NaiveDate) -> *mut bindings::mg_date {
    // Unix epoch date is a known valid date
    let unix_epoch = NaiveDate::from_ymd_opt(1970, 1, 1)
        .expect("Unix epoch is a valid date")
        .num_days_from_ce();
    let ptr = unsafe { bindings::mg_date_make((input.num_days_from_ce() - unix_epoch) as i64) };
    // mg_date_make can return NULL on OOM
    if ptr.is_null() {
        return std::ptr::null_mut();
    }
    ptr
}

pub(crate) fn naive_local_time_to_mg_local_time(input: &NaiveTime) -> *mut bindings::mg_local_time {
    let hours_ns = hours_as_seconds(input.hour() as i64) * NSEC_IN_SEC;
    let minutes_ns = minutes_as_seconds(input.minute() as i64) * NSEC_IN_SEC;
    let seconds_ns = (input.second() as i64) * NSEC_IN_SEC;
    let nanoseconds = input.nanosecond() as i64;
    let ptr =
        unsafe { bindings::mg_local_time_make(hours_ns + minutes_ns + seconds_ns + nanoseconds) };
    // mg_local_time_make can return NULL on OOM
    if ptr.is_null() {
        return std::ptr::null_mut();
    }
    ptr
}

pub(crate) fn naive_local_date_time_to_mg_local_date_time(
    input: &NaiveDateTime,
) -> *mut bindings::mg_local_date_time {
    // Unix epoch date is a known valid date
    let unix_epoch = NaiveDate::from_ymd_opt(1970, 1, 1)
        .expect("Unix epoch is a valid date")
        .num_days_from_ce();
    let days_s = days_as_seconds((input.num_days_from_ce() - unix_epoch) as i64);
    let hours_s = hours_as_seconds(input.hour() as i64);
    let minutes_s = minutes_as_seconds(input.minute() as i64);
    let seconds_s = input.second() as i64;
    let nanoseconds = input.nanosecond() as i64;
    let ptr = unsafe {
        bindings::mg_local_date_time_make(days_s + hours_s + minutes_s + seconds_s, nanoseconds)
    };
    // mg_local_date_time_make can return NULL on OOM
    if ptr.is_null() {
        return std::ptr::null_mut();
    }
    ptr
}

pub(crate) fn duration_to_mg_duration(input: &Duration) -> *mut bindings::mg_duration {
    // Duration returns total number of nanoseconds, in order to create a valid mg_duration object,
    // days and seconds have to be reducted from the total duration. In addition, one can get numer
    // of nanoseconds and then substract days and seconds, but since nanoseconds can overflow quite
    // quicky (with 292 years), it's better to use Duration and first reduce days and seconds.
    let mut duration = *input;
    let days = input.num_days();
    duration -= Duration::days(days);
    let seconds = input.num_seconds();
    duration -= Duration::seconds(seconds);
    // After subtracting days and seconds, remaining nanoseconds should always fit in i64
    let nanoseconds = duration.num_nanoseconds().unwrap_or(0);
    let ptr = unsafe { bindings::mg_duration_make(0, days, seconds, nanoseconds) };
    // mg_duration_make can return NULL on OOM
    if ptr.is_null() {
        return std::ptr::null_mut();
    }
    ptr
}

pub(crate) fn point2d_to_mg_point_2d(input: &Point2D) -> *mut bindings::mg_point_2d {
    let ptr =
        unsafe { bindings::mg_point_2d_make(input.srid, input.x_longitude, input.y_latitude) };
    // mg_point_2d_make can return NULL on OOM
    if ptr.is_null() {
        return std::ptr::null_mut();
    }
    ptr
}

pub(crate) fn point3d_to_mg_point_3d(input: &Point3D) -> *mut bindings::mg_point_3d {
    let ptr = unsafe {
        bindings::mg_point_3d_make(
            input.srid,
            input.x_longitude,
            input.y_latitude,
            input.z_height,
        )
    };
    // mg_point_3d_make can return NULL on OOM
    if ptr.is_null() {
        return std::ptr::null_mut();
    }
    ptr
}

pub(crate) fn vector_to_mg_list(vector: &[QueryParam]) -> *mut bindings::mg_list {
    let size = vector.len() as u32;
    let mg_list = unsafe { bindings::mg_list_make_empty(size) };
    if mg_list.is_null() {
        return std::ptr::null_mut();
    }
    for mg_val in vector {
        let mg_value = mg_val.to_c_mg_value();
        if mg_value.is_null() {
            // If value allocation fails, destroy the list and return null
            unsafe { bindings::mg_list_destroy(mg_list) };
            return std::ptr::null_mut();
        }
        unsafe {
            bindings::mg_list_append(mg_list, mg_value);
        };
    }
    mg_list
}

impl Value {
    pub(crate) unsafe fn from_mg_value(c_mg_value: *const bindings::mg_value) -> Value {
        match unsafe { bindings::mg_value_get_type(c_mg_value) } {
            bindings::mg_value_type_MG_VALUE_TYPE_NULL => Value::Null,
            bindings::mg_value_type_MG_VALUE_TYPE_BOOL => Value::Bool(mg_value_bool(c_mg_value)),
            bindings::mg_value_type_MG_VALUE_TYPE_INTEGER => Value::Int(mg_value_int(c_mg_value)),
            bindings::mg_value_type_MG_VALUE_TYPE_FLOAT => Value::Float(mg_value_float(c_mg_value)),
            bindings::mg_value_type_MG_VALUE_TYPE_STRING => {
                Value::String(mg_value_string(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_DATE => {
                // If date conversion fails, return Null instead of panicking
                mg_value_naive_date(c_mg_value)
                    .map(Value::Date)
                    .unwrap_or(Value::Null)
            }
            bindings::mg_value_type_MG_VALUE_TYPE_LOCAL_TIME => {
                // If time conversion fails, return Null instead of panicking
                mg_value_naive_local_time(c_mg_value)
                    .map(Value::LocalTime)
                    .unwrap_or(Value::Null)
            }
            bindings::mg_value_type_MG_VALUE_TYPE_LOCAL_DATE_TIME => {
                // If datetime conversion fails, return Null instead of panicking
                mg_value_naive_local_date_time(c_mg_value)
                    .map(Value::LocalDateTime)
                    .unwrap_or(Value::Null)
            }
            bindings::mg_value_type_MG_VALUE_TYPE_DATE_TIME_ZONE_ID => {
                let c_datetime_zone_id =
                    unsafe { bindings::mg_value_date_time_zone_id(c_mg_value) };
                // If datetime conversion fails, return Null instead of panicking
                mg_value_datetime_zone_id(c_datetime_zone_id)
                    .map(Value::DateTime)
                    .unwrap_or(Value::Null)
            }
            bindings::mg_value_type_MG_VALUE_TYPE_DURATION => {
                Value::Duration(mg_value_duration(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_POINT_2D => {
                Value::Point2D(mg_value_point2d(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_POINT_3D => {
                Value::Point3D(mg_value_point3d(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_LIST => {
                Value::List(mg_value_list_to_vec(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_MAP => Value::Map(mg_value_map(c_mg_value)),
            bindings::mg_value_type_MG_VALUE_TYPE_NODE => Value::Node(mg_value_node(c_mg_value)),
            bindings::mg_value_type_MG_VALUE_TYPE_RELATIONSHIP => {
                Value::Relationship(mg_value_relationship(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_UNBOUND_RELATIONSHIP => {
                Value::UnboundRelationship(mg_value_unbound_relationship(c_mg_value))
            }
            bindings::mg_value_type_MG_VALUE_TYPE_PATH => Value::Path(mg_value_path(c_mg_value)),
            bindings::mg_value_type_MG_VALUE_TYPE_UNKNOWN => Value::Null,
            _ => panic!("Unknown type"),
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Value::Null => write!(f, "NULL"),
            Value::Bool(x) => write!(f, "{}", x),
            Value::Int(x) => write!(f, "{}", x),
            Value::Float(x) => write!(f, "{}", x),
            Value::String(x) => write!(f, "'{}'", x),
            Value::Date(x) => write!(f, "'{}'", x),
            Value::LocalTime(x) => write!(f, "'{}'", x),
            Value::LocalDateTime(x) => write!(f, "'{}'", x),
            Value::DateTime(x) => write!(
                f,
                "'{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09} {}{:02}:{:02}'",
                x.year,
                x.month,
                x.day,
                x.hour,
                x.minute,
                x.second,
                x.nanosecond,
                if x.time_zone_offset_seconds >= 0 {
                    "+"
                } else {
                    "-"
                },
                x.time_zone_offset_seconds.abs() / 3600,
                (x.time_zone_offset_seconds.abs() % 3600) / 60
            ),
            Value::Duration(x) => write!(f, "'{}'", x),
            Value::Point2D(x) => write!(f, "'{}'", x),
            Value::Point3D(x) => write!(f, "'{}'", x),
            Value::List(x) => write!(
                f,
                "{}",
                x.iter()
                    .map(|val| val.to_string())
                    .collect::<Vec<String>>()
                    .join(", ")
            ),
            Value::Map(x) => write!(f, "{}", mg_map_to_string(x)),
            Value::Node(x) => write!(f, "{}", x),
            Value::Relationship(x) => write!(f, "{}", x),
            Value::UnboundRelationship(x) => write!(f, "{}", x),
            Value::Path(x) => write!(f, "{}", x),
        }
    }
}

fn mg_map_to_string(mg_map: &HashMap<String, Value>) -> String {
    let mut properties: Vec<String> = Vec::new();
    let mut sorted: Vec<_> = mg_map.iter().collect();
    sorted.sort_by(|x, y| x.0.cmp(y.0));
    for (key, value) in sorted {
        properties.push(format!("'{}': {}", key, value));
    }
    format!("{{{}}}", properties.join(", "))
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "(:{} {})",
            self.labels.join(", "),
            mg_map_to_string(&self.properties)
        )
    }
}

impl fmt::Display for Relationship {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[:{} {}]",
            self.type_,
            mg_map_to_string(&self.properties)
        )
    }
}

impl fmt::Display for UnboundRelationship {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[:{} {}]",
            self.type_,
            mg_map_to_string(&self.properties)
        )
    }
}

impl fmt::Display for Path {
    fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result {
        unimplemented!();
    }
}

#[cfg(test)]
mod tests;