simd_json/value/lazy/
trait_impls.rs

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
use std::{
    borrow::{Borrow, Cow},
    hash::Hash,
    io::{self, Write},
};

use value_trait::{
    base::{
        TypedValue, ValueAsArray as _, ValueAsMutArray, ValueAsMutObject, ValueAsObject as _,
        ValueAsScalar, ValueIntoString, Writable,
    },
    derived::{
        ValueArrayTryAccess, ValueObjectAccessAsArray as _, ValueObjectAccessAsObject as _,
        ValueObjectAccessAsScalar, ValueObjectAccessTryAsArray as _,
        ValueObjectAccessTryAsObject as _, ValueObjectAccessTryAsScalar, ValueObjectTryAccess,
        ValueTryAsScalar,
    },
    TryTypeError, ValueBuilder, ValueType,
};

use crate::{borrowed, tape};

use super::{Array, Object, Value};

impl<'value> ValueBuilder<'value> for Value<'static, 'static, 'value> {
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn null() -> Self {
        Value::Tape(tape::Value::null())
    }
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn array_with_capacity(capacity: usize) -> Self {
        Value::Value(Cow::Owned(borrowed::Value::array_with_capacity(capacity)))
    }
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn object_with_capacity(capacity: usize) -> Self {
        Value::Value(Cow::Owned(borrowed::Value::object_with_capacity(capacity)))
    }
}

// TypedContainerValue
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input>
where
    'input: 'tape,
{
    /// returns true if the current value can be represented as an array
    #[must_use]
    pub fn is_array(&self) -> bool {
        self.as_array().is_some()
    }

    /// returns true if the current value can be represented as an object
    #[must_use]
    pub fn is_object(&self) -> bool {
        self.as_object().is_some()
    }
}

