meos 0.3.0

Rust bindings for MEOS C API
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
use std::{
    cmp,
    ffi::{c_void, CStr, CString},
    fmt::Debug,
    ptr,
};

use crate::temporal::Temporal;
use chrono::{DateTime, TimeDelta, TimeZone, Utc};

use crate::{
    collections::{
        base::{
            Span, {impl_collection, Collection},
        },
        datetime::TsTzSpan,
        number::{FloatSpan, IntSpan, NumberSpan},
    },
    errors::ParseError,
    temporal::TFloat,
    utils::{create_interval, from_meos_timestamp, to_meos_timestamp},
    WKBVariant,
};

use super::r#box::Box as MeosBox;

pub struct TBox {
    _inner: ptr::NonNull<meos_sys::TBox>,
}

impl MeosBox for TBox {
    fn from_wkb(wkb: &[u8]) -> Self {
        unsafe { Self::from_inner(meos_sys::tbox_from_wkb(wkb.as_ptr(), wkb.len())) }
    }

    fn from_hexwkb(hexwkb: &[u8]) -> Self {
        let c_hexwkb = CString::new(hexwkb).unwrap();
        unsafe {
            let inner = meos_sys::tbox_from_hexwkb(c_hexwkb.as_ptr());
            Self::from_inner(inner)
        }
    }

    fn from_temporal_span(value: TsTzSpan) -> Self {
        unsafe { Self::from_inner(meos_sys::span_to_tbox(value.inner())) }
    }

    /// Creates a new `TBox` instance from a `DateTime<Tz>` object.
    /// Using the value as the lower and upper bounds of the temporal span
    ///
    /// ## Arguments
    /// * `time` - A `DateTime<Utc>` instance.
    ///
    /// ## Returns
    /// A new `TBox` instance representing the timestamp.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// use chrono::{Utc, TimeZone};
    /// let datetime = Utc.with_ymd_and_hms(2020, 5, 15, 12, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// assert_eq!(tbox.tmin().unwrap(), datetime);
    /// ```
    fn from_time<Tz: TimeZone>(time: DateTime<Tz>) -> Self {
        // Convert DateTime<Utc> to the expected timestamp format for MEOS
        let timestamptz = to_meos_timestamp(&time);
        unsafe { Self::from_inner(meos_sys::timestamptz_to_tbox(timestamptz)) }
    }

    /// Converts the `TBox` into a `TsTzSpan` representing the timestamp with time zone span.
    ///
    /// ## Returns
    /// A `TsTzSpan` instance.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::meos_initialize;
    /// use chrono::{Utc, TimeZone, TimeDelta};
    /// # meos_initialize();
    /// let datetime = Utc.with_ymd_and_hms(2020, 5, 15, 12, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// let tstzspan = tbox.tstzspan();
    /// assert_eq!(tstzspan, (datetime..=datetime).into()); // Assuming `TsTzSpan` has a `to_string` method
    /// ```
    fn tstzspan(&self) -> TsTzSpan {
        unsafe { TsTzSpan::from_inner(meos_sys::tbox_to_tstzspan(self.inner())) }
    }

    fn as_wkb(&self, variant: WKBVariant) -> &[u8] {
        unsafe {
            let mut size: usize = 0;
            let ptr = meos_sys::tbox_as_wkb(self.inner(), variant.into(), &raw mut size);
            std::slice::from_raw_parts(ptr, size)
        }
    }

    fn as_hexwkb(&self, variant: WKBVariant) -> &[u8] {
        unsafe {
            let mut size: usize = 0;
            let ptr = meos_sys::tbox_as_hexwkb(self.inner(), variant.into(), &raw mut size);
            CStr::from_ptr(ptr).to_bytes()
        }
    }

    /// Checks if the `TBox` has a valid X dimension.
    ///
    /// ## Returns
    /// A boolean indicating the presence of an X dimension.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_int(5);
    /// assert!(tbox.has_x());
    /// ```
    fn has_x(&self) -> bool {
        unsafe { meos_sys::tbox_hasx(self.inner()) }
    }

    /// Checks if the `TBox` has a valid T (time) dimension.
    ///
    /// ## Returns
    /// A boolean indicating the presence of a T dimension.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// use chrono::{Utc, TimeZone};
    /// let datetime = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// assert!(tbox.has_t());
    /// ```
    fn has_t(&self) -> bool {
        unsafe { meos_sys::tbox_hast(self.inner()) }
    }

