structom 0.2.5

efficient data format for all needs
Documentation
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
use std::{
	collections::HashMap,
	fmt::{self, Display, Formatter},
	hash::{self, Hash},
	ops::{Index, IndexMut},
	slice::SliceIndex,
	sync::LazyLock,
};

use chrono::{DateTime, TimeDelta, Utc};
use num_bigint::BigInt;

use crate::stringify::{StringifyOptions, str_key, stringify};

/// type representing a structom value.
///
/// `Value`s are wrappers that represent any structom value, builtin or user defined.
///
/// they can be created manually or from source or binary, manipulated or converted to native types, and stringified or serialized.
///
/// ## api
/// `Value` can be created through different forms
/// ```
/// // manually
/// Value::from(1) // => Uint(1)
///
/// // from source
/// parse("1", &ParseOptions::default(), &VoidProvider{}); // => Uint(1)
///
/// // from binary
/// decode(&[0, 16, 1], &VoidProvider{}); // => Uint(1)
/// ```
///
/// `Value` has different methods for manipulation its value.
/// ```
/// let value = Value::Uint(1);
///
/// // test type
/// value.is_uint(); // => true
///
/// // get ref to inner value
/// value.as_uint(); // => Some(1)
///
/// // compare inner value
/// value == 1; // => true
///
/// // index for array and map
/// Value::from(vec![1, 2, 3])[1] // => Uint(2)
/// ```
///
/// `Value` can be transformed into other forms.
/// ```
/// let value = Value::Uint(1);
///
/// // convert to native type
///	value.cast<u64>(); // => Ok(1)
///
/// // stringify into object notation
/// stringify(value, &StringifyOptions::default()); // => "1"
///
/// // encode into binary
/// encode(value); // => [0, 16, 1]
/// ```
///
/// ## representation
/// builtin types are represented through their respective variant.
///
/// structs are represented through the `Map` variant containing the struct fields.
///
/// enums are represented by the `UnitVar` case it is unit variant.       
/// else they are represented by a `Map` variant containing the fields, with a special key [`Key::enum_variant_key()`] storing the variant name.
///
/// for metadata wrapped types, they are represented by a `Map` variant containing the metadata with their values, with special keys: [`Key::has_meta_key()`] of value `true` and [`Key::inner_key()`] containing the wrapped value.
#[derive(Debug, Clone)]
pub enum Value {
	/// boolean value, types: `bool`.
	Bool(bool),
	/// signed integer value, types: `i8`, `i16`, `i32`, `i64` `vint`.
	Int(i64),
	/// unsigned integer value, types: `u8`, `u16`, `u32`, `u64` `vuint`.
	Uint(u64),
	/// big integer value, types: `bint`.
	BigInt(BigInt),
	/// floating point value, types: `f32`, `f64`
	Float(f64),
	/// string value, types: `str`.
	Str(String),
	/// instance value, types: `inst`, `instN`.
	Inst(DateTime<Utc>),
	/// duration value, types: `dur`.
	Dur(TimeDelta),
	/// uuid value, types: `uuid`.
	UUID([u8; 16]),
	/// array value, types: `arr`.
	Arr(Vec<Value>),
	/// map value, types: `map`, structs, enums with fields.
	Map(Box<HashMap<Key, Value>>),
	/// unit variant enum, types: `enum`.
	UnitVar(String),
}

/// a type used as [`Value::Map`] key.
///
/// `Key` is a subset of [`Value`] for types that can be used a keys for `Value::Map`.
///
/// it supports all the api supported by its underlaying variants, and can be converted from and into `Value`.
///
/// ## example
/// ```
/// let map = Value::Map(HashMap::new())
/// 	.insert(Key::from("some_key"), Value::Uint(1)); // => {"some_key": 1}
/// ```
#[derive(Debug, Clone, Hash, Eq)]
pub enum Key {
	/// boolean value, types: `bool`.
	Bool(bool),
	/// signed integer value, types: `i8`, `i16`, `i32`, `i64` `vint`.
	Int(i64),
	/// unsigned integer value, types: `u8`, `u16`, `u32`, `u64` `vuint`.
	Uint(u64),
	/// big integer value, types: `bint`.
	BigInt(BigInt),
	/// string value, types: `str`.
	Str(String),
	/// instance value, types: `inst`, `instN`.
	Inst(DateTime<Utc>),
	/// duration value, types: `dur`.
	Dur(TimeDelta),
	/// uuid value, types: `uuid`.
	UUID([u8; 16]),
}

