rayforce 0.1.0

Safe Rust bindings for RayforceDB - the ultra-fast columnar database
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
//! Scalar types for Rayforce.

use crate::error::{RayforceError, Result};
use crate::ffi::RayObj;
use crate::types::RayType;
use crate::*;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Timelike};
use std::fmt;
use uuid::Uuid;

/// Boolean type.
#[derive(Clone)]
pub struct RayBool {
    ptr: RayObj,
}

impl RayBool {
    /// Create a new boolean.
    pub fn new(value: bool) -> Self {
        Self {
            ptr: RayObj::from(value),
        }
    }

    /// Get the boolean value.
    pub fn value(&self) -> bool {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.b8.as_ref() != 0 }
    }
}

impl RayType for RayBool {
    const TYPE_CODE: i8 = -(TYPE_B8 as i8);
    const RAY_NAME: &'static str = "RayBool";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<bool> for RayBool {
    fn from(val: bool) -> Self {
        RayBool::new(val)
    }
}

impl From<RayBool> for bool {
    fn from(b: RayBool) -> Self {
        b.value()
    }
}

/// Type alias for backward compatibility.
pub type B8 = RayBool;

/// Unsigned byte type.
#[derive(Clone)]
pub struct RayU8 {
    ptr: RayObj,
}

impl RayU8 {
    /// Create a new unsigned byte.
    pub fn new(value: u8) -> Self {
        Self {
            ptr: RayObj::from(value),
        }
    }

    /// Get the byte value.
    pub fn value(&self) -> u8 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.u8_.as_ref() }
    }
}

impl RayType for RayU8 {
    const TYPE_CODE: i8 = -(TYPE_U8 as i8);
    const RAY_NAME: &'static str = "RayU8";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

/// Type alias for backward compatibility.
pub type U8 = RayU8;

/// Character type.
#[derive(Clone)]
pub struct RayChar {
    ptr: RayObj,
}

impl RayChar {
    /// Create a new character.
    pub fn new(value: char) -> Self {
        unsafe {
            let ptr = c8(value as i8);
            Self {
                ptr: RayObj::from_raw(ptr),
            }
        }
    }

    /// Get the character value.
    pub fn value(&self) -> char {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.c8.as_ref() as u8 as char }
    }
}

impl RayType for RayChar {
    const TYPE_CODE: i8 = -(TYPE_C8 as i8);
    const RAY_NAME: &'static str = "RayChar";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

impl fmt::Debug for RayChar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RayChar({:?})", self.value())
    }
}

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

/// Type alias for backward compatibility.
pub type C8 = RayChar;

/// 16-bit integer type.
#[derive(Clone)]
pub struct RayI16 {
    ptr: RayObj,
}

impl RayI16 {
    /// Create a new RayI16.
    pub fn new(value: i16) -> Self {
        Self {
            ptr: RayObj::from(value),
        }
    }

    /// Get the integer value.
    pub fn value(&self) -> i16 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.i16_.as_ref() }
    }
}

impl RayType for RayI16 {
    const TYPE_CODE: i8 = -(TYPE_I16 as i8);
    const RAY_NAME: &'static str = "RayI16";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

/// Type alias for backward compatibility.
pub type I16 = RayI16;

/// 32-bit integer type.
#[derive(Clone)]
pub struct RayI32 {
    ptr: RayObj,
}

impl RayI32 {
    /// Create a new RayI32.
    pub fn new(value: i32) -> Self {
        Self {
            ptr: RayObj::from(value),
        }
    }

    /// Get the integer value.
    pub fn value(&self) -> i32 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.i32_.as_ref() }
    }
}

impl RayType for RayI32 {
    const TYPE_CODE: i8 = -(TYPE_I32 as i8);
    const RAY_NAME: &'static str = "RayI32";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

/// Type alias for backward compatibility.
pub type I32 = RayI32;

/// 64-bit integer type.
#[derive(Clone)]
pub struct RayI64 {
    ptr: RayObj,
}

impl RayI64 {
    /// Create a new RayI64.
    pub fn new(value: i64) -> Self {
        Self {
            ptr: RayObj::from(value),
        }
    }

    /// Get the integer value.
    pub fn value(&self) -> i64 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.i64_.as_ref() }
    }
}

impl RayType for RayI64 {
    const TYPE_CODE: i8 = -(TYPE_I64 as i8);
    const RAY_NAME: &'static str = "RayI64";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<i64> for RayI64 {
    fn from(val: i64) -> Self {
        RayI64::new(val)
    }
}

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

/// Type alias for backward compatibility.
pub type I64 = RayI64;

/// 64-bit floating point type.
#[derive(Clone)]
pub struct RayF64 {
    ptr: RayObj,
}