    /// Retrieves the minimum value of the X dimension in the `TBox`.
    ///
    /// ## Returns
    /// An optional `f64` representing the minimum X value, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_float(3.14);
    /// assert_eq!(tbox.xmin(), Some(3.14));
    /// ```
    fn xmin(&self) -> Option<f64> {
        unsafe {
            let mut value = 0.0;
            let ptr: *mut f64 = ptr::addr_of_mut!(value);
            if meos_sys::tbox_xmin(self.inner(), ptr) {
                Some(value)
            } else {
                None
            }
        }
    }

    /// Retrieves the maximum value of the X dimension in the `TBox`.
    ///
    /// ## Returns
    /// An optional `f64` representing the maximum X value, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_float(3.14);
    /// assert_eq!(tbox.xmax(), Some(3.14));
    /// ```
    fn xmax(&self) -> Option<f64> {
        unsafe {
            let mut value = 0.0;
            let ptr: *mut f64 = ptr::addr_of_mut!(value);
            if meos_sys::tbox_xmax(self.inner(), ptr) {
                Some(value)
            } else {
                None
            }
        }
    }

    /// Retrieves the minimum value of the T (time) dimension in the `TBox`.
    ///
    /// ## Returns
    /// An optional `DateTime<Utc>` representing the minimum time value, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// use chrono::{Utc, TimeZone};
    /// let datetime = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// assert_eq!(tbox.tmin(), Some(datetime));
    /// ```
    fn tmin(&self) -> Option<DateTime<Utc>> {
        unsafe {
            let mut value: i64 = 0;
            let ptr: *mut i64 = ptr::addr_of_mut!(value);
            if meos_sys::tbox_tmin(self.inner(), ptr) {
                Some(from_meos_timestamp(value))
            } else {
                None
            }
        }
    }

    /// Retrieves the maximum value of the T (time) dimension in the `TBox`.
    ///
    /// ## Returns
    /// An optional `DateTime<Utc>` representing the maximum time value, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// use chrono::{Utc, TimeZone};
    /// let datetime = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// assert_eq!(tbox.tmax(), Some(datetime));
    /// ```
    fn tmax(&self) -> Option<DateTime<Utc>> {
        unsafe {
            let mut value: i64 = 0;
            let ptr: *mut i64 = ptr::addr_of_mut!(value);
            if meos_sys::tbox_tmax(self.inner(), ptr) {
                Some(from_meos_timestamp(value))
            } else {
                None
            }
        }
    }
    /// Checks if the minimum T (time) value is inclusive in the `TBox`.
    ///
    /// ## Returns
    /// An optional `bool` indicating if the minimum T value is inclusive, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_int(5);
    /// assert_eq!(tbox.is_tmin_inclusive(), None); // No temporal dimension
    /// ```
    fn is_tmin_inclusive(&self) -> Option<bool> {
        unsafe {
            let mut is_inclusive = false;
            let ptr: *mut bool = ptr::addr_of_mut!(is_inclusive);
            if meos_sys::tbox_tmin_inc(self.inner(), ptr) {
                Some(is_inclusive)
            } else {
                None
            }
        }
    }

    /// Checks if the maximum T (time) value is inclusive in the `TBox`.
    ///
    /// ## Returns
    /// An optional `bool` indicating if the maximum T value is inclusive, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// use chrono::{Utc, TimeZone};
    /// let datetime = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// assert_eq!(tbox.is_tmax_inclusive(), Some(true));
    /// ```
    fn is_tmax_inclusive(&self) -> Option<bool> {
        unsafe {
            let mut is_inclusive = false;
            let ptr: *mut bool = ptr::addr_of_mut!(is_inclusive);
            if meos_sys::tbox_tmax_inc(self.inner(), ptr) {
                Some(is_inclusive)
            } else {
                None
            }
        }
    }

    /// Expands the `TBox` by a specified duration on the T (time) dimension.
    ///
    /// ## Arguments
    /// * `duration` - The duration by which to expand the T dimension.
    ///
    /// ## Returns
    /// A new `TBox` instance with expanded bounds.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// use chrono::{Utc, TimeZone, TimeDelta};
    /// let datetime = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    /// let tbox = TBox::from_time(datetime);
    /// let expanded_tbox = tbox.expand_time(TimeDelta::days(1));
    ///
    /// assert!(expanded_tbox.tmin().unwrap() < datetime);
    /// assert!(expanded_tbox.tmax().unwrap() > datetime);
    /// ```
    fn expand_time(&self, duration: TimeDelta) -> TBox {
        let interval = create_interval(duration);
        unsafe {
            Self::from_inner(meos_sys::tbox_expand_time(
                self.inner(),
                std::ptr::addr_of!(interval),
            ))
        }
    }