impl Default for Value {
	fn default() -> Self {
		Value::Uint(0)
	}
}
impl Default for Key {
	fn default() -> Self {
		Key::Uint(0)
	}
}

pub static ENUM_VARIANT_KEY: LazyLock<Key> =
	LazyLock::new(|| Key::Str("$enum_variant".to_string()));
pub static HAS_META_KEY: LazyLock<Key> = LazyLock::new(|| Key::Str("$has_meta".to_string()));
pub static INNER_KEY: LazyLock<Key> = LazyLock::new(|| Key::Str("$value".to_string()));
impl Key {
	/// the enum variant key in an enum map
	pub fn enum_variant_key() -> &'static Key {
		&ENUM_VARIANT_KEY
	}
	/// the has metadata key in a metadata wrapped type
	pub fn has_meta_key() -> &'static Key {
		&HAS_META_KEY
	}
	/// the inner value key of a metadata wrapped type
	pub fn inner_key() -> &'static Key {
		&INNER_KEY
	}
}

macro_rules! is_impl {
	($enum:ident, $(($ty:ident, $met:ident)),+) => {
		$(pub fn $met(&self) -> bool {
			match self {
				$enum::$ty(_) => true,
				_ => false,
			}
		})+
	};
}

/// `is_T() -> bool`: whether the inner value is of type `T`.
impl Value {
	is_impl!(Value, (Bool, is_bool), (Uint, is_uint), (Int, is_int), (Str, is_str));
	is_impl!(Value, (BigInt, is_bigint), (Float, is_float), (Inst, is_inst), (Dur, is_dur));
	is_impl!(Value, (UUID, is_uuid), (Arr, is_array), (Map, is_map), (UnitVar, is_unit_variant));

	/// whether the inner value is an enum
	pub fn is_enum(&self) -> bool {
		match self {
			Value::UnitVar(_) => true,
			Value::Map(map) => map.contains_key(&ENUM_VARIANT_KEY),
			_ => false,
		}
	}
	/// whether the inner value is a metadata wrapped type
	pub fn has_meta(&self) -> bool {
		match self {
			Value::Map(map) => map.contains_key(&HAS_META_KEY),
			_ => false,
		}
	}
}

/// `is_T() -> bool`: whether the inner value is of type `T`.
impl Key {
	is_impl!(Key, (Bool, is_bool), (Uint, is_uint), (Int, is_int), (Str, is_str));
	is_impl!(Key, (BigInt, is_bigint), (Inst, is_inst), (Dur, is_dur), (UUID, is_uuid));
}

impl Value {
	/// convert `Value` into [`Key`]
	pub fn into_key(self) -> Option<Key> {
		self.try_into().ok()
	}
	/// get name of the wrapped enum variant, if not enum return `None`.
	pub fn enum_variant(&self) -> Option<&str> {
		match self {
			Value::UnitVar(str) => Some(str),
			Value::Map(map) => map.get(&ENUM_VARIANT_KEY)?.as_str(),
			_ => None,
		}
	}
	/// get the inner value of a metadata wrapped type, else return self.
	pub fn inner(&self) -> &Value {
		match &self {
			Value::Map(map) if map.contains_key(&HAS_META_KEY) => &map[&INNER_KEY],
			_ => self,
		}
	}
	/// get mut ref to the inner value of a metadata wrapped type, else return self.
	pub fn inner_mut(&mut self) -> &mut Value {
		if let Value::Map(map) = self {
			if map.contains_key(&HAS_META_KEY) {
				return map.get_mut(&INNER_KEY).unwrap();
			}
			panic!("actualy this doesnt work since of dark magic, if you can fix it, please do")
		}
		self
	}
	/// unwrap the inner value of a metadata wrapped type, else return self.
	pub fn into_inner(self) -> Value {
		match self {
			Value::Map(mut map) if map.contains_key(&HAS_META_KEY) => {
				map.remove(&INNER_KEY).unwrap()
			}
			_ => self,
		}
	}
}

