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
//! Objects parsed by the parser.
//!
//! When you are done feeding the parser call `::get_object()` method on a parser. This will give you
//! an owned copy of an `Object`. Difference between `Object` and `ObjectRef` - internal reference count is only decreased for `Object` when dropped.
//!
//! ### Cloning
//!
//! Due to how rust std lib blanket impls work it's impossible to clone `ObjectRef` without converting it into owned `Object`.
//! `Object` implements clone by increasing reference count of object.
//!
//! #### Deep Cloning
//!
//! It's possible to create a deep copy of an `Object` and `ObjectRef` by calling `ObjectRef::deep_copy()`. Copy returned by that method is a completly different object with different address in memory.
//!
//! ### Equality and Ordering
//!
//! Literally all objects can be compared. The order:
//!
//! 1. Type of objects
//! 2. Size of objects
//! 3. Content of objects
//!
//! That means you can compare a string to float, and it will give some result. I'm not sure about usefulness of this, but it is totally possible.
use crate::raw::iterator::Iter;
use crate::raw::{utils, Priority};
use crate::traits::FromObject;
use bitflags::_core::borrow::Borrow;
use bitflags::_core::cmp::Ordering;
use bitflags::_core::convert::Infallible;
use bitflags::_core::fmt::{Display, Formatter};
use libucl_bind::{
    ucl_object_compare, ucl_object_copy, ucl_object_frombool, ucl_object_fromdouble,
    ucl_object_fromint, ucl_object_fromstring, ucl_object_get_priority, ucl_object_key,
    ucl_object_lookup, ucl_object_lookup_path, ucl_object_ref, ucl_object_t,
    ucl_object_toboolean_safe, ucl_object_todouble_safe, ucl_object_toint_safe,
    ucl_object_tostring_forced, ucl_object_tostring_safe, ucl_object_type, ucl_object_unref,
    ucl_type_t,
};
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::convert::TryInto;
use std::error::Error;
use std::ffi::CStr;
use std::fmt;
use std::hash::BuildHasher;
use std::mem::MaybeUninit;
use std::net::{AddrParseError, SocketAddr};
use std::num::TryFromIntError;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::time::Duration;

/// Errors that could be returned by `Object` or `ObjectRef` functions.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ObjectError {
    KeyNotFound(String),
    /// Object was found, but value type doesn't match the desired type.
    ///
    /// NOTE: Error only returned when conversion is done by `FromObject` trait. Built-in functions return `None`.
    WrongType {
        key: String,
        actual_type: ucl_type_t,
        wanted_type: ucl_type_t,
    },
    /// Wrapper around `TryFromIntError`.
    IntConversionError(TryFromIntError),
    /// Wrapper around `AddrParseError`.
    AddrParseError(AddrParseError),
    /// An error that we couldn't match to internal type.
    Other(String),
    /// Not an error, but required for some conversions.
    None,
}

impl Error for ObjectError {}

impl ObjectError {
    /// Wrap error in Box<>.
    pub fn boxed(self) -> Box<ObjectError> {
        Box::new(self)
    }

    /// Wrap error in Box<> and erase its type.
    pub fn boxed_dyn(self) -> Box<dyn Error> {
        Box::new(self)
    }

    /// Create a new error `Other` by extracting the error description.
    pub fn other<E: Display>(err: E) -> ObjectError {
        ObjectError::Other(err.to_string())
    }
}
impl From<Infallible> for ObjectError {
    fn from(_: Infallible) -> Self {
        ObjectError::None
    }
}

impl From<AddrParseError> for ObjectError {
    fn from(source: AddrParseError) -> Self {
        ObjectError::AddrParseError(source)
    }
}
impl From<TryFromIntError> for ObjectError {
    fn from(source: TryFromIntError) -> Self {
        ObjectError::IntConversionError(source)
    }
}

impl fmt::Display for ObjectError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ObjectError::KeyNotFound(key) => write!(f, "Key \"{}\" not found in the object", key),
            ObjectError::WrongType {
                key,
                actual_type,
                wanted_type,
            } => write!(
                f,
                "Key \"{}\" actual type is {:?} and not {:?}",
                key, actual_type, wanted_type
            ),
            ObjectError::IntConversionError(e) => e.fmt(f),
            ObjectError::AddrParseError(e) => e.fmt(f),
            ObjectError::Other(e) => e.fmt(f),
            ObjectError::None => write!(f, "Impossible error was possible after all."),
        }
    }
}

/// Owned and mutable instance of UCL Object.
/// All methods that do not require mutability should be implemented on `ObjectRef` instead.
#[derive(Eq)]
pub struct Object {
    inner: ObjectRef,
}