    /// Rounds the numerical values in the `TBox` to a specified number of decimal places.
    ///
    /// ## Arguments
    /// * `max_decimals` - The maximum number of decimal places.
    ///
    /// ## Returns
    /// A new `TBox` instance with rounded values.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_float(3.141592);
    /// let rounded_tbox = tbox.round(2);
    /// assert_eq!(rounded_tbox.xmin(), Some(3.14));
    /// ```
    fn round(&self, max_decimals: i32) -> TBox {
        let result = unsafe { meos_sys::tbox_round(self.inner(), max_decimals) };
        TBox::from_inner(result)
    }

    // ------------------------- Distance Operations --------------------------------
    /// Calculates the nearest approach distance between two `TBox` instances.
    ///
    /// ## Arguments
    /// * `other` - The other `TBox` to measure the distance to.
    ///
    /// ## Returns
    /// A `f64` representing the nearest approach distance.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox1 = TBox::from_float(1.0);
    /// let tbox2 = TBox::from_float(5.0);
    /// let distance = tbox1.nearest_approach_distance(&tbox2);
    /// assert_eq!(distance, 4.0);
    /// ```
    fn nearest_approach_distance(&self, other: &TBox) -> f64 {
        unsafe { meos_sys::nad_tboxfloat_tboxfloat(self.inner(), other.inner()) }
    }

    /// Shifts and scales the T (time) dimension of the `TBox`.
    ///
    /// ## Arguments
    /// * `delta` - The duration to shift by.
    /// * `width` - The new width for the T dimension.
    ///
    /// ## Returns
    /// A new `TBox` instance with shifted and scaled bounds.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::TsTzSpan;
    /// use chrono::{Utc, TimeZone, TimeDelta};
    /// let datetime1 = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    /// let datetime2 = Utc.with_ymd_and_hms(2020, 2, 1, 0, 0, 0).unwrap();
    /// let tspan: TsTzSpan = (datetime1..datetime2).into();
    /// let tbox = TBox::from_temporal_span(tspan);
    /// let shifted_scaled_tbox = tbox.shift_scale_time(Some(TimeDelta::days(1)), Some(TimeDelta::days(2)));
    ///
    /// assert_eq!(shifted_scaled_tbox.tmin().unwrap(), datetime1 + TimeDelta::days(2));
    /// assert_eq!(shifted_scaled_tbox.tmax().unwrap(), Utc.with_ymd_and_hms(2020, 1, 7, 0, 0, 0).unwrap());
    /// ```
    fn shift_scale_time(&self, delta: Option<TimeDelta>, width: Option<TimeDelta>) -> TBox {
        let d = {
            if let Some(d) = delta {
                &raw const *Box::new(create_interval(d))
            } else {
                std::ptr::null()
            }
        };

        let w = {
            if let Some(w) = width {
                &raw const *Box::new(create_interval(w))
            } else {
                std::ptr::null()
            }
        };

        let modified = unsafe { meos_sys::tbox_shift_scale_time(self.inner(), d, w) };
        TBox::from_inner(modified)
    }

    fn union(&self, other: &Self, strict: bool) -> Option<Self> {
        let result = unsafe { meos_sys::union_tbox_tbox(self.inner(), other.inner(), strict) };
        if result.is_null() {
            None
        } else {
            Some(Self::from_inner(result))
        }
    }

    fn intersection(&self, other: &Self) -> Option<Self> {
        let result = unsafe { meos_sys::intersection_tbox_tbox(self.inner(), other.inner()) };
        if result.is_null() {
            None
        } else {
            Some(Self::from_inner(result))
        }
    }
}

impl TBox {
    fn inner(&self) -> *const meos_sys::TBox {
        self._inner.as_ptr()
    }

    /// # Panics
    /// Panics if the inner pointer is null.
    pub fn from_inner(inner: *mut meos_sys::TBox) -> Self {
        Self {
            _inner: ptr::NonNull::new(inner).expect("Null pointers not allowed"),
        }
    }

    /// Creates a new `TBox` instance from an integer value.
    /// Using the value as both lower and upper bounds of the value span
    ///
    /// ## Arguments
    /// * `value` - An integer to convert to `TBox`.
    ///
    /// ## Returns
    /// A new `TBox` instance representing the integer.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_int(42);
    /// assert_eq!(tbox.xmin().unwrap(), 42.0);
    /// ```
    pub fn from_int(value: i32) -> Self {
        unsafe { Self::from_inner(meos_sys::int_to_tbox(value)) }
    }