impl TryFrom<Value> for Key {
	type Error = ();
	fn try_from(value: Value) -> Result<Self, Self::Error> {
		match value {
			Value::Bool(b) => Ok(Key::Bool(b)),
			Value::Int(i) => Ok(Key::Int(i)),
			Value::Uint(i) => Ok(Key::Uint(i)),
			Value::BigInt(i) => Ok(Key::BigInt(i)),
			Value::Str(s) => Ok(Key::Str(s)),
			Value::Inst(i) => Ok(Key::Inst(i)),
			Value::Dur(d) => Ok(Key::Dur(d)),
			Value::UUID(u) => Ok(Key::UUID(u)),
			_ => Err(()),
		}
	}
}
impl From<Key> for Value {
	fn from(key: Key) -> Self {
		match key {
			Key::Bool(b) => Value::Bool(b),
			Key::Int(i) => Value::Int(i),
			Key::Uint(i) => Value::Uint(i),
			Key::BigInt(i) => Value::BigInt(i),
			Key::Str(s) => Value::Str(s),
			Key::Inst(i) => Value::Inst(i),
			Key::Dur(d) => Value::Dur(d),
			Key::UUID(u) => Value::UUID(u),
		}
	}
}

macro_rules! from_impl {
	($enum:ident, $(($ty:ty, $var:ident)),+) => {
		$(impl From<$ty> for $enum {
			fn from(v: $ty) -> Self {
				$enum::$var(v)
			}
		})+
	};
	($enum:ident, $var:ident, $as:ident, [$($ty:ident),+]) => {
		$(impl From<$ty> for $enum {
			fn from(v: $ty) -> Self {
				$enum::$var(v as $as)
			}
		})+
	};
}

from_impl!(Value, (bool, Bool), (i64, Int), (u64, Uint), (f64, Float));
from_impl!(Value, (String, Str), (DateTime<Utc>, Inst), (TimeDelta, Dur));
from_impl!(Value, ([u8; 16], UUID), (BigInt, BigInt));

from_impl!(Value, Uint, u64, [u8, u16, u32, usize]);
from_impl!(Value, Int, i64, [i8, i16, i32, isize]);
from_impl!(Value, Float, f64, [f32]);

from_impl!(Key, (bool, Bool), (i64, Int), (u64, Uint), (String, Str), (BigInt, BigInt));
from_impl!(Key, (DateTime<Utc>, Inst), (TimeDelta, Dur), ([u8; 16], UUID));

from_impl!(Key, Uint, u64, [u8, u16, u32, usize]);
from_impl!(Key, Int, i64, [i8, i16, i32, isize]);

impl From<&str> for Value {
	fn from(s: &str) -> Self {
		Value::Str(s.to_string())
	}
}
impl From<&str> for Key {
	fn from(s: &str) -> Self {
		Key::Str(s.to_string())
	}
}

impl<T: Into<Value>> From<Vec<T>> for Value {
	fn from(v: Vec<T>) -> Self {
		Value::Arr(v.into_iter().map(|v| v.into()).collect())
	}
}
impl<K: Into<Key>, V: Into<Value>> From<HashMap<K, V>> for Value {
	fn from(m: HashMap<K, V>) -> Self {
		Value::Map(Box::new(m.into_iter().map(|(k, v)| (k.into(), v.into())).collect()))
	}
}

impl Value {
	pub fn map_from<K: Into<Key>, V: Into<Value>, I: IntoIterator<Item = (K, V)>>(
		iter: I,
	) -> Value {
		Value::Map(Box::new(iter.into_iter().map(|(k, v)| (k.into(), v.into())).collect()))
	}
}