impl<'borrow, 'tape, 'value> ValueAsScalar for Value<'borrow, 'tape, 'value> {
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_null(&self) -> Option<()> {
        match &self {
            Value::Tape(tape) => tape.as_null(),
            Value::Value(value) => value.as_null(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_bool(&self) -> Option<bool> {
        match &self {
            Value::Tape(tape) => tape.as_bool(),
            Value::Value(value) => value.as_bool(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_i64(&self) -> Option<i64> {
        match &self {
            Value::Tape(tape) => tape.as_i64(),
            Value::Value(value) => value.as_i64(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_i128(&self) -> Option<i128> {
        match &self {
            Value::Tape(tape) => tape.as_i128(),
            Value::Value(value) => value.as_i128(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_u64(&self) -> Option<u64> {
        match &self {
            Value::Tape(tape) => tape.as_u64(),
            Value::Value(value) => value.as_u64(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_u128(&self) -> Option<u128> {
        match &self {
            Value::Tape(tape) => tape.as_u128(),
            Value::Value(value) => value.as_u128(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_f64(&self) -> Option<f64> {
        match &self {
            Value::Tape(tape) => tape.as_f64(),
            Value::Value(value) => value.as_f64(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn cast_f64(&self) -> Option<f64> {
        match &self {
            Value::Tape(tape) => tape.cast_f64(),
            Value::Value(value) => value.cast_f64(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_str(&self) -> Option<&str> {
        match &self {
            Value::Tape(tape) => tape.as_str(),
            Value::Value(value) => value.as_str(),
        }
    }
}

impl<'borrow, 'tape, 'value> ValueIntoString for Value<'borrow, 'tape, 'value> {
    type String = Cow<'value, str>;

    fn into_string(self) -> Option<<Self as ValueIntoString>::String> {
        match self {
            Value::Tape(tape) => tape.into_string().map(Cow::Borrowed),
            // This is a bit complex but it allows us to avoid cloning
            Value::Value(value) => match value {
                Cow::Borrowed(value) => match value {
                    #[cfg(feature = "beef")]
                    borrowed::Value::String(s) => Some(s.clone().into()),
                    #[cfg(not(feature = "beef"))]
                    borrowed::Value::String(s) => Some(s.clone()),
                    _ => None,
                },
                Cow::Owned(value) => match value {
                    #[cfg(feature = "beef")]
                    borrowed::Value::String(s) => Some(s.into()),
                    #[cfg(not(feature = "beef"))]
                    borrowed::Value::String(s) => Some(s),
                    _ => None,
                },
            },
        }
    }
}

// impl<'tape, 'input> ValueIntoContainer for Value<'tape, 'input> {
//     type Array = array::Array<'tape, 'input>;
//     type Object = Object<'tape, 'input>;
//     #[must_use]
//     fn into_array(self) -> Option<Self::Array> {
//         if let Some(Node::Array { count, .. }) = self.0.first() {
//             // we add one element as we want to keep the array header
//             let count = *count + 1;
//             Some(Array(&self.0[..count]))
//         } else {
//             None
//         }
//     }

//     #[must_use]
//     fn into_object(self) -> Option<Self::Object> {
//         if let Some(Node::Object { count, .. }) = self.0.first() {
//             // we add one element as we want to keep the object header
//             let count = *count + 1;
//             Some(Object(&self.0[..count]))
//         } else {
//             None
//         }
//     }
// }

impl<'borrow, 'tape, 'input> TypedValue for Value<'borrow, 'tape, 'input> {
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn value_type(&self) -> ValueType {
        match &self {
            Value::Tape(tape) => tape.value_type(),
            Value::Value(value) => value.value_type(),
        }
    }
}

// TryValueObjectAccess
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    // type Key = &str;
    // type Target = Value<'tape, 'input>;

    /// Tries to get a value based on a key, returns a `TryTypeError` if the
    /// current Value isn't an Object, returns `None` if the key isn't in the object
    /// # Errors
    /// if the value is not an object
    pub fn try_get<Q>(&self, k: &Q) -> Result<Option<Value<'_, 'tape, 'input>>, TryTypeError>
    where
        str: Borrow<Q> + Hash + Eq,
        for<'b> crate::cow::Cow<'b, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => Ok(tape.try_get(k)?.map(Value::Tape)),
            Value::Value(value) => Ok(value.try_get(k)?.map(Cow::Borrowed).map(Value::Value)),
        }
    }
}

//TryValueArrayAccess
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input>
where
    'input: 'tape,
{
    /// Tries to get a value based on n index, returns a type error if the
    /// current value isn't an Array, returns `None` if the index is out of bounds
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    pub fn try_get_idx(&self, i: usize) -> Result<Option<Value<'_, 'tape, 'input>>, TryTypeError> {
        match self {
            Value::Tape(tape) => Ok(tape.try_get_idx(i)?.map(Value::Tape)),
            Value::Value(value) => Ok(value.try_get_idx(i)?.map(Cow::Borrowed).map(Value::Value)),
        }
    }
}

// impl<'tape, 'value> ValueAsContainer for Value<'tape, 'value> {
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input>
where
    'input: 'tape,
{
    // type Array = array::Array<'tape, 'value>;
    // type Object = Object<'tape, 'value>;

    /// Tries to represent the value as an array and returns a reference to it
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    pub fn as_array(&self) -> Option<Array<'_, 'tape, 'input>> {
        match self {
            Value::Tape(tape) => tape.as_array().map(Array::Tape),
            Value::Value(value) => value.as_array().map(Array::Value),
        }
    }

    /// Tries to represent the value as an array and returns a reference to it
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    pub fn as_object(&self) -> Option<Object<'_, 'tape, 'input>> {
        match self {
            Value::Tape(tape) => tape.as_object().map(Object::Tape),
            Value::Value(value) => value.as_object().map(Object::Value),
        }
    }
}

impl<'borrow, 'tape, 'input> ValueAsMutArray for Value<'borrow, 'tape, 'input> {
    type Array = Vec<borrowed::Value<'input>>;
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_array_mut(&mut self) -> Option<&mut Vec<borrowed::Value<'input>>> {
        self.as_mut().as_array_mut()
    }
}
impl<'borrow, 'tape, 'input> ValueAsMutObject for Value<'borrow, 'tape, 'input> {
    type Object = super::borrowed::Object<'input>;
    #[cfg_attr(not(feature = "no-inline"), inline)]
    #[must_use]
    fn as_object_mut(&mut self) -> Option<&mut super::borrowed::Object<'input>> {
        self.as_mut().as_object_mut()
    }
}

// ContainerValueTryAs (needed as we don't have ValueAsContainer)
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    /// Tries to represent the value as an array and returns a reference to it
    /// # Errors
    /// if the requested type doesn't match the actual type
    pub fn try_as_array(&self) -> Result<Array<'_, 'tape, 'input>, TryTypeError> {
        self.as_array().ok_or(TryTypeError {
            expected: ValueType::Array,
            got: self.value_type(),
        })
    }

    /// Tries to represent the value as an object and returns a reference to it
    /// # Errors
    /// if the requested type doesn't match the actual type
    pub fn try_as_object(&self) -> Result<Object<'_, 'tape, 'input>, TryTypeError> {
        self.as_object().ok_or(TryTypeError {
            expected: ValueType::Object,
            got: self.value_type(),
        })
    }
}
// ValueObjectAccess (needed as we don't have ValueAsContainer ) and can't return references
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    /// Gets a ref to a value based on a key, returns `None` if the
    /// current Value isn't an Object or doesn't contain the key
    /// it was asked for.
    #[must_use]
    pub fn get<'k, Q>(&self, k: &'k Q) -> Option<Value<'_, 'tape, 'input>>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => tape.get(k).map(Value::Tape),
            Value::Value(value) => value
                .as_object()?
                .get(k)
                .map(Cow::Borrowed)
                .map(Value::Value),
        }
    }

    /// Checks if a Value contains a given key. This will return
    /// flase if Value isn't an object  
    #[must_use]
    pub fn contains_key<Q>(&self, k: &Q) -> bool
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        let Some(o) = self.as_object() else {
            return false;
        };
        let v = o.get(k).is_some();
        v
    }
}

// ValueArrayAccess (needed as we don't have ValueAsContainer)
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    /// Gets a ref to a value based on n index, returns `None` if the
    /// current Value isn't an Array or doesn't contain the index
    /// it was asked for.
    #[must_use]
    pub fn get_idx(&self, i: usize) -> Option<Value<'_, 'tape, 'input>> {
        match self {
            Value::Tape(tape) => tape.get_idx(i).map(Value::Tape),
            Value::Value(value) => value
                .as_array()?
                .get(i)
                .map(Cow::Borrowed)
                .map(Value::Value),
        }
    }
}

// impl<'tape, 'input> ValueObjectAccessAsScalar for Value<'tape, 'input>
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input>
where
    'input: 'tape,
{
    /// Tries to get an element of an object as a bool
    pub fn get_bool<Q>(&self, k: &Q) -> Option<bool>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_bool()
    }

    /// Tries to get an element of an object as a i128
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_i128<Q>(&self, k: &Q) -> Option<i128>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_i128()
    }

    /// Tries to get an element of an object as a i64
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_i64<Q>(&self, k: &Q) -> Option<i64>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_i64()
    }

    /// Tries to get an element of an object as a i32
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_i32<Q>(&self, k: &Q) -> Option<i32>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_i32()
    }

    /// Tries to get an element of an object as a i16
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_i16<Q>(&self, k: &Q) -> Option<i16>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_i16()
    }

    /// Tries to get an element of an object as a i8
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_i8<Q>(&self, k: &Q) -> Option<i8>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_i8()
    }