    /// Creates a new `TBox` instance from an float value.
    /// Using the value as both lower and upper bounds of the value span
    ///
    /// ## Arguments
    /// * `value` - An float to convert to `TBox`.
    ///
    /// ## Returns
    /// A new `TBox` instance representing the float.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_float(42.0);
    /// assert_eq!(tbox.xmin().unwrap(), 42.0);
    /// ```
    pub fn from_float(value: f64) -> Self {
        unsafe { Self::from_inner(meos_sys::float_to_tbox(value)) }
    }

    /// Creates a new `TBox` instance from an float value.
    /// Using the value as both lower and upper bounds of the value span
    ///
    /// ## Arguments
    /// * `value` - An float to convert to `TBox`.
    ///
    /// ## Returns
    /// A new `TBox` instance representing the float.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::Span;
    /// # use meos::FloatSpan;
    ///
    /// let span: FloatSpan = (42.0..50.0).into();
    /// let tbox = TBox::from_numeric_span(&span);
    /// assert_eq!(tbox.xmin().unwrap(), 42.0);
    /// assert_eq!(tbox.xmax().unwrap(), 50.0);
    /// ```
    pub fn from_numeric_span(value: &impl NumberSpan) -> Self {
        unsafe { Self::from_inner(meos_sys::span_to_tbox(value.inner())) }
    }

    // pub fn from_tnumber(temporal: TNumber) -> Self {
    //     unsafe {
    //         let inner = tnumber_meos_sys::tbox(temporal.inner);
    //         Self::from_inner(inner)
    //     }
    // }

    /// Converts the `TBox` into an `IntSpan` representing the integer span.
    ///
    /// ## Returns
    /// An `IntSpan` instance.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::IntSpan;
    ///
    /// let tbox = TBox::from_int(5);
    /// let intspan = tbox.intspan();
    /// assert_eq!(intspan, (5..6).into());
    /// ```
    pub fn intspan(&self) -> IntSpan {
        unsafe { IntSpan::from_inner(meos_sys::tbox_to_intspan(self.inner())) }
    }

    /// Converts the `TBox` into an `FloatSpan` representing the float span.
    ///
    /// ## Returns
    /// An `FloatSpan` instance.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::FloatSpan;
    ///
    /// let tbox = TBox::from_float(5.0);
    /// let floatspan = tbox.floatspan();
    /// assert_eq!(floatspan, (5.0..=5.0).into());
    /// ```
    pub fn floatspan(&self) -> FloatSpan {
        unsafe { FloatSpan::from_inner(meos_sys::tbox_to_floatspan(self.inner())) }
    }

    // ------------------------- Accessors -------------------------------------

    /// Checks if the minimum X value is inclusive in the `TBox`.
    ///
    /// ## Returns
    /// An optional `bool` indicating if the minimum X value is inclusive, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// let tbox = TBox::from_float(3.14);
    /// assert_eq!(tbox.xmin_is_inclusive(), Some(true));
    /// ```
    pub fn xmin_is_inclusive(&self) -> Option<bool> {
        unsafe {
            let mut is_inclusive = false;
            let ptr: *mut bool = ptr::addr_of_mut!(is_inclusive);
            if meos_sys::tbox_xmin_inc(self.inner(), ptr) {
                Some(is_inclusive)
            } else {
                None
            }
        }
    }

    /// Checks if the maximum X value is inclusive in the `TBox`.
    ///
    /// ## Returns
    /// An optional `bool` indicating if the maximum X value is inclusive, or `None` if not available.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_float(3.14);
    /// assert_eq!(tbox.xmax_is_inclusive(), Some(true)); // Assuming inclusivity is true by default
    /// ```
    pub fn xmax_is_inclusive(&self) -> Option<bool> {
        unsafe {
            let mut is_inclusive = false;
            let ptr: *mut bool = ptr::addr_of_mut!(is_inclusive);
            if meos_sys::tbox_xmax_inc(self.inner(), ptr) {
                Some(is_inclusive)
            } else {
                None
            }
        }
    }

    // ------------------------- Transformation --------------------------------
    /// Expands the `TBox` by a specified value on the X dimension.
    ///
    /// ## Arguments
    /// * `value` - The value by which to expand the X dimension.
    ///
    /// ## Returns
    /// A new `TBox` instance with expanded bounds.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// let tbox = TBox::from_float(3.0);
    /// let expanded_tbox = tbox.expand_value(2.0);
    /// assert_eq!(expanded_tbox.xmin(), Some(1.0));
    /// assert_eq!(expanded_tbox.xmax(), Some(5.0));
    /// ```
    pub fn expand_value(&self, value: f64) -> TBox {
        unsafe { Self::from_inner(meos_sys::tfloatbox_expand(self.inner(), value)) }
    }