impl AsRef<ObjectRef> for Object {
    fn as_ref(&self) -> &ObjectRef {
        &self.inner
    }
}

impl Deref for Object {
    type Target = ObjectRef;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for Object {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl fmt::Debug for Object {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let ptr = unsafe { ucl_object_tostring_forced(self.as_ptr()) };
        let cstr = unsafe { CStr::from_ptr(ptr) };
        let as_string = cstr.to_string_lossy().to_string();
        f.debug_struct("Object")
            .field("string_value", &as_string)
            .finish()
    }
}

impl Object {
    pub(crate) fn from_c_ptr(object: *const ucl_object_t) -> Option<Object> {
        ObjectRef::from_c_ptr(object).map(|obj_ref| Object { inner: obj_ref })
    }
}

/// Objects may not actually dropped, but their reference count is decreased.
impl Drop for Object {
    fn drop(&mut self) {
        unsafe {
            if !self.object.is_null() {
                ucl_object_unref(self.object as *mut ucl_object_t);
            }
        }
    }
}

impl Borrow<ObjectRef> for Object {
    fn borrow(&self) -> &ObjectRef {
        &self.inner
    }
}

/// An immutable reference to UCL Object structure.
/// Provides most of the libUCL interface for interacting with parser results.
#[derive(Eq)]
pub struct ObjectRef {
    object: *mut ucl_object_t,
    kind: ucl_type_t,
}

impl ObjectRef {
    /// Return mutable pointer to inner struct.
    pub fn as_mut_ptr(&mut self) -> *mut ucl_object_t {
        self.object
    }
    /// Return const pointer to inner struct.
    pub fn as_ptr(&self) -> *const ucl_object_t {
        self.object
    }

    pub(crate) fn from_c_ptr(object: *const ucl_object_t) -> Option<ObjectRef> {
        if object.is_null() {
            return None;
        }
        let kind = unsafe { ucl_object_type(object) };
        let result = ObjectRef {
            object: object as *mut ucl_object_t,
            kind,
        };
        Some(result)
    }

    /// Perform a deep copy
    pub fn deep_copy(&self) -> Object {
        let ptr = unsafe { ucl_object_copy(self.as_ptr()) };
        Object::from_c_ptr(ptr).expect("Got Object with null ptr")
    }

    /// Returns `true` if this object is a null.
    pub fn is_null(&self) -> bool {
        self.kind == ucl_type_t::UCL_NULL
    }

    /// Returns `true` if this object is an object (think hashmap).
    pub fn is_object(&self) -> bool {
        self.kind == ucl_type_t::UCL_OBJECT
    }

    /// Returns `true` if this object is a string.
    pub fn is_string(&self) -> bool {
        self.kind == ucl_type_t::UCL_STRING
    }

    /// Returns `true` if this object is an integer.
    pub fn is_integer(&self) -> bool {
        self.kind == ucl_type_t::UCL_INT
    }

    /// Returns `true` if this object is a float.
    pub fn is_float(&self) -> bool {
        self.kind == ucl_type_t::UCL_FLOAT
    }

    /// Returns `true` if this object is a boolean type.
    pub fn is_boolean(&self) -> bool {
        self.kind == ucl_type_t::UCL_BOOLEAN
    }

    /// Returns `true` if this object is an array.
    pub fn is_array(&self) -> bool {
        self.kind == ucl_type_t::UCL_ARRAY
    }

    /// Returns `true` if this object is a time/duration.
    pub fn is_time(&self) -> bool {
        self.kind == ucl_type_t::UCL_TIME
    }

    /// Get priority assigned to the object.
    pub fn priority(&self) -> Priority {
        let out = unsafe { ucl_object_get_priority(self.object) };
        Priority::from(out)
    }

    /// Get type/kind of given object
    pub fn kind(&self) -> ucl_type_t {
        self.kind
    }

    /// Get key assigned to the object
    pub fn key(&self) -> Option<String> {
        let c_str = unsafe { ucl_object_key(self.object) };
        utils::to_str(c_str)
    }

    /// Lookup a key within an object with type Object.
    pub fn lookup<K: AsRef<str>>(&self, key: K) -> Option<ObjectRef> {
        if !self.is_object() {
            return None;
        }
        let key = utils::to_c_string(key);
        let obj = unsafe { ucl_object_lookup(self.object, key.as_ptr()) };
        ObjectRef::from_c_ptr(obj as *mut ucl_object_t)
    }