impl RayF64 {
    /// Create a new RayF64.
    pub fn new(value: f64) -> Self {
        Self {
            ptr: RayObj::from(value),
        }
    }

    /// Get the float value.
    pub fn value(&self) -> f64 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.f64_.as_ref() }
    }
}

impl RayType for RayF64 {
    const TYPE_CODE: i8 = -(TYPE_F64 as i8);
    const RAY_NAME: &'static str = "RayF64";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<f64> for RayF64 {
    fn from(val: f64) -> Self {
        RayF64::new(val)
    }
}

impl From<RayF64> for f64 {
    fn from(f: RayF64) -> Self {
        f.value()
    }
}

/// Type alias for backward compatibility.
pub type F64 = RayF64;

/// Symbol type.
#[derive(Clone)]
pub struct RaySymbol {
    ptr: RayObj,
}

impl RaySymbol {
    /// Create a new symbol.
    pub fn new(value: &str) -> Self {
        Self {
            ptr: crate::ffi::new_symbol(value),
        }
    }

    /// Get the symbol value as a string.
    pub fn value(&self) -> String {
        crate::ffi::symbol_to_string(&self.ptr).unwrap_or_default()
    }
}

impl RayType for RaySymbol {
    const TYPE_CODE: i8 = -(TYPE_SYMBOL as i8);
    const RAY_NAME: &'static str = "RaySymbol";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

impl fmt::Debug for RaySymbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RaySymbol(`{})", self.value())
    }
}

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

impl From<&str> for RaySymbol {
    fn from(val: &str) -> Self {
        RaySymbol::new(val)
    }
}

/// Type alias for backward compatibility.
pub type Symbol = RaySymbol;

/// Quoted symbol (for use in expressions).
#[derive(Clone)]
pub struct RayQuotedSymbol {
    ptr: RayObj,
}

impl RayQuotedSymbol {
    /// Create a new quoted symbol.
    pub fn new(value: &str) -> Self {
        let sym = RaySymbol::new(value);
        unsafe {
            // Clone the symbol to "quote" it
            let quoted = clone_obj(sym.ptr.as_ptr());
            Self {
                ptr: RayObj::from_raw(quoted),
            }
        }
    }

    /// Get the symbol value as a string.
    pub fn value(&self) -> String {
        crate::ffi::symbol_to_string(&self.ptr).unwrap_or_default()
    }
}

impl RayType for RayQuotedSymbol {
    const TYPE_CODE: i8 = -(TYPE_SYMBOL as i8);
    const RAY_NAME: &'static str = "RayQuotedSymbol";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

impl fmt::Debug for RayQuotedSymbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "RayQuotedSymbol(`{})", self.value())
    }
}

/// Type alias for backward compatibility.
pub type QuotedSymbol = RayQuotedSymbol;

/// Date type (days since 2000-01-01).
#[derive(Clone)]
pub struct RayDate {
    ptr: RayObj,
}

impl RayDate {
    /// Create a new date from days since epoch (2000-01-01).
    pub fn from_days(days: i32) -> Self {
        Self {
            ptr: crate::ffi::new_date(days),
        }
    }

    /// Create a new date from a NaiveDate.
    pub fn from_naive_date(date: NaiveDate) -> Self {
        let epoch = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
        let days = (date - epoch).num_days() as i32;
        Self::from_days(days)
    }

    /// Get the days since epoch.
    pub fn days(&self) -> i32 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.i32_.as_ref() }
    }

    /// Get the date as a NaiveDate.
    pub fn to_naive_date(&self) -> NaiveDate {
        let epoch = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
        epoch + chrono::Duration::days(self.days() as i64)
    }
}

impl RayType for RayDate {
    const TYPE_CODE: i8 = -(TYPE_DATE as i8);
    const RAY_NAME: &'static str = "RayDate";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<NaiveDate> for RayDate {
    fn from(date: NaiveDate) -> Self {
        RayDate::from_naive_date(date)
    }
}

/// Type alias for backward compatibility.
pub type Date = RayDate;

/// Time type (milliseconds since midnight).
#[derive(Clone)]
pub struct RayTime {
    ptr: RayObj,
}

impl RayTime {
    /// Create a new time from milliseconds since midnight.
    pub fn from_ms(ms: i32) -> Self {
        Self {
            ptr: crate::ffi::new_time(ms),
        }
    }

    /// Create a new time from a NaiveTime.
    pub fn from_naive_time(time: NaiveTime) -> Self {
        let ms = time.num_seconds_from_midnight() as i32 * 1000
            + time.nanosecond() as i32 / 1_000_000;
        Self::from_ms(ms)
    }