macro_rules! try_into_impl {
	($enum:ident, $(($ty:ty, $var:ident)),+) => {
		$(impl TryInto<$ty> for $enum {
			type Error = ();
			fn try_into(self) -> Result<$ty, Self::Error> {
				match self {
					$enum::$var(v) => Ok(v as $ty),
					_ => Err(()),
				}
			}
		})+
	};
}
macro_rules! try_into_int_impl {
	($enum:ident, [$($ty:ty),+]) => {
		$(impl TryInto<$ty> for $enum {
			type Error = ();
			fn try_into(self) -> Result<$ty, Self::Error> {
				match self {
					$enum::Int(v) => Ok(v.try_into().map_err(|_| ())?),
					$enum::Uint(v) => Ok(v.try_into().map_err(|_| ())?),
					_ => Err(()),
				}
			}
		})+
	};
}
try_into_impl!(Value, (bool, Bool), (u64, Uint), (i64, Int), (f64, Float), (f32, Float));
try_into_impl!(Value, (String, Str), (DateTime<Utc>, Inst), (TimeDelta, Dur));
try_into_impl!(Value, ([u8; 16], UUID), (BigInt, BigInt));

try_into_int_impl!(Value, [u8, u16, u32, usize, i8, i16, i32, isize]);

try_into_impl!(Key, (bool, Bool), (u64, Uint), (i64, Int), (String, Str), (BigInt, BigInt));
try_into_impl!(Key, (DateTime<Utc>, Inst), (TimeDelta, Dur), ([u8; 16], UUID));
try_into_int_impl!(Key, [u8, u16, u32, usize, i8, i16, i32, isize]);

impl<T> TryInto<Vec<T>> for Value
where
	Value: TryInto<T>,
{
	type Error = ();
	fn try_into(self) -> Result<Vec<T>, Self::Error> {
		match self {
			Value::Arr(v) => {
				let mut vec = Vec::<T>::with_capacity(v.len());
				for item in v {
					vec.push(item.try_into().map_err(|_| ())?);
				}
				Ok(vec)
			}
			_ => Err(()),
		}
	}
}
impl<K, V> TryInto<HashMap<K, V>> for Value
where
	Key: TryInto<K>,
	Value: TryInto<V>,
	K: Eq + hash::Hash,
{
	type Error = ();
	fn try_into(self) -> Result<HashMap<K, V>, Self::Error> {
		match self {
			Value::Map(m) => {
				let mut map = HashMap::<K, V>::with_capacity(m.len());
				for (k, v) in *m {
					map.insert(k.try_into().map_err(|_| ())?, v.try_into().map_err(|_| ())?);
				}
				Ok(map)
			}
			_ => Err(()),
		}
	}
}

impl Value {
	/// cast value into `T`
	pub fn cast<T>(self) -> Option<T>
	where
		Value: TryInto<T>,
	{
		TryInto::try_into(self).ok()
	}
}
impl Key {
	/// cast key into `T`
	pub fn cast<T>(self) -> Option<T>
	where
		Key: TryInto<T>,
	{
		TryInto::try_into(self).ok()
	}
}

macro_rules! as_impl {
	($enum:ident, $(($ty:ty, $met:ident, $var:ident)),+) => {
		$(pub fn $met(&self) -> Option<$ty> {
			match self {
				$enum::$var(v) => Some(*v),
				_ => None,
			}
		})+
	};
}
macro_rules! as_ref_impl {
	($enum:ident, $(($ty:ty, $met:ident, $var:ident)),+) => {
		$(pub fn $met(&self) -> Option<&$ty> {
			match self {
				$enum::$var(v) => Some(v),
				_ => None,
			}
		})+
	};
}
macro_rules! as_mut_impl {
	($enum:ident, $(($ty:ty, $met:ident, $var:ident)),+) => {
		$(pub fn $met(&mut self) -> Option<&mut $ty> {
			match self {
				$enum::$var(v) => Some(v),
				_ => None,
			}
		})+
	};
}

/// `as_T() -> Option<T>`: get copy / reference of the inner value if it is of type `T`, else `None`.
///
/// `as_mut_T() -> Option<T>`: get mutable reference to the inner value if it is of type `T`, else `None`.
impl Value {
	as_impl!(Value, (bool, as_bool, Bool), (i64, as_int, Int), (u64, as_uint, Uint));
	as_impl!(Value, (f64, as_float, Float), ([u8; 16], as_uuid, UUID));
	as_impl!(Value, (TimeDelta, as_dur, Dur), (DateTime<Utc>, as_inst, Inst));
	as_ref_impl!(Value, (str, as_str, Str), ([Value], as_slice, Arr));
	as_ref_impl!(Value, (BigInt, as_bigint, BigInt), (HashMap<Key, Value>, as_map, Map));
	as_mut_impl!(Value, (Vec<Value>, as_vec_mut, Arr), (HashMap<Key, Value>, as_map_mut, Map));
}