    /// Shifts and scales the X dimension of the `TBox`.
    ///
    /// ## Arguments
    /// * `delta` - The value to shift by.
    /// * `width` - The new width for the X dimension.
    ///
    /// ## Returns
    /// A new `TBox` instance with shifted and scaled bounds.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use std::str::FromStr;
    /// # use meos::meos_initialize;
    /// # meos_initialize();
    /// let tbox = TBox::from_str("TBOXFLOAT XT([0, 10),[2020-06-01, 2020-06-05])").unwrap();
    /// let shifted_scaled_tbox = tbox.shift_scale_value(Some(2.0), Some(4.0));
    ///
    /// assert_eq!(shifted_scaled_tbox.xmin(), Some(2.0)); // Example calculation
    /// assert_eq!(shifted_scaled_tbox.xmax(), Some(6.0)); // Example calculation
    /// ```
    pub fn shift_scale_value(&self, delta: Option<f64>, width: Option<f64>) -> TBox {
        let d = delta.unwrap_or_default();
        let w = width.unwrap_or_default();
        let modified = unsafe {
            meos_sys::tfloatbox_shift_scale(self.inner(), d, w, delta.is_some(), width.is_some())
        };
        TBox::from_inner(modified)
    }
}

impl Collection for TBox {
    impl_collection!(tbox, TFloat);

    fn contains(&self, content: &Self::Type) -> bool {
        unsafe { meos_sys::contains_tbox_tnumber(self.inner(), content.inner()) }
    }
}

impl cmp::PartialEq for TBox {
    /// Checks if two `TBox` instances are equal.
    ///
    /// # Arguments
    /// * `other` - Another `TBox` instance.
    ///
    /// ## Returns
    /// * `true` if the spans are equal, `false` otherwise.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::meos_initialize;
    /// use std::str::FromStr;
    /// # meos_initialize();
    /// let span1: TBox = TBox::from_str("TBOXFLOAT XT([0, 10),[2020-06-01, 2020-06-05])").unwrap();
    /// let span2: TBox = TBox::from_str("TBOXFLOAT XT([0, 10),[2020-06-01, 2020-06-05])").unwrap();
    /// assert_eq!(span1, span2);
    /// ```
    fn eq(&self, other: &Self) -> bool {
        unsafe { meos_sys::tbox_eq(self.inner(), other.inner()) }
    }
}

impl cmp::Eq for TBox {}

impl Debug for TBox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let out_str = unsafe { meos_sys::tbox_out(self.inner(), 3) };
        let c_str = unsafe { CStr::from_ptr(out_str) };
        let str = c_str.to_str().map_err(|_| std::fmt::Error)?;
        let result = f.write_str(str);
        unsafe { libc::free(out_str.cast::<c_void>()) };
        result
    }
}

impl Clone for TBox {
    fn clone(&self) -> Self {
        unsafe { Self::from_inner(meos_sys::tbox_copy(self.inner())) }
    }
}

impl std::str::FromStr for TBox {
    type Err = ParseError;
    /// Parses a `TBox` from a string representation.
    ///
    /// ## Arguments
    /// * `string` - A string slice containing the representation.
    ///
    /// ## Returns
    /// * A `TBox` instance.
    ///
    /// ## Errors
    /// * Returns `ParseSpanError` if the string cannot be parsed.
    ///
    /// ## Example
    /// ```
    /// # use meos::TBox;
    /// use meos::Box;
    /// # use meos::Span;
    /// # use meos::IntSpan;
    /// # use meos::TsTzSpan;
    /// use std::str::FromStr;
    /// # use meos::meos_initialize;
    /// # meos_initialize();
    ///
    /// let tbox: TBox = "TBOXINT XT([0, 10),[2020-06-01, 2020-06-05])".parse().expect("Failed to parse span");
    /// let value_span: IntSpan = tbox.intspan();
    /// let temporal_span: TsTzSpan = tbox.tstzspan();
    /// assert_eq!(value_span, (0..10).into());
    /// assert_eq!(temporal_span, TsTzSpan::from_str("[2020-06-01, 2020-06-05]").unwrap());
    /// ```
    fn from_str(string: &str) -> Result<Self, Self::Err> {
        CString::new(string).map_err(|_| ParseError).map(|string| {
            let inner = unsafe { meos_sys::tbox_in(string.as_ptr()) };
            Self::from_inner(inner)
        })
    }
}