    /// Get the milliseconds since midnight.
    pub fn ms(&self) -> i32 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.i32_.as_ref() }
    }

    /// Get the time as a NaiveTime.
    pub fn to_naive_time(&self) -> NaiveTime {
        let ms = self.ms();
        let secs = (ms / 1000) as u32;
        let nanos = ((ms % 1000) * 1_000_000) as u32;
        NaiveTime::from_num_seconds_from_midnight_opt(secs, nanos)
            .unwrap_or_else(|| NaiveTime::from_hms_opt(0, 0, 0).unwrap())
    }
}

impl RayType for RayTime {
    const TYPE_CODE: i8 = -(TYPE_TIME as i8);
    const RAY_NAME: &'static str = "RayTime";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<NaiveTime> for RayTime {
    fn from(time: NaiveTime) -> Self {
        RayTime::from_naive_time(time)
    }
}

/// Type alias for backward compatibility.
pub type Time = RayTime;

/// Timestamp type (nanoseconds since epoch).
#[derive(Clone)]
pub struct RayTimestamp {
    ptr: RayObj,
}

impl RayTimestamp {
    /// Create a new timestamp from nanoseconds since epoch.
    pub fn from_nanos(ns: i64) -> Self {
        Self {
            ptr: crate::ffi::new_timestamp(ns),
        }
    }

    /// Create a new timestamp from a NaiveDateTime.
    pub fn from_naive_datetime(dt: NaiveDateTime) -> Self {
        let ns = dt.and_utc().timestamp_nanos_opt().unwrap_or(0);
        Self::from_nanos(ns)
    }

    /// Get the nanoseconds since epoch.
    pub fn nanos(&self) -> i64 {
        unsafe { *(*self.ptr.as_ptr()).__bindgen_anon_1.i64_.as_ref() }
    }

    /// Get the timestamp as a NaiveDateTime.
    pub fn to_naive_datetime(&self) -> NaiveDateTime {
        let ns = self.nanos();
        let secs = ns / 1_000_000_000;
        let nsec = (ns % 1_000_000_000) as u32;
        chrono::DateTime::from_timestamp(secs, nsec)
            .map(|dt| dt.naive_utc())
            .unwrap_or_default()
    }
}

impl RayType for RayTimestamp {
    const TYPE_CODE: i8 = -(TYPE_TIMESTAMP as i8);
    const RAY_NAME: &'static str = "RayTimestamp";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<NaiveDateTime> for RayTimestamp {
    fn from(dt: NaiveDateTime) -> Self {
        RayTimestamp::from_naive_datetime(dt)
    }
}

/// Type alias for backward compatibility.
pub type Timestamp = RayTimestamp;

/// GUID type.
#[derive(Clone)]
pub struct RayGuid {
    ptr: RayObj,
}

impl RayGuid {
    /// Create a new GUID from a UUID.
    pub fn new(uuid: Uuid) -> Self {
        let bytes = uuid.as_bytes();
        unsafe {
            let obj = vector(TYPE_I64 as i8, 2);
            (*obj).type_ = -(TYPE_GUID as i8);
            let dst = (obj as *mut u8).add(std::mem::size_of::<obj_t>() - 8).add(8);
            std::ptr::copy_nonoverlapping(bytes.as_ptr(), dst, 16);
            Self {
                ptr: RayObj::from_raw(obj),
            }
        }
    }

    /// Create a new random GUID.
    pub fn random() -> Self {
        Self::new(Uuid::new_v4())
    }

    /// Parse a GUID from a string.
    pub fn parse(s: &str) -> Result<Self> {
        let uuid = Uuid::parse_str(s)
            .map_err(|e| RayforceError::InvalidGuid(e.to_string()))?;
        Ok(Self::new(uuid))
    }

    /// Get the GUID as a UUID.
    pub fn to_uuid(&self) -> Uuid {
        unsafe {
            let raw = (self.ptr.as_ptr() as *const u8)
                .add(std::mem::size_of::<obj_t>() - 8)
                .add(8);
            let bytes: [u8; 16] = std::slice::from_raw_parts(raw, 16)
                .try_into()
                .unwrap_or([0; 16]);
            Uuid::from_bytes(bytes)
        }
    }
}

impl RayType for RayGuid {
    const TYPE_CODE: i8 = -(TYPE_GUID as i8);
    const RAY_NAME: &'static str = "RayGuid";

    fn from_ptr(ptr: RayObj) -> Result<Self> {
        if ptr.type_code() != Self::TYPE_CODE {
            return Err(RayforceError::TypeMismatch {
                expected: Self::RAY_NAME.into(),
                actual: format!("type code {}", ptr.type_code()),
            });
        }
        Ok(Self { ptr })
    }

    fn ptr(&self) -> &RayObj {
        &self.ptr
    }
}

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

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

impl From<Uuid> for RayGuid {
    fn from(uuid: Uuid) -> Self {
        RayGuid::new(uuid)
    }
}

/// Type alias for backward compatibility.
pub type GUID = RayGuid;