/// `as_T() -> Option<T>`: get copy / reference of the inner value if it is of type `T`, else `None`.
impl Key {
	as_impl!(Key, (bool, as_bool, Bool), (i64, as_int, Int), ([u8; 16], as_uuid, UUID));
	as_impl!(Key, (TimeDelta, as_dur, Dur), (DateTime<Utc>, as_inst, Inst), (u64, as_uint, Uint));
	as_ref_impl!(Key, (str, as_str, Str), (BigInt, as_bigint, BigInt));
}

impl PartialEq<Value> for Value {
	fn eq(&self, other: &Value) -> bool {
		match (self, other) {
			(Value::Bool(a), Value::Bool(b)) => *a == *b,
			(Value::Int(a), Value::Int(b)) => *a == *b,
			(Value::Uint(a), Value::Uint(b)) => *a == *b,
			(Value::Int(a), Value::Uint(b)) => *a == *b as _,
			(Value::Uint(a), Value::Int(b)) => *a == *b as _,
			(Value::BigInt(a), Value::BigInt(b)) => a == b,
			(Value::Float(a), Value::Float(b)) => *a == *b,
			(Value::Str(a), Value::Str(b)) => a == b,
			(Value::Inst(a), Value::Inst(b)) => a == b,
			(Value::Dur(a), Value::Dur(b)) => a == b,
			(Value::UUID(a), Value::UUID(b)) => a == b,
			(Value::Arr(a), Value::Arr(b)) => a == b,
			(Value::Map(a), Value::Map(b)) => a == b,
			(Value::UnitVar(a), Value::UnitVar(b)) => a == b,
			_ => false,
		}
	}
}
impl PartialEq<Key> for Key {
	fn eq(&self, other: &Key) -> bool {
		match (self, other) {
			(Key::Bool(a), Key::Bool(b)) => *a == *b,
			(Key::Int(a), Key::Int(b)) => *a == *b,
			(Key::Uint(a), Key::Uint(b)) => *a == *b,
			(Key::Int(a), Key::Uint(b)) => *a == *b as _,
			(Key::Uint(a), Key::Int(b)) => *a == *b as _,
			(Key::BigInt(a), Key::BigInt(b)) => a == b,
			(Key::Str(a), Key::Str(b)) => a == b,
			(Key::Inst(a), Key::Inst(b)) => a == b,
			(Key::Dur(a), Key::Dur(b)) => a == b,
			(Key::UUID(a), Key::UUID(b)) => a == b,
			_ => false,
		}
	}
}

impl PartialEq<Key> for Value {
	fn eq(&self, other: &Key) -> bool {
		match (self, other) {
			(Value::Bool(a), Key::Bool(b)) => a == b,
			(Value::Int(a), Key::Int(b)) => a == b,
			(Value::Uint(a), Key::Uint(b)) => a == b,
			(Value::BigInt(a), Key::BigInt(b)) => a == b,
			(Value::Str(a), Key::Str(b)) => a == b,
			(Value::Inst(a), Key::Inst(b)) => a == b,
			(Value::Dur(a), Key::Dur(b)) => a == b,
			(Value::UUID(a), Key::UUID(b)) => a == b,
			_ => false,
		}
	}
}