    /// Perform a nested lookup with dot notation.
    pub fn lookup_path<K: AsRef<str>>(&self, path: K) -> Option<ObjectRef> {
        if !self.is_object() {
            return None;
        }
        let key = utils::to_c_string(path);
        let obj = unsafe { ucl_object_lookup_path(self.object, key.as_ptr()) };
        ObjectRef::from_c_ptr(obj as *mut ucl_object_t)
    }
    /// Return string value or None.
    pub fn as_string(&self) -> Option<String> {
        if !self.is_string() {
            return None;
        }
        let mut ptr = MaybeUninit::zeroed();
        let result = unsafe { ucl_object_tostring_safe(self.object, ptr.as_mut_ptr()) };
        if result {
            let ptr = unsafe { ptr.assume_init() };
            utils::to_str(ptr)
        } else {
            None
        }
    }

    /// Return an integer value or None.
    pub fn as_i64(&self) -> Option<i64> {
        if !self.is_integer() {
            return None;
        }
        let mut ptr = MaybeUninit::zeroed();
        let result = unsafe { ucl_object_toint_safe(self.object, ptr.as_mut_ptr()) };
        if result {
            let ptr = unsafe { ptr.assume_init() };
            Some(ptr)
        } else {
            None
        }
    }

    /// Return a float number of seconds. Only works if object is time.
    pub fn as_time(&self) -> Option<f64> {
        if !self.is_time() {
            return None;
        }
        self.as_f64()
    }

    /// Return a float value or None. This function also works on time object.
    pub fn as_f64(&self) -> Option<f64> {
        if !(self.is_float() || self.is_time()) {
            return None;
        }
        let mut ptr = MaybeUninit::zeroed();
        let result = unsafe { ucl_object_todouble_safe(self.object, ptr.as_mut_ptr()) };
        if result {
            let ptr = unsafe { ptr.assume_init() };
            Some(ptr)
        } else {
            None
        }
    }

    /// Return a boolean value or None.
    pub fn as_bool(&self) -> Option<bool> {
        if !self.is_boolean() {
            return None;
        }
        let mut ptr = MaybeUninit::zeroed();
        let result = unsafe { ucl_object_toboolean_safe(self.object, ptr.as_mut_ptr()) };
        if result {
            let ptr = unsafe { ptr.assume_init() };
            Some(ptr)
        } else {
            None
        }
    }

    /// Return `()` or None.
    pub fn as_null(&self) -> Option<()> {
        if !self.is_null() {
            return None;
        }
        Some(())
    }