    /// Tries to get an element of an object as a u128
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_u128<Q>(&self, k: &Q) -> Option<u128>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k)?.as_u128()
    }

    /// Tries to get an element of an object as a u64
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_u64<Q>(&self, k: &Q) -> Option<u64>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_u64())
    }

    /// Tries to get an element of an object as a usize
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_usize<Q>(&self, k: &Q) -> Option<usize>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_usize())
    }

    /// Tries to get an element of an object as a u32
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_u32<Q>(&self, k: &Q) -> Option<u32>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_u32())
    }

    /// Tries to get an element of an object as a u16
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_u16<Q>(&self, k: &Q) -> Option<u16>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_u16())
    }

    /// Tries to get an element of an object as a u8
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_u8<Q>(&self, k: &Q) -> Option<u8>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_u8())
    }

    /// Tries to get an element of an object as a f64
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_f64<Q>(&self, k: &Q) -> Option<f64>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_f64())
    }

    /// Tries to get an element of an object as a f32
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_f32<Q>(&self, k: &Q) -> Option<f32>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.get(k).and_then(|v| v.as_f32())
    }
    /// Tries to get an element of an object as a str
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn get_str<Q>(&self, k: &Q) -> Option<&str>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => tape.get_str(k),
            Value::Value(value) => value.get_str(k),
        }
    }
}

// ValueObjectContainerAccess
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    /// Tries to get an element of an object as a array
    #[must_use]
    pub fn get_array<Q>(&self, k: &Q) -> Option<Array<'_, 'tape, 'input>>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => tape.get_array(k).map(Array::Tape),
            Value::Value(value) => value.get_array(k).map(Array::Value),
        }
    }

    /// Tries to get an element of an object as a object
    #[must_use]
    pub fn get_object<Q>(&self, k: &Q) -> Option<Object<'_, 'tape, 'input>>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => tape.get_object(k).map(Object::Tape),
            Value::Value(value) => value.get_object(k).map(Object::Value),
        }
    }
}
// TryValueObjectContainerAccess
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    /// Tries to get an element of an object as an array, returns
    /// an error if it isn't a array
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    pub fn try_get_array<Q>(&self, k: &Q) -> Result<Option<Array<'_, 'tape, 'input>>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => Ok(tape.try_get_array(k)?.map(Array::Tape)),
            Value::Value(value) => Ok(value.try_get_array(k)?.map(Array::Value)),
        }
    }

    /// Tries to get an element of an object as an object, returns
    /// an error if it isn't an object
    ///
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    pub fn try_get_object<Q>(
        &self,
        k: &Q,
    ) -> Result<Option<Object<'_, 'tape, 'input>>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => Ok(tape.try_get_object(k)?.map(Object::Tape)),
            Value::Value(value) => Ok(value.try_get_object(k)?.map(Object::Value)),
        }
    }
}