macro_rules! eq_impl {
	($enum:ident, $(($ty:ty, $var:ident)),+) => {
		$(impl PartialEq<$ty> for $enum {
			fn eq(&self, other: &$ty) -> bool {
				match self {
					$enum::$var(v) => v == other,
					_ => false,
				}
			}
		})+
	};
}
macro_rules! eq_int_impl {
	($enum:ident, [$($ty:ty),+]) => {
		$(impl PartialEq<$ty> for $enum {
			fn eq(&self, other: &$ty) -> bool {
				match self {
					$enum::Uint(v) => *v as $ty == *other,
					$enum::Int(v) => *v as $ty == *other,
					_ => false,
				}
			}
		}
		impl PartialEq<$ty> for &$enum {
			fn eq(&self, other: &$ty) -> bool {
				match self {
					$enum::Uint(v) => *v as $ty == *other,
					$enum::Int(v) => *v as $ty == *other,
					_ => false,
				}
			}
		})+
	};
}

eq_impl!(Value, (bool, Bool), (DateTime<Utc>, Inst), (TimeDelta, Dur), (f64, Float));
eq_impl!(Value, (&str, Str), (String, Str), ([u8; 16], UUID), (BigInt, BigInt));

eq_int_impl!(Value, [u8, u16, u32, u64, usize]);
eq_int_impl!(Value, [i8, i16, i32, i64, isize]);

eq_impl!(Key, (bool, Bool), (DateTime<Utc>, Inst), (TimeDelta, Dur), (String, Str), (&str, Str));
eq_impl!(Key, (BigInt, BigInt), ([u8; 16], UUID));

eq_int_impl!(Key, [u8, u16, u32, u64, usize]);
eq_int_impl!(Key, [i8, i16, i32, i64, isize]);

impl<T> PartialEq<Vec<T>> for Value
where
	Value: PartialEq<T>,
{
	fn eq(&self, other: &Vec<T>) -> bool {
		match self {
			Value::Arr(a) => a == other,
			_ => false,
		}
	}
}

impl<I: SliceIndex<[Value]>> Index<I> for Value {
	type Output = <I as SliceIndex<[Value]>>::Output;
	fn index(&self, index: I) -> &Self::Output {
		match self {
			Value::Arr(a) => &a[index],
			_ => panic!(),
		}
	}
}
impl<I: SliceIndex<[Value]>> IndexMut<I> for Value {
	fn index_mut(&mut self, index: I) -> &mut Self::Output {
		match self {
			Value::Arr(a) => &mut a[index],
			_ => panic!(),
		}
	}
}
impl Index<&Key> for Value {
	type Output = Value;
	fn index(&self, index: &Key) -> &Self::Output {
		match self {
			Value::Map(m) => m.get(index).unwrap(),
			_ => panic!(),
		}
	}
}
impl IndexMut<&Key> for Value {
	fn index_mut(&mut self, index: &Key) -> &mut Self::Output {
		match self {
			Value::Map(m) => match m.contains_key(index) {
				true => m.get_mut(index).unwrap(),
				false => m.entry(index.clone()).or_insert(Value::default()),
			},
			_ => panic!(),
		}
	}
}
impl Value {
	/// get an item by index if value is an array, else return `None`.
	pub fn index_arr<I: SliceIndex<[Value]>>(
		&self, index: I,
	) -> Option<&<I as SliceIndex<[Value]>>::Output> {
		match self {
			Value::Arr(a) => a.get(index),
			_ => None,
		}
	}
	/// get a mutable reference to an item by index if value is an array, else return `None`.
	pub fn index_arr_mut<I: SliceIndex<[Value]>>(
		&mut self, index: I,
	) -> Option<&mut <I as SliceIndex<[Value]>>::Output> {
		match self {
			Value::Arr(a) => a.get_mut(index),
			_ => None,
		}
	}
	/// get an item by key if value is a map, else return `None`.
	pub fn index_map(&self, key: &Key) -> Option<&Value> {
		match self {
			Value::Map(m) => m.get(key),
			_ => None,
		}
	}
	/// get a mutable reference to an item by key if value is a map, else return `None`.
	pub fn index_map_mut(&mut self, key: &Key) -> Option<&mut Value> {
		match self {
			Value::Map(m) => m.get_mut(key),
			_ => None,
		}
	}
}

impl Display for Value {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		if f.alternate() {
			stringify(self, &StringifyOptions { ident: "\t", ..Default::default() }).fmt(f)
		} else {
			stringify(self, &StringifyOptions::default()).fmt(f)
		}
	}
}

impl Display for Key {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		str_key(self).fmt(f)
	}
}