    /// Preferred way to construct an iterator. Items returned by this iterator are always `ObjectRef`.
    pub fn iter(&self) -> Iter {
        Iter::new(self)
    }
}

impl From<i64> for Object {
    fn from(source: i64) -> Self {
        let ptr = unsafe { ucl_object_fromint(source) };
        Object::from_c_ptr(ptr).expect("Failed to construct an object.")
    }
}

impl From<f64> for Object {
    fn from(source: f64) -> Self {
        let ptr = unsafe { ucl_object_fromdouble(source) };
        Object::from_c_ptr(ptr).expect("Failed to construct an object.")
    }
}

impl From<bool> for Object {
    fn from(source: bool) -> Self {
        let ptr = unsafe { ucl_object_frombool(source) };
        Object::from_c_ptr(ptr).expect("Failed to construct an object.")
    }
}
impl From<&str> for Object {
    fn from(source: &str) -> Self {
        let cstring = utils::to_c_string(source);
        let ptr = unsafe { ucl_object_fromstring(cstring.as_ptr()) };
        Object::from_c_ptr(ptr).expect("Failed to construct an object.")
    }
}

impl FromObject<ObjectRef> for i64 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(ret) = value.as_i64() {
            Ok(ret)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for u64 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for i32 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for u32 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for i16 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for u16 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for i8 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for u8 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(val) = value.as_i64() {
            val.try_into().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_INT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for f64 {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(ret) = value.as_f64() {
            Ok(ret)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_FLOAT,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for bool {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(ret) = value.as_bool() {
            Ok(ret)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_BOOLEAN,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for () {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if value.is_null() {
            Ok(())
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_NULL,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for String {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(ret) = value.as_string() {
            Ok(ret)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_STRING,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for PathBuf {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(ret) = value.as_string() {
            Ok(ret.into())
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_STRING,
            };
            Err(err)
        }
    }
}

impl FromObject<ObjectRef> for SocketAddr {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(ret) = value.as_string() {
            ret.parse().map_err(ObjectError::from)
        } else {
            let err = ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_STRING,
            };
            Err(err)
        }
    }
}
impl<T> FromObject<ObjectRef> for Vec<T>
where
    T: FromObject<ObjectRef>,
{
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        let ret = value
            .iter()
            .map(T::try_from)
            .collect::<Vec<Result<T, ObjectError>>>();
        if let Some(Err(err)) = ret.iter().find(|e| e.is_err()) {
            Err(err.clone())
        } else {
            let list = ret.into_iter().filter_map(|e| e.ok()).collect();
            Ok(list)
        }
    }
}

impl<T> FromObject<ObjectRef> for Option<T>
where
    T: FromObject<ObjectRef>,
{
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        (T::try_from(value)).map(Some)
    }
}

impl<T, S> FromObject<ObjectRef> for HashMap<String, T, S>
where
    T: FromObject<ObjectRef> + Clone,
    S: BuildHasher + Default,
{
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if ucl_type_t::UCL_OBJECT != value.kind {
            return Err(ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_OBJECT,
            });
        }
        let as_entries: Vec<(String, Result<T, ObjectError>)> = value
            .iter()
            .map(|obj| {
                (
                    obj.key().expect("Object without key!"),
                    FromObject::try_from(obj),
                )
            })
            .collect();

        if let Some((_, Err(e))) = as_entries.iter().find(|(_key, result)| result.is_err()) {
            Err(e.clone())
        } else {
            Ok(as_entries
                .iter()
                .cloned()
                .map(|(key, result)| (key, result.unwrap()))
                .collect())
        }
    }
}

impl FromObject<ObjectRef> for Duration {
    fn try_from(value: ObjectRef) -> Result<Self, ObjectError> {
        if let Some(seconds) = value.as_time() {
            Ok(Duration::from_secs_f64(seconds))
        } else {
            Err(ObjectError::WrongType {
                key: value.key().unwrap_or_default(),
                actual_type: value.kind,
                wanted_type: ucl_type_t::UCL_TIME,
            })
        }
    }
}

impl fmt::Debug for ObjectRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let ptr = unsafe { ucl_object_tostring_forced(self.as_ptr()) };
        let cstr = unsafe { CStr::from_ptr(ptr) };
        let as_string = cstr.to_string_lossy().to_string();
        f.debug_struct("ObjectRef")
            .field("string_value", &as_string)
            .finish()
    }
}

impl ToOwned for ObjectRef {
    type Owned = Object;

    fn to_owned(&self) -> Self::Owned {
        let ptr = unsafe { ucl_object_ref(self.as_ptr()) };
        Object::from_c_ptr(ptr).expect("Got ObjectRef with null ptr")
    }
}

impl PartialEq for ObjectRef {
    fn eq(&self, other: &Self) -> bool {
        let cmp = unsafe { ucl_object_compare(self.as_ptr(), other.as_ptr()) };
        cmp == 0
    }
}

impl PartialEq for Object {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref() == other.as_ref()
    }
}

impl Clone for Object {
    fn clone(&self) -> Self {
        let ptr = unsafe { ucl_object_ref(self.as_ptr()) };
        Object::from_c_ptr(ptr).expect("Got ObjectRef with null ptr")
    }
}

impl PartialOrd for ObjectRef {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let cmp = unsafe { ucl_object_compare(self.as_ptr(), other.as_ptr()) };
        match cmp {
            cmp if cmp == 0 => Some(Ordering::Equal),
            cmp if cmp < 0 => Some(Ordering::Less),
            cmp if cmp > 0 => Some(Ordering::Greater),
            _ => unreachable!(),
        }
    }
}

impl Ord for ObjectRef {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}

impl PartialOrd for Object {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.as_ref().partial_cmp(other.as_ref())
    }
}

impl Ord for Object {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}
#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn eq() {
        let left = Object::from(1);
        let right = Object::from(1);
        let right_but_wrong = Object::from(2);

        assert_eq!(left, right);
        assert_ne!(left, right_but_wrong);
    }

    #[test]
    fn deep_copy() {
        let left = Object::from(1);
        let right = left.deep_copy();
        assert_eq!(left, right);
        assert_ne!(left.as_ptr(), right.as_ptr());
    }

    #[test]
    fn order_good() {
        let left = Object::from(1);
        let right = Object::from(2);

        assert!(left < right);
    }

    #[test]
    fn order_int_and_float() {
        let left = Object::from(1);
        let right = Object::from(1.5);

        assert!(left < right);
    }

    #[test]
    fn order_wtf() {
        let left = Object::from("a string?");
        let right = Object::from(2);

        assert_ne!(left, right);
    }
}