// impl<'tape, 'input> ValueObjectAccessTryAsScalar for Value<'tape, 'input> {
impl<'borrow, 'tape, 'input> Value<'borrow, 'tape, 'input> {
    /// Tries to get an element of an object as a bool, returns
    /// an error if it isn't bool
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    pub fn try_get_bool<Q>(&self, k: &Q) -> Result<Option<bool>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_bool()).transpose()
    }

    /// Tries to get an element of an object as a i128, returns
    /// an error if it isn't i128
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_i128<Q>(&self, k: &Q) -> Result<Option<i128>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_i128()).transpose()
    }

    /// Tries to get an element of an object as a i64, returns
    /// an error if it isn't a i64
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_i64<Q>(&self, k: &Q) -> Result<Option<i64>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_i64()).transpose()
    }

    /// Tries to get an element of an object as a i32, returns
    /// an error if it isn't a i32
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_i32<Q>(&self, k: &Q) -> Result<Option<i32>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_i32()).transpose()
    }

    /// Tries to get an element of an object as a i16, returns
    /// an error if it isn't a i16
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_i16<Q>(&self, k: &Q) -> Result<Option<i16>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_i16()).transpose()
    }

    /// Tries to get an element of an object as a u128, returns
    /// an error if it isn't a u128
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_i8<Q>(&self, k: &Q) -> Result<Option<i8>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_i8()).transpose()
    }

    /// Tries to get an element of an object as a u64, returns
    /// an error if it isn't a u64
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_u128<Q>(&self, k: &Q) -> Result<Option<u128>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_u128()).transpose()
    }

    /// Tries to get an element of an object as a usize, returns
    /// an error if it isn't a usize
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_u64<Q>(&self, k: &Q) -> Result<Option<u64>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_u64()).transpose()
    }

    /// Tries to get an element of an object as a u32, returns
    /// an error if it isn't a u32
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_usize<Q>(&self, k: &Q) -> Result<Option<usize>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_usize()).transpose()
    }

    /// Tries to get an element of an object as a u16, returns
    /// an error if it isn't a u16
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_u32<Q>(&self, k: &Q) -> Result<Option<u32>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_u32()).transpose()
    }

    /// Tries to get an element of an object as a u8, returns
    /// an error if it isn't a u8
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_u16<Q>(&self, k: &Q) -> Result<Option<u16>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_u16()).transpose()
    }

    /// Tries to get an element of an object as a u8, returns
    /// an error if it isn't a u8
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_u8<Q>(&self, k: &Q) -> Result<Option<u8>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_u8()).transpose()
    }

    /// Tries to get an element of an object as a f64, returns
    /// an error if it isn't a f64
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_f64<Q>(&self, k: &Q) -> Result<Option<f64>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_f64()).transpose()
    }

    /// Tries to get an element of an object as a f32, returns
    /// an error if it isn't a f32
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_f32<Q>(&self, k: &Q) -> Result<Option<f32>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        self.try_get(k)?.map(|v| v.try_as_f32()).transpose()
    }

    /// Tries to get an element of an object as a str, returns
    /// an error if it isn't a str
    /// # Errors
    /// if the requested type doesn't match the actual type or the value is not an object
    #[cfg_attr(not(feature = "no-inline"), inline)]
    pub fn try_get_str<Q>(&self, k: &Q) -> Result<Option<&'_ str>, TryTypeError>
    where
        str: Borrow<Q>,
        for<'a> crate::cow::Cow<'a, str>: Borrow<Q>,
        Q: ?Sized + Hash + Eq + Ord,
    {
        match self {
            Value::Tape(tape) => tape.try_get_str(k),
            Value::Value(value) => value.try_get_str(k),
        }
    }
}

impl<'borrow, 'tape, 'input> Writable for Value<'borrow, 'tape, 'input> {
    #[cfg_attr(not(feature = "no-inline"), inline)]
    fn encode(&self) -> String {
        match self {
            Value::Tape(tape) => tape.encode(),
            Value::Value(value) => value.encode(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    fn encode_pp(&self) -> String {
        match self {
            Value::Tape(tape) => tape.encode_pp(),
            Value::Value(value) => value.encode_pp(),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    fn write<'writer, W>(&self, w: &mut W) -> io::Result<()>
    where
        W: 'writer + Write,
    {
        match self {
            Value::Tape(tape) => tape.write(w),
            Value::Value(value) => value.write(w),
        }
    }

    #[cfg_attr(not(feature = "no-inline"), inline)]
    fn write_pp<'writer, W>(&self, w: &mut W) -> io::Result<()>
    where
        W: 'writer + Write,
    {
        match self {
            Value::Tape(tape) => tape.write_pp(w),
            Value::Value(value) => value.write_pp(w),
        }
    }
}