value_trait/
derived.rs

1use std::{borrow::Borrow, hash::Hash};
2
3use crate::{AccessError, TryTypeError, array::Array, object::Object};
4
5/// `try_as_*` access to scalar value types
6pub trait ValueTryAsScalar {
7    /// Tries to represent the value as a bool
8    /// # Errors
9    /// if the requested type doesn't match the actual type
10    fn try_as_bool(&self) -> Result<bool, TryTypeError>;
11
12    /// Tries to represent the value as a i128
13    /// # Errors
14    /// if the requested type doesn't match the actual type
15    fn try_as_i128(&self) -> Result<i128, TryTypeError>;
16
17    /// Tries to represent the value as an i64
18    /// # Errors
19    /// if the requested type doesn't match the actual type
20    fn try_as_i64(&self) -> Result<i64, TryTypeError>;
21    /// Tries to represent the value as an i32
22    /// # Errors
23    /// if the requested type doesn't match the actual type
24    fn try_as_i32(&self) -> Result<i32, TryTypeError>;
25
26    /// Tries to represent the value as an i16
27    /// # Errors
28    /// if the requested type doesn't match the actual type
29    fn try_as_i16(&self) -> Result<i16, TryTypeError>;
30    /// Tries to represent the value as an i8
31    /// # Errors
32    /// if the requested type doesn't match the actual type
33    fn try_as_i8(&self) -> Result<i8, TryTypeError>;
34
35    /// Tries to represent the value as an u128
36    /// # Errors
37    /// if the requested type doesn't match the actual type
38    fn try_as_u128(&self) -> Result<u128, TryTypeError>;
39
40    /// Tries to represent the value as an u64
41    /// # Errors
42    /// if the requested type doesn't match the actual type
43    fn try_as_u64(&self) -> Result<u64, TryTypeError>;
44
45    /// Tries to represent the value as an usize
46    /// # Errors
47    /// if the requested type doesn't match the actual type
48    fn try_as_usize(&self) -> Result<usize, TryTypeError>;
49
50    /// Tries to represent the value as an u32
51    /// # Errors
52    /// if the requested type doesn't match the actual type
53    fn try_as_u32(&self) -> Result<u32, TryTypeError>;
54
55    /// Tries to represent the value as an u16
56    /// # Errors
57    /// if the requested type doesn't match the actual type
58    fn try_as_u16(&self) -> Result<u16, TryTypeError>;
59
60    /// Tries to represent the value as an u8
61    /// # Errors
62    /// if the requested type doesn't match the actual type
63    fn try_as_u8(&self) -> Result<u8, TryTypeError>;
64
65    /// Tries to represent the value as a f64
66    /// # Errors
67    /// if the requested type doesn't match the actual type
68    fn try_as_f64(&self) -> Result<f64, TryTypeError>;
69
70    /// Tries to Casts the current value to a f64 if possible, this will turn integer
71    /// values into floats and error if it isn't possible
72    /// # Errors
73    /// if the requested type doesn't match the actual type
74    fn try_cast_f64(&self) -> Result<f64, TryTypeError>;
75
76    /// Tries to represent the value as a f32
77    /// # Errors
78    /// if the requested type doesn't match the actual type
79    fn try_as_f32(&self) -> Result<f32, TryTypeError>;
80
81    /// Tries to represent the value as a &str
82    /// # Errors
83    /// if the requested type doesn't match the actual type
84    fn try_as_str(&self) -> Result<&str, TryTypeError>;
85
86    /// Tries to represent the value as a Char
87    /// # Errors
88    /// if the requested type doesn't match the actual type
89    fn try_as_char(&self) -> Result<char, TryTypeError>;
90}
91
92/// Type checks for scalar values on a value
93pub trait TypedScalarValue {
94    /// returns true if the current value is null
95    #[must_use]
96    fn is_null(&self) -> bool;
97
98    /// returns true if the current value a floatingpoint number
99    #[must_use]
100    fn is_float(&self) -> bool;
101
102    /// returns true if the current value a integer number
103    #[must_use]
104    fn is_integer(&self) -> bool;
105
106    /// returns true if the current value a number either float or intege    
107    #[must_use]
108    fn is_number(&self) -> bool;
109
110    /// returns true if the current value a bool
111    #[must_use]
112    fn is_bool(&self) -> bool;
113
114    /// returns true if the current value can be represented as a i128
115    #[must_use]
116    fn is_i128(&self) -> bool;
117
118    /// returns true if the current value can be represented as a i64
119    #[must_use]
120    fn is_i64(&self) -> bool;
121
122    /// returns true if the current value can be represented as a i32
123    #[must_use]
124    fn is_i32(&self) -> bool;
125
126    /// returns true if the current value can be represented as a i16
127    #[must_use]
128    fn is_i16(&self) -> bool;
129
130    /// returns true if the current value can be represented as a i8
131    #[must_use]
132    fn is_i8(&self) -> bool;
133
134    /// returns true if the current value can be represented as a u128
135    #[must_use]
136    fn is_u128(&self) -> bool;
137
138    /// returns true if the current value can be represented as a u64
139    #[must_use]
140    fn is_u64(&self) -> bool;
141
142    /// returns true if the current value can be represented as a usize
143    #[must_use]
144    fn is_usize(&self) -> bool;
145
146    /// returns true if the current value can be represented as a u32    
147    #[must_use]
148    fn is_u32(&self) -> bool;
149
150    /// returns true if the current value can be represented as a u16
151    #[must_use]
152    fn is_u16(&self) -> bool;
153
154    /// returns true if the current value can be represented as a u8
155    #[must_use]
156    fn is_u8(&self) -> bool;
157
158    /// returns true if the current value can be represented as a f64
159    #[must_use]
160    fn is_f64(&self) -> bool;
161
162    /// returns true if the current value can be cast into a f64
163    #[must_use]
164    fn is_f64_castable(&self) -> bool;
165
166    /// returns true if the current value can be represented as a f64
167    #[must_use]
168    fn is_f32(&self) -> bool;
169    /// returns true if the current value can be represented as a str
170    #[must_use]
171    fn is_str(&self) -> bool;
172
173    /// returns true if the current value can be represented as a char
174    #[must_use]
175    fn is_char(&self) -> bool;
176}
177
178/// Type checks for array values on a value
179pub trait TypedArrayValue {
180    /// returns true if the current value can be represented as an array
181    #[must_use]
182    fn is_array(&self) -> bool;
183}
184
185/// Type checks for object values on a value
186pub trait TypedObjectValue {
187    /// returns true if the current value can be represented as an object
188    #[must_use]
189    fn is_object(&self) -> bool;
190}
191
192/// `try_as_*` access to array value types
193pub trait ValueTryAsArray {
194    /// The array structure
195    type Array: Array;
196
197    /// Tries to represent the value as an array and returns a reference to it
198    /// # Errors
199    /// if the requested type doesn't match the actual type
200    fn try_as_array(&self) -> Result<&Self::Array, TryTypeError>;
201}
202/// `try_as_*` access to object value types
203pub trait ValueTryAsObject {
204    /// The object structure
205    type Object: Object;
206
207    /// Tries to represent the value as an object and returns a reference to it
208    /// # Errors
209    /// if the requested type doesn't match the actual type
210    fn try_as_object(&self) -> Result<&Self::Object, TryTypeError>;
211}
212
213/// Access to a value as an object
214pub trait ValueObjectAccess {
215    /// The type for Objects
216    type Key: ?Sized;
217    /// The target for nested lookups
218    type Target;
219
220    /// Gets a ref to a value based on a key, returns `None` if the
221    /// current Value isn't an Object or doesn't contain the key
222    /// it was asked for.
223    fn get<Q>(&self, k: &Q) -> Option<&Self::Target>
224    where
225        Self::Key: Borrow<Q>,
226        Q: ?Sized + Hash + Eq + Ord;
227
228    /// Checks if a Value contains a given key. This will return
229    /// flase if Value isn't an object  
230    fn contains_key<Q>(&self, k: &Q) -> bool
231    where
232        Self::Key: Borrow<Q>,
233        Q: ?Sized + Hash + Eq + Ord;
234}
235
236/// Access to a value as an array
237pub trait ValueArrayAccess<I> {
238    /// The target for nested lookups
239    type Target: ?Sized;
240    /// Gets a ref to a value based on n index, returns `None` if the
241    /// current Value isn't an Array or doesn't contain the index
242    /// it was asked for.
243    #[must_use]
244    fn get_idx(&self, i: I) -> Option<&Self::Target>;
245}
246
247/// Access to scalar values in an object
248pub trait ValueObjectAccessAsScalar {
249    /// The type for Objects
250    type Key: ?Sized;
251    /// Tries to get an element of an object as a bool
252    fn get_bool<Q>(&self, k: &Q) -> Option<bool>
253    where
254        Self::Key: Borrow<Q>,
255        Q: ?Sized + Hash + Eq + Ord;
256
257    /// Tries to get an element of an object as a i128
258    fn get_i128<Q>(&self, k: &Q) -> Option<i128>
259    where
260        Self::Key: Borrow<Q>,
261        Q: ?Sized + Hash + Eq + Ord;
262
263    /// Tries to get an element of an object as a i64
264    fn get_i64<Q>(&self, k: &Q) -> Option<i64>
265    where
266        Self::Key: Borrow<Q>,
267        Q: ?Sized + Hash + Eq + Ord;
268
269    /// Tries to get an element of an object as a i32
270    fn get_i32<Q>(&self, k: &Q) -> Option<i32>
271    where
272        Self::Key: Borrow<Q>,
273        Q: ?Sized + Hash + Eq + Ord;
274
275    /// Tries to get an element of an object as a i16
276    fn get_i16<Q>(&self, k: &Q) -> Option<i16>
277    where
278        Self::Key: Borrow<Q>,
279        Q: ?Sized + Hash + Eq + Ord;
280
281    /// Tries to get an element of an object as a i8
282    fn get_i8<Q>(&self, k: &Q) -> Option<i8>
283    where
284        Self::Key: Borrow<Q>,
285        Q: ?Sized + Hash + Eq + Ord;
286
287    /// Tries to get an element of an object as a u128
288    fn get_u128<Q>(&self, k: &Q) -> Option<u128>
289    where
290        Self::Key: Borrow<Q>,
291        Q: ?Sized + Hash + Eq + Ord;
292
293    /// Tries to get an element of an object as a u64
294    fn get_u64<Q>(&self, k: &Q) -> Option<u64>
295    where
296        Self::Key: Borrow<Q>,
297        Q: ?Sized + Hash + Eq + Ord;
298
299    /// Tries to get an element of an object as a usize
300    fn get_usize<Q>(&self, k: &Q) -> Option<usize>
301    where
302        Self::Key: Borrow<Q>,
303        Q: ?Sized + Hash + Eq + Ord;
304
305    /// Tries to get an element of an object as a u32
306    fn get_u32<Q>(&self, k: &Q) -> Option<u32>
307    where
308        Self::Key: Borrow<Q>,
309        Q: ?Sized + Hash + Eq + Ord;
310
311    /// Tries to get an element of an object as a u16
312    fn get_u16<Q>(&self, k: &Q) -> Option<u16>
313    where
314        Self::Key: Borrow<Q>,
315        Q: ?Sized + Hash + Eq + Ord;
316
317    /// Tries to get an element of an object as a u8
318    fn get_u8<Q>(&self, k: &Q) -> Option<u8>
319    where
320        Self::Key: Borrow<Q>,
321        Q: ?Sized + Hash + Eq + Ord;
322
323    /// Tries to get an element of an object as a f64
324    fn get_f64<Q>(&self, k: &Q) -> Option<f64>
325    where
326        Self::Key: Borrow<Q>,
327        Q: ?Sized + Hash + Eq + Ord;
328
329    /// Tries to get an element of an object as a f32
330    fn get_f32<Q>(&self, k: &Q) -> Option<f32>
331    where
332        Self::Key: Borrow<Q>,
333        Q: ?Sized + Hash + Eq + Ord;
334
335    /// Tries to get an element of an object as a str
336    fn get_str<Q>(&self, k: &Q) -> Option<&str>
337    where
338        Self::Key: Borrow<Q>,
339        Q: ?Sized + Hash + Eq + Ord;
340}
341/// `try_as_*` access to scalar values in an object
342pub trait ValueObjectAccessTryAsScalar {
343    /// The type for Objects
344    type Key: ?Sized;
345
346    /// Tries to get an element of an object as a bool, returns
347    /// an error if it isn't bool
348    /// # Errors
349    /// if the requested type doesn't match the actual type or the value is not an object
350    fn try_get_bool<Q>(&self, k: &Q) -> Result<Option<bool>, TryTypeError>
351    where
352        Self::Key: Borrow<Q>,
353        Q: ?Sized + Hash + Eq + Ord;
354
355    /// Tries to get an element of an object as a i128, returns
356    /// an error if it isn't i128
357    /// # Errors
358    /// if the requested type doesn't match the actual type or the value is not an object
359    fn try_get_i128<Q>(&self, k: &Q) -> Result<Option<i128>, TryTypeError>
360    where
361        Self::Key: Borrow<Q>,
362        Q: ?Sized + Hash + Eq + Ord;
363
364    /// Tries to get an element of an object as a i64, returns
365    /// an error if it isn't a i64
366    /// # Errors
367    /// if the requested type doesn't match the actual type or the value is not an object
368    fn try_get_i64<Q>(&self, k: &Q) -> Result<Option<i64>, TryTypeError>
369    where
370        Self::Key: Borrow<Q>,
371        Q: ?Sized + Hash + Eq + Ord;
372
373    /// Tries to get an element of an object as a i32, returns
374    /// an error if it isn't a i32
375    /// # Errors
376    /// if the requested type doesn't match the actual type or the value is not an object
377    fn try_get_i32<Q>(&self, k: &Q) -> Result<Option<i32>, TryTypeError>
378    where
379        Self::Key: Borrow<Q>,
380        Q: ?Sized + Hash + Eq + Ord;
381
382    /// Tries to get an element of an object as a i16, returns
383    /// an error if it isn't a i16
384    /// # Errors
385    /// if the requested type doesn't match the actual type or the value is not an object
386    fn try_get_i16<Q>(&self, k: &Q) -> Result<Option<i16>, TryTypeError>
387    where
388        Self::Key: Borrow<Q>,
389        Q: ?Sized + Hash + Eq + Ord;
390
391    /// Tries to get an element of an object as a i8, returns
392    /// an error if it isn't a i8
393    /// # Errors
394    /// if the requested type doesn't match the actual type or the value is not an object
395    fn try_get_i8<Q>(&self, k: &Q) -> Result<Option<i8>, TryTypeError>
396    where
397        Self::Key: Borrow<Q>,
398        Q: ?Sized + Hash + Eq + Ord;
399
400    /// Tries to get an element of an object as a u128, returns
401    /// an error if it isn't a u128
402    /// # Errors
403    /// if the requested type doesn't match the actual type or the value is not an object
404    fn try_get_u128<Q>(&self, k: &Q) -> Result<Option<u128>, TryTypeError>
405    where
406        Self::Key: Borrow<Q>,
407        Q: ?Sized + Hash + Eq + Ord;
408
409    /// Tries to get an element of an object as a u64, returns
410    /// an error if it isn't a u64
411    /// # Errors
412    /// if the requested type doesn't match the actual type or the value is not an object
413    fn try_get_u64<Q>(&self, k: &Q) -> Result<Option<u64>, TryTypeError>
414    where
415        Self::Key: Borrow<Q>,
416        Q: ?Sized + Hash + Eq + Ord;
417
418    /// Tries to get an element of an object as a usize, returns
419    /// an error if it isn't a usize
420    /// # Errors
421    /// if the requested type doesn't match the actual type or the value is not an object
422    fn try_get_usize<Q>(&self, k: &Q) -> Result<Option<usize>, TryTypeError>
423    where
424        Self::Key: Borrow<Q>,
425        Q: ?Sized + Hash + Eq + Ord;
426
427    /// Tries to get an element of an object as a u32, returns
428    /// an error if it isn't a u32
429    /// # Errors
430    /// if the requested type doesn't match the actual type or the value is not an object
431    fn try_get_u32<Q>(&self, k: &Q) -> Result<Option<u32>, TryTypeError>
432    where
433        Self::Key: Borrow<Q>,
434        Q: ?Sized + Hash + Eq + Ord;
435
436    /// Tries to get an element of an object as a u16, returns
437    /// an error if it isn't a u16
438    /// # Errors
439    /// if the requested type doesn't match the actual type or the value is not an object
440    fn try_get_u16<Q>(&self, k: &Q) -> Result<Option<u16>, TryTypeError>
441    where
442        Self::Key: Borrow<Q>,
443        Q: ?Sized + Hash + Eq + Ord;
444
445    /// Tries to get an element of an object as a u8, returns
446    /// an error if it isn't a u8
447    /// # Errors
448    /// if the requested type doesn't match the actual type or the value is not an object
449    fn try_get_u8<Q>(&self, k: &Q) -> Result<Option<u8>, TryTypeError>
450    where
451        Self::Key: Borrow<Q>,
452        Q: ?Sized + Hash + Eq + Ord;
453
454    /// Tries to get an element of an object as a f64, returns
455    /// an error if it isn't a f64
456    /// # Errors
457    /// if the requested type doesn't match the actual type or the value is not an object
458    fn try_get_f64<Q>(&self, k: &Q) -> Result<Option<f64>, TryTypeError>
459    where
460        Self::Key: Borrow<Q>,
461        Q: ?Sized + Hash + Eq + Ord;
462
463    /// Tries to get an element of an object as a f32, returns
464    /// an error if it isn't a f32
465    /// # Errors
466    /// if the requested type doesn't match the actual type or the value is not an object
467    fn try_get_f32<Q>(&self, k: &Q) -> Result<Option<f32>, TryTypeError>
468    where
469        Self::Key: Borrow<Q>,
470        Q: ?Sized + Hash + Eq + Ord;
471
472    /// Tries to get an element of an object as a str, returns
473    /// an error if it isn't a str
474    /// # Errors
475    /// if the requested type doesn't match the actual type or the value is not an object
476    fn try_get_str<Q>(&self, k: &Q) -> Result<Option<&str>, TryTypeError>
477    where
478        Self::Key: Borrow<Q>,
479        Q: ?Sized + Hash + Eq + Ord;
480}
481
482/// Access to array values in an object
483pub trait ValueObjectAccessAsArray {
484    /// The type for Objects
485    type Key: ?Sized;
486    /// The array structure
487    type Array: Array;
488
489    /// Tries to get an element of an object as a array
490    fn get_array<Q>(&self, k: &Q) -> Option<&Self::Array>
491    where
492        Self::Key: Borrow<Q>,
493        Q: ?Sized + Hash + Eq + Ord;
494}
495
496/// Access to object values in an object
497pub trait ValueObjectAccessAsObject {
498    /// The type for Objects
499    type Key: ?Sized;
500    /// The object structure
501    type Object: Object;
502
503    /// Tries to get an element of an object as a object
504    fn get_object<Q>(&self, k: &Q) -> Option<&Self::Object>
505    where
506        Self::Key: Borrow<Q>,
507        Q: ?Sized + Hash + Eq + Ord;
508}
509
510/// `try_get_array` access to object values in an object
511pub trait ValueObjectAccessTryAsArray {
512    /// The type for Objects
513    type Key: ?Sized;
514    /// The array structure
515    type Array: Array;
516
517    /// Tries to get an element of an object as an array, returns
518    /// an error if it isn't a array
519    /// # Errors
520    /// if the requested type doesn't match the actual type or the value is not an object
521    fn try_get_array<Q>(&self, k: &Q) -> Result<Option<&Self::Array>, TryTypeError>
522    where
523        Self::Key: Borrow<Q>,
524        Q: ?Sized + Hash + Eq + Ord;
525}
526/// `try_get_object` access to object values in an object
527pub trait ValueObjectAccessTryAsObject {
528    /// The type for Objects
529    type Key: ?Sized;
530    /// The object structure
531    type Object: Object;
532
533    /// Tries to get an element of an object as an object, returns
534    /// an error if it isn't an object
535    ///
536    /// # Errors
537    /// if the requested type doesn't match the actual type or the value is not an object
538    fn try_get_object<Q>(&self, k: &Q) -> Result<Option<&Self::Object>, TryTypeError>
539    where
540        Self::Key: Borrow<Q>,
541        Q: ?Sized + Hash + Eq + Ord;
542}
543/// Mutatability for object like values
544pub trait MutableObject {
545    /// The type for Object Keys
546    type Key: ?Sized;
547    /// The type for Object Values
548    type Target;
549    /// The type for Objects
550    type Object;
551    /// Insert into this `Value` as an `Object`.
552    /// Will return an `AccessError::NotAnObject` if called
553    /// on a `Value` that isn't an object - otherwise will
554    /// behave the same as `HashMap::insert`
555    /// # Errors
556    ///
557    /// Will return `Err` if `self` is not an object.
558    fn insert<K, V>(
559        &mut self,
560        k: K,
561        v: V,
562    ) -> std::result::Result<Option<Self::Target>, AccessError>
563    where
564        Self::Key: From<K> + Hash + Eq,
565        V: Into<Self::Target>;
566
567    /// Tries to insert into this `Value` as an `Object`.
568    /// If the `Value` isn't an object this opoeration will
569    /// return `None` and have no effect.
570    #[inline]
571    fn try_insert<K, V>(&mut self, k: K, v: V) -> Option<Self::Target>
572    where
573        Self::Key: From<K> + Hash + Eq,
574        V: Into<Self::Target>,
575    {
576        self.insert(k, v).ok().flatten()
577    }
578
579    /// Remove from this `Value` as an `Object`.
580    /// Will return an `AccessError::NotAnObject` if called
581    /// on a `Value` that isn't an object - otherwise will
582    /// behave the same as `HashMap::remove`
583    /// # Errors
584    ///
585    /// Will return `Err` if `self` is not an Object.
586    fn remove<Q>(&mut self, k: &Q) -> std::result::Result<Option<Self::Target>, AccessError>
587    where
588        Self::Key: Borrow<Q>,
589        Q: ?Sized + Hash + Eq + Ord;
590
591    /// Tries to remove from this `Value` as an `Object`.
592    /// If the `Value` isn't an object this opoeration will
593    /// return `None` and have no effect.
594    #[inline]
595    fn try_remove<Q>(&mut self, k: &Q) -> Option<Self::Target>
596    where
597        Self::Key: Borrow<Q>,
598        Q: ?Sized + Hash + Eq + Ord,
599    {
600        self.remove(k).ok().flatten()
601    }
602
603    /// Same as `get` but returns a mutable ref instead
604    //    fn get_amut(&mut self, k: &str) -> Option<&mut Self>;
605    fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut Self::Target>
606    where
607        Self::Key: Borrow<Q>,
608        Q: ?Sized + Hash + Eq + Ord;
609}
610/// `try_as_*` access to a value as an object
611pub trait ValueObjectTryAccess {
612    /// The type for Objects
613    type Key: ?Sized;
614    /// The target for nested lookups
615    type Target;
616    /// Tries to get a value based on a key, returns a `TryTypeError` if the
617    /// current Value isn't an Object, returns `None` if the key isn't in the object
618    /// # Errors
619    /// if the value is not an object
620    fn try_get<Q>(&self, k: &Q) -> Result<Option<&Self::Target>, TryTypeError>
621    where
622        Self::Key: Borrow<Q>,
623        Q: ?Sized + Hash + Eq + Ord;
624}
625
626/// Mutatability for array like values
627pub trait MutableArray {
628    /// The type for Array Values
629    type Target;
630    /// Pushes to this `Value` as an `Array`.
631    /// Will return an `AccessError::NotAnArray` if called
632    /// on a `Value` that isn't an `Array` - otherwise will
633    /// behave the same as `Vec::push`
634    /// # Errors
635    ///
636    /// Will return `Err` if `self` is not an array.
637    fn push<V>(&mut self, v: V) -> std::result::Result<(), AccessError>
638    where
639        V: Into<Self::Target>;
640
641    /// Tries to push to a `Value` if as an `Array`.
642    /// This function will have no effect if `Value` is of
643    /// a different type
644    #[inline]
645    fn try_push<V>(&mut self, v: V)
646    where
647        V: Into<Self::Target>,
648    {
649        let _: Result<_, _> = self.push(v);
650    }
651
652    /// Pops from this `Value` as an `Array`.
653    /// Will return an `AccessError::NotAnArray` if called
654    /// on a `Value` that isn't an `Array` - otherwise will
655    /// behave the same as `Vec::pop`
656    /// # Errors
657    ///
658    /// Will return `Err` if `self` is not an array.
659    fn pop(&mut self) -> std::result::Result<Option<Self::Target>, AccessError>;
660
661    /// Tries to pop from a `Value` as an `Array`.
662    /// if the `Value` is any other type `None` will
663    /// always be returned
664    #[inline]
665    fn try_pop(&mut self) -> Option<Self::Target> {
666        self.pop().ok().flatten()
667    }
668}
669/// Mutatability for array like values
670pub trait MutableValueArrayAccess<I> {
671    /// The type for Array Values
672    type Target: ?Sized;
673
674    /// Same as `get_idx` but returns a mutable ref instead
675    fn get_idx_mut(&mut self, i: I) -> Option<&mut Self::Target>;
676}
677
678/// Access to a value as an array with error handling
679pub trait ValueArrayTryAccess {
680    /// The target for nested lookups
681    type Target: ?Sized;
682    /// Tries to get a value based on n index, returns a type error if the
683    /// current value isn't an Array, returns `None` if the index is out of bounds
684    /// # Errors
685    /// if the requested type doesn't match the actual type or the value is not an object
686    fn try_get_idx(&self, i: usize) -> Result<Option<&Self::Target>, TryTypeError>;
687}
688
689/// A trait that allows destructively turning a value into it's string representation
690pub trait ValueTryIntoString {
691    /// The type for Strings
692    type String;
693    /// Tries to turn the value into it's string representation
694    /// # Errors
695    /// if the requested type doesn't match the actual type
696    fn try_into_string(self) -> Result<Self::String, TryTypeError>;
697}
698
699/// A trait that specifies how to turn the Value `into` it's sub types with error handling
700pub trait ValueTryIntoArray {
701    /// The type for Arrays
702    type Array;
703
704    /// Tries to turn the value into it's array representation
705    /// # Errors
706    /// if the requested type doesn't match the actual type
707    fn try_into_array(self) -> Result<Self::Array, TryTypeError>;
708}
709/// A trait that specifies how to turn the Value `into` it's sub types with error handling
710pub trait ValueTryIntoObject {
711    /// The type for Objects
712    type Object;
713
714    /// Tries to turn the value into it's object representation
715    /// # Errors
716    /// if the requested type doesn't match the actual type
717    fn try_into_object(self) -> Result<Self::Object, TryTypeError>;
718}