amadeus_types/
value.rs

1//! Implement [`Record`] for [`Value`] – an enum representing any valid Parquet value.
2
3#![allow(clippy::type_complexity)]
4
5use fxhash::FxBuildHasher;
6use hashlink::LinkedHashMap;
7use recycle::VecExt;
8use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize};
9use std::{
10	cmp::Ordering, collections::HashMap, convert::TryInto, fmt, fmt::Debug, hash::{BuildHasher, Hash, Hasher}, iter::FromIterator, sync::Arc
11};
12
13use crate::list::ListVec;
14
15use super::{
16	AmadeusOrd, Bson, Data, Date, DateTime, DateTimeWithoutTimezone, DateWithoutTimezone, Decimal, Downcast, DowncastError, DowncastFrom, Enum, Group, IpAddr, Json, List, Time, TimeWithoutTimezone, Timezone, Url, ValueRequired, Webpage
17};
18
19#[derive(Clone, PartialEq, Debug)]
20pub enum SchemaIncomplete {
21	Bool,
22	U8,
23	I8,
24	U16,
25	I16,
26	U32,
27	I32,
28	U64,
29	I64,
30	F32,
31	F64,
32	Date,
33	DateWithoutTimezone,
34	Time,
35	TimeWithoutTimezone,
36	DateTime,
37	DateTimeWithoutTimezone,
38	Timezone,
39	Decimal,
40	Bson,
41	String,
42	Json,
43	Enum,
44	List(Box<SchemaIncomplete>),
45	Map(Box<(SchemaIncomplete, SchemaIncomplete)>),
46	Group(
47		Option<(
48			Vec<SchemaIncomplete>,
49			Option<Arc<LinkedHashMap<String, usize, FxBuildHasher>>>,
50		)>,
51	),
52	Option(Box<SchemaIncomplete>),
53}
54
55#[derive(Clone, PartialEq, Debug)]
56pub enum Schema {
57	Bool,
58	U8,
59	I8,
60	U16,
61	I16,
62	U32,
63	I32,
64	U64,
65	I64,
66	F32,
67	F64,
68	Date,
69	DateWithoutTimezone,
70	Time,
71	TimeWithoutTimezone,
72	DateTime,
73	DateTimeWithoutTimezone,
74	Timezone,
75	Decimal,
76	Bson,
77	String,
78	Json,
79	Enum,
80	List(Box<Schema>),
81	Map(Box<(Schema, Schema)>),
82	Group(
83		Vec<Schema>,
84		Option<Arc<LinkedHashMap<String, usize, FxBuildHasher>>>,
85	),
86	Option(Box<Schema>),
87}
88
89/// Represents any valid Amadeus value.
90#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
91pub enum Value {
92	// Primitive types
93	/// Boolean value (`true`, `false`).
94	Bool(bool),
95	/// Signed integer INT_8.
96	U8(u8),
97	/// Signed integer INT_16.
98	I8(i8),
99	/// Signed integer INT_32.
100	U16(u16),
101	/// Signed integer INT_64.
102	I16(i16),
103	/// Unsigned integer UINT_8.
104	U32(u32),
105	/// Unsigned integer UINT_16.
106	I32(i32),
107	/// Unsigned integer UINT_32.
108	U64(u64),
109	/// Unsigned integer UINT_64.
110	I64(i64),
111	/// IEEE 32-bit floating point value.
112	F32(f32),
113	/// IEEE 64-bit floating point value.
114	F64(f64),
115	/// Date without a time of day, stores the number of days from the Unix epoch, 1
116	/// January 1970.
117	Date(Date),
118	/// Date without a time of day, stores the number of days from the Unix epoch, 1
119	/// January 1970.
120	DateWithoutTimezone(DateWithoutTimezone),
121	/// Time of day, stores the number of microseconds from midnight.
122	Time(Time),
123	/// Time of day, stores the number of microseconds from midnight.
124	TimeWithoutTimezone(TimeWithoutTimezone),
125	/// Milliseconds from the Unix epoch, 1 January 1970.
126	DateTime(DateTime),
127	/// Milliseconds from the Unix epoch, 1 January 1970.
128	DateTimeWithoutTimezone(DateTimeWithoutTimezone),
129	/// Timezone.
130	Timezone(Timezone),
131	/// Decimal value.
132	Decimal(Decimal),
133	/// BSON binary value.
134	Bson(Bson),
135	/// UTF-8 encoded character string.
136	String(String),
137	/// JSON string.
138	Json(Json),
139	/// Enum string.
140	Enum(Enum),
141	/// URL
142	Url(Url),
143	/// Webpage
144	Webpage(Webpage<'static>),
145	/// Ip Address
146	IpAddr(IpAddr),
147
148	// Complex types
149	/// List of elements.
150	List(List<Value>),
151	/// Map of key-value pairs.
152	Map(HashMap<Value, Value>),
153	/// Struct, child elements are tuples of field-value pairs.
154	Group(Group),
155	/// Optional element.
156	Option(#[serde(with = "optional_value")] Option<ValueRequired>),
157}
158
159mod optional_value {
160	use super::{Value, ValueRequired};
161	use serde::{Deserialize, Deserializer, Serializer};
162
163	pub fn serialize<S>(t: &Option<ValueRequired>, serializer: S) -> Result<S::Ok, S::Error>
164	where
165		S: Serializer,
166	{
167		match t {
168			Some(value) => match value {
169				ValueRequired::Bool(value) => serializer.serialize_some(&value),
170				ValueRequired::U8(value) => serializer.serialize_some(&value),
171				ValueRequired::I8(value) => serializer.serialize_some(&value),
172				ValueRequired::U16(value) => serializer.serialize_some(&value),
173				ValueRequired::I16(value) => serializer.serialize_some(&value),
174				ValueRequired::U32(value) => serializer.serialize_some(&value),
175				ValueRequired::I32(value) => serializer.serialize_some(&value),
176				ValueRequired::U64(value) => serializer.serialize_some(&value),
177				ValueRequired::I64(value) => serializer.serialize_some(&value),
178				ValueRequired::F32(value) => serializer.serialize_some(&value),
179				ValueRequired::F64(value) => serializer.serialize_some(&value),
180				ValueRequired::Date(value) => serializer.serialize_some(&value),
181				ValueRequired::DateWithoutTimezone(value) => serializer.serialize_some(&value),
182				ValueRequired::Time(value) => serializer.serialize_some(&value),
183				ValueRequired::TimeWithoutTimezone(value) => serializer.serialize_some(&value),
184				ValueRequired::DateTime(value) => serializer.serialize_some(&value),
185				ValueRequired::DateTimeWithoutTimezone(value) => serializer.serialize_some(&value),
186				ValueRequired::Timezone(value) => serializer.serialize_some(&value),
187				ValueRequired::Decimal(value) => serializer.serialize_some(&value),
188				ValueRequired::Bson(value) => serializer.serialize_some(&value),
189				ValueRequired::String(value) => serializer.serialize_some(&value),
190				ValueRequired::Json(value) => serializer.serialize_some(&value),
191				ValueRequired::Enum(value) => serializer.serialize_some(&value),
192				ValueRequired::Url(value) => serializer.serialize_some(&value),
193				ValueRequired::Webpage(value) => serializer.serialize_some(&value),
194				ValueRequired::IpAddr(value) => serializer.serialize_some(&value),
195				ValueRequired::List(value) => serializer.serialize_some(&value),
196				ValueRequired::Map(value) => serializer.serialize_some(&value),
197				ValueRequired::Group(value) => serializer.serialize_some(&value),
198			},
199			None => serializer.serialize_none(),
200		}
201	}
202	pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<ValueRequired>, D::Error>
203	where
204		D: Deserializer<'de>,
205	{
206		Option::<Value>::deserialize(deserializer).map(|x| x.map(Into::into).unwrap())
207	}
208}
209
210#[allow(clippy::derive_hash_xor_eq)]
211impl Hash for Value {
212	fn hash<H: Hasher>(&self, state: &mut H) {
213		match self {
214			Self::Bool(value) => {
215				0_u8.hash(state);
216				value.hash(state);
217			}
218			Self::U8(value) => {
219				1_u8.hash(state);
220				value.hash(state);
221			}
222			Self::I8(value) => {
223				2_u8.hash(state);
224				value.hash(state);
225			}
226			Self::U16(value) => {
227				3_u8.hash(state);
228				value.hash(state);
229			}
230			Self::I16(value) => {
231				4_u8.hash(state);
232				value.hash(state);
233			}
234			Self::U32(value) => {
235				5_u8.hash(state);
236				value.hash(state);
237			}
238			Self::I32(value) => {
239				6_u8.hash(state);
240				value.hash(state);
241			}
242			Self::U64(value) => {
243				7_u8.hash(state);
244				value.hash(state);
245			}
246			Self::I64(value) => {
247				8_u8.hash(state);
248				value.hash(state);
249			}
250			Self::F32(_value) => {
251				9_u8.hash(state);
252			}
253			Self::F64(_value) => {
254				10_u8.hash(state);
255			}
256			Self::Date(value) => {
257				11_u8.hash(state);
258				value.hash(state);
259			}
260			Self::DateWithoutTimezone(value) => {
261				11_u8.hash(state);
262				value.hash(state);
263			}
264			Self::Time(value) => {
265				12_u8.hash(state);
266				value.hash(state);
267			}
268			Self::TimeWithoutTimezone(value) => {
269				12_u8.hash(state);
270				value.hash(state);
271			}
272			Self::DateTime(value) => {
273				13_u8.hash(state);
274				value.hash(state);
275			}
276			Self::DateTimeWithoutTimezone(value) => {
277				13_u8.hash(state);
278				value.hash(state);
279			}
280			Self::Timezone(value) => {
281				13_u8.hash(state);
282				value.hash(state);
283			}
284			Self::Decimal(_value) => {
285				14_u8.hash(state);
286			}
287			Self::Bson(value) => {
288				15_u8.hash(state);
289				value.hash(state);
290			}
291			Self::String(value) => {
292				16_u8.hash(state);
293				value.hash(state);
294			}
295			Self::Json(value) => {
296				17_u8.hash(state);
297				value.hash(state);
298			}
299			Self::Enum(value) => {
300				18_u8.hash(state);
301				value.hash(state);
302			}
303			Self::Url(value) => {
304				19_u8.hash(state);
305				value.hash(state);
306			}
307			Self::Webpage(value) => {
308				20_u8.hash(state);
309				value.hash(state);
310			}
311			Self::IpAddr(value) => {
312				21_u8.hash(state);
313				value.hash(state);
314			}
315			Self::List(value) => {
316				22_u8.hash(state);
317				value.hash(state);
318			}
319			Self::Map(_value) => {
320				23_u8.hash(state);
321			}
322			Self::Group(_value) => {
323				24_u8.hash(state);
324			}
325			Self::Option(value) => {
326				25_u8.hash(state);
327				value.hash(state);
328			}
329		}
330	}
331}
332impl Eq for Value {}
333impl PartialOrd for Value {
334	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
335		match (self, other) {
336			(Self::Bool(a), Self::Bool(b)) => a.partial_cmp(b),
337			(Self::U8(a), Self::U8(b)) => a.partial_cmp(b),
338			(Self::I8(a), Self::I8(b)) => a.partial_cmp(b),
339			(Self::U16(a), Self::U16(b)) => a.partial_cmp(b),
340			(Self::I16(a), Self::I16(b)) => a.partial_cmp(b),
341			(Self::U32(a), Self::U32(b)) => a.partial_cmp(b),
342			(Self::I32(a), Self::I32(b)) => a.partial_cmp(b),
343			(Self::U64(a), Self::U64(b)) => a.partial_cmp(b),
344			(Self::I64(a), Self::I64(b)) => a.partial_cmp(b),
345			(Self::F32(a), Self::F32(b)) => a.partial_cmp(b),
346			(Self::F64(a), Self::F64(b)) => a.partial_cmp(b),
347			(Self::Date(a), Self::Date(b)) => a.partial_cmp(b),
348			(Self::DateWithoutTimezone(a), Self::DateWithoutTimezone(b)) => a.partial_cmp(b),
349			(Self::Time(a), Self::Time(b)) => a.partial_cmp(b),
350			(Self::TimeWithoutTimezone(a), Self::TimeWithoutTimezone(b)) => a.partial_cmp(b),
351			(Self::DateTime(a), Self::DateTime(b)) => a.partial_cmp(b),
352			(Self::DateTimeWithoutTimezone(a), Self::DateTimeWithoutTimezone(b)) => {
353				a.partial_cmp(b)
354			}
355			(Self::Timezone(a), Self::Timezone(b)) => a.partial_cmp(b),
356			(Self::Decimal(a), Self::Decimal(b)) => a.partial_cmp(b),
357			(Self::Bson(a), Self::Bson(b)) => a.partial_cmp(b),
358			(Self::String(a), Self::String(b)) => a.partial_cmp(b),
359			(Self::Json(a), Self::Json(b)) => a.partial_cmp(b),
360			(Self::Enum(a), Self::Enum(b)) => a.partial_cmp(b),
361			(Self::Url(a), Self::Url(b)) => a.partial_cmp(b),
362			(Self::Webpage(a), Self::Webpage(b)) => a.partial_cmp(b),
363			(Self::IpAddr(a), Self::IpAddr(b)) => a.partial_cmp(b),
364			(Self::List(a), Self::List(b)) => a.partial_cmp(b),
365			(Self::Map(_a), Self::Map(_b)) => None, // TODO?
366			(Self::Group(a), Self::Group(b)) => a.partial_cmp(b),
367			(Self::Option(a), Self::Option(b)) => a.partial_cmp(b),
368			_ => None,
369		}
370	}
371}
372impl AmadeusOrd for Value {
373	fn amadeus_cmp(&self, other: &Self) -> Ordering {
374		match (self, other) {
375			(Self::Bool(a), Self::Bool(b)) => a.amadeus_cmp(b),
376			(Self::U8(a), Self::U8(b)) => a.amadeus_cmp(b),
377			(Self::I8(a), Self::I8(b)) => a.amadeus_cmp(b),
378			(Self::U16(a), Self::U16(b)) => a.amadeus_cmp(b),
379			(Self::I16(a), Self::I16(b)) => a.amadeus_cmp(b),
380			(Self::U32(a), Self::U32(b)) => a.amadeus_cmp(b),
381			(Self::I32(a), Self::I32(b)) => a.amadeus_cmp(b),
382			(Self::U64(a), Self::U64(b)) => a.amadeus_cmp(b),
383			(Self::I64(a), Self::I64(b)) => a.amadeus_cmp(b),
384			(Self::F32(a), Self::F32(b)) => a.amadeus_cmp(b),
385			(Self::F64(a), Self::F64(b)) => a.amadeus_cmp(b),
386			(Self::Date(a), Self::Date(b)) => a.amadeus_cmp(b),
387			(Self::DateWithoutTimezone(a), Self::DateWithoutTimezone(b)) => a.amadeus_cmp(b),
388			(Self::Time(a), Self::Time(b)) => a.amadeus_cmp(b),
389			(Self::TimeWithoutTimezone(a), Self::TimeWithoutTimezone(b)) => a.amadeus_cmp(b),
390			(Self::DateTime(a), Self::DateTime(b)) => a.amadeus_cmp(b),
391			(Self::DateTimeWithoutTimezone(a), Self::DateTimeWithoutTimezone(b)) => {
392				a.amadeus_cmp(b)
393			}
394			(Self::Timezone(a), Self::Timezone(b)) => a.amadeus_cmp(b),
395			(Self::Decimal(a), Self::Decimal(b)) => a.amadeus_cmp(b),
396			(Self::Bson(a), Self::Bson(b)) => a.amadeus_cmp(b),
397			(Self::String(a), Self::String(b)) => a.amadeus_cmp(b),
398			(Self::Json(a), Self::Json(b)) => a.amadeus_cmp(b),
399			(Self::Enum(a), Self::Enum(b)) => a.amadeus_cmp(b),
400			(Self::Url(a), Self::Url(b)) => a.amadeus_cmp(b),
401			(Self::Webpage(a), Self::Webpage(b)) => a.amadeus_cmp(b),
402			(Self::IpAddr(a), Self::IpAddr(b)) => a.amadeus_cmp(b),
403			(Self::List(a), Self::List(b)) => a.amadeus_cmp(b),
404			(Self::Map(a), Self::Map(b)) => a.amadeus_cmp(b),
405			(Self::Group(a), Self::Group(b)) => a.amadeus_cmp(b),
406			(Self::Option(a), Self::Option(b)) => a.amadeus_cmp(b),
407			_ => unimplemented!(),
408		}
409	}
410}
411
412impl Value {
413	fn type_name(&self) -> &'static str {
414		match self {
415			Self::Bool(_value) => "bool",
416			Self::U8(_value) => "u8",
417			Self::I8(_value) => "i8",
418			Self::U16(_value) => "u16",
419			Self::I16(_value) => "i16",
420			Self::U32(_value) => "u32",
421			Self::I32(_value) => "i32",
422			Self::U64(_value) => "u64",
423			Self::I64(_value) => "i64",
424			Self::F32(_value) => "f32",
425			Self::F64(_value) => "f64",
426			Self::Date(_value) => "date",
427			Self::DateWithoutTimezone(_value) => "date_without_timezone",
428			Self::Time(_value) => "time",
429			Self::TimeWithoutTimezone(_value) => "time_without_timezone",
430			Self::DateTime(_value) => "date_time",
431			Self::DateTimeWithoutTimezone(_value) => "date_time_without_timezone",
432			Self::Timezone(_value) => "timezone",
433			Self::Decimal(_value) => "decimal",
434			Self::Bson(_value) => "bson",
435			Self::String(_value) => "string",
436			Self::Json(_value) => "json",
437			Self::Enum(_value) => "enum",
438			Self::Url(_value) => "url",
439			Self::Webpage(_value) => "webpage",
440			Self::IpAddr(_value) => "ip_addr",
441			Self::List(_value) => "list",
442			Self::Map(_value) => "map",
443			Self::Group(_value) => "group",
444			Self::Option(_value) => "option",
445		}
446	}
447
448	/// Returns true if the `Value` is an Bool. Returns false otherwise.
449	pub fn is_bool(&self) -> bool {
450		matches!(self, Self::Bool(_))
451	}
452
453	/// If the `Value` is an Bool, return a reference to it. Returns Err otherwise.
454	pub fn as_bool(&self) -> Result<bool, DowncastError> {
455		if let Self::Bool(ret) = self {
456			Ok(*ret)
457		} else {
458			Err(DowncastError {
459				from: self.type_name(),
460				to: "bool",
461			})
462		}
463	}
464
465	/// If the `Value` is an Bool, return it. Returns Err otherwise.
466	pub fn into_bool(self) -> Result<bool, DowncastError> {
467		if let Self::Bool(ret) = self {
468			Ok(ret)
469		} else {
470			Err(DowncastError {
471				from: self.type_name(),
472				to: "bool",
473			})
474		}
475	}
476
477	/// Returns true if the `Value` is an U8. Returns false otherwise.
478	pub fn is_u8(&self) -> bool {
479		matches!(self, Self::U8(_))
480	}
481
482	/// If the `Value` is an U8, return a reference to it. Returns Err otherwise.
483	pub fn as_u8(&self) -> Result<u8, DowncastError> {
484		if let Self::U8(ret) = self {
485			Ok(*ret)
486		} else {
487			Err(DowncastError {
488				from: self.type_name(),
489				to: "u8",
490			})
491		}
492	}
493
494	/// If the `Value` is an U8, return it. Returns Err otherwise.
495	pub fn into_u8(self) -> Result<u8, DowncastError> {
496		if let Self::U8(ret) = self {
497			Ok(ret)
498		} else {
499			Err(DowncastError {
500				from: self.type_name(),
501				to: "u8",
502			})
503		}
504	}
505
506	/// Returns true if the `Value` is an I8. Returns false otherwise.
507	pub fn is_i8(&self) -> bool {
508		matches!(self, Self::I8(_))
509	}
510
511	/// If the `Value` is an I8, return a reference to it. Returns Err otherwise.
512	pub fn as_i8(&self) -> Result<i8, DowncastError> {
513		if let Self::I8(ret) = self {
514			Ok(*ret)
515		} else {
516			Err(DowncastError {
517				from: self.type_name(),
518				to: "i8",
519			})
520		}
521	}
522
523	/// If the `Value` is an I8, return it. Returns Err otherwise.
524	pub fn into_i8(self) -> Result<i8, DowncastError> {
525		if let Self::I8(ret) = self {
526			Ok(ret)
527		} else {
528			Err(DowncastError {
529				from: self.type_name(),
530				to: "i8",
531			})
532		}
533	}
534
535	/// Returns true if the `Value` is an U16. Returns false otherwise.
536	pub fn is_u16(&self) -> bool {
537		matches!(self, Self::U16(_))
538	}
539
540	/// If the `Value` is an U16, return a reference to it. Returns Err otherwise.
541	pub fn as_u16(&self) -> Result<u16, DowncastError> {
542		if let Self::U16(ret) = self {
543			Ok(*ret)
544		} else {
545			Err(DowncastError {
546				from: self.type_name(),
547				to: "u16",
548			})
549		}
550	}
551
552	/// If the `Value` is an U16, return it. Returns Err otherwise.
553	pub fn into_u16(self) -> Result<u16, DowncastError> {
554		if let Self::U16(ret) = self {
555			Ok(ret)
556		} else {
557			Err(DowncastError {
558				from: self.type_name(),
559				to: "u16",
560			})
561		}
562	}
563
564	/// Returns true if the `Value` is an I16. Returns false otherwise.
565	pub fn is_i16(&self) -> bool {
566		matches!(self, Self::I16(_))
567	}
568
569	/// If the `Value` is an I16, return a reference to it. Returns Err otherwise.
570	pub fn as_i16(&self) -> Result<i16, DowncastError> {
571		if let Self::I16(ret) = self {
572			Ok(*ret)
573		} else {
574			Err(DowncastError {
575				from: self.type_name(),
576				to: "i16",
577			})
578		}
579	}
580
581	/// If the `Value` is an I16, return it. Returns Err otherwise.
582	pub fn into_i16(self) -> Result<i16, DowncastError> {
583		if let Self::I16(ret) = self {
584			Ok(ret)
585		} else {
586			Err(DowncastError {
587				from: self.type_name(),
588				to: "i16",
589			})
590		}
591	}
592
593	/// Returns true if the `Value` is an U32. Returns false otherwise.
594	pub fn is_u32(&self) -> bool {
595		matches!(self, Self::U32(_))
596	}
597
598	/// If the `Value` is an U32, return a reference to it. Returns Err otherwise.
599	pub fn as_u32(&self) -> Result<u32, DowncastError> {
600		if let Self::U32(ret) = self {
601			Ok(*ret)
602		} else {
603			Err(DowncastError {
604				from: self.type_name(),
605				to: "u32",
606			})
607		}
608	}
609
610	/// If the `Value` is an U32, return it. Returns Err otherwise.
611	pub fn into_u32(self) -> Result<u32, DowncastError> {
612		if let Self::U32(ret) = self {
613			Ok(ret)
614		} else {
615			Err(DowncastError {
616				from: self.type_name(),
617				to: "u32",
618			})
619		}
620	}
621
622	/// Returns true if the `Value` is an I32. Returns false otherwise.
623	pub fn is_i32(&self) -> bool {
624		matches!(self, Self::I32(_))
625	}
626
627	/// If the `Value` is an I32, return a reference to it. Returns Err otherwise.
628	pub fn as_i32(&self) -> Result<i32, DowncastError> {
629		if let Self::I32(ret) = self {
630			Ok(*ret)
631		} else {
632			Err(DowncastError {
633				from: self.type_name(),
634				to: "i32",
635			})
636		}
637	}
638
639	/// If the `Value` is an I32, return it. Returns Err otherwise.
640	pub fn into_i32(self) -> Result<i32, DowncastError> {
641		if let Self::I32(ret) = self {
642			Ok(ret)
643		} else {
644			Err(DowncastError {
645				from: self.type_name(),
646				to: "i32",
647			})
648		}
649	}
650
651	/// Returns true if the `Value` is an U64. Returns false otherwise.
652	pub fn is_u64(&self) -> bool {
653		matches!(self, Self::U64(_))
654	}
655
656	/// If the `Value` is an U64, return a reference to it. Returns Err otherwise.
657	pub fn as_u64(&self) -> Result<u64, DowncastError> {
658		if let Self::U64(ret) = self {
659			Ok(*ret)
660		} else {
661			Err(DowncastError {
662				from: self.type_name(),
663				to: "u64",
664			})
665		}
666	}
667
668	/// If the `Value` is an U64, return it. Returns Err otherwise.
669	pub fn into_u64(self) -> Result<u64, DowncastError> {
670		if let Self::U64(ret) = self {
671			Ok(ret)
672		} else {
673			Err(DowncastError {
674				from: self.type_name(),
675				to: "u64",
676			})
677		}
678	}
679
680	/// Returns true if the `Value` is an I64. Returns false otherwise.
681	pub fn is_i64(&self) -> bool {
682		matches!(self, Self::I64(_))
683	}
684
685	/// If the `Value` is an I64, return a reference to it. Returns Err otherwise.
686	pub fn as_i64(&self) -> Result<i64, DowncastError> {
687		if let Self::I64(ret) = self {
688			Ok(*ret)
689		} else {
690			Err(DowncastError {
691				from: self.type_name(),
692				to: "i64",
693			})
694		}
695	}
696
697	/// If the `Value` is an I64, return it. Returns Err otherwise.
698	pub fn into_i64(self) -> Result<i64, DowncastError> {
699		if let Self::I64(ret) = self {
700			Ok(ret)
701		} else {
702			Err(DowncastError {
703				from: self.type_name(),
704				to: "i64",
705			})
706		}
707	}
708
709	/// Returns true if the `Value` is an F32. Returns false otherwise.
710	pub fn is_f32(&self) -> bool {
711		matches!(self, Self::F32(_))
712	}
713
714	/// If the `Value` is an F32, return a reference to it. Returns Err otherwise.
715	pub fn as_f32(&self) -> Result<f32, DowncastError> {
716		if let Self::F32(ret) = self {
717			Ok(*ret)
718		} else {
719			Err(DowncastError {
720				from: self.type_name(),
721				to: "f32",
722			})
723		}
724	}
725
726	/// If the `Value` is an F32, return it. Returns Err otherwise.
727	pub fn into_f32(self) -> Result<f32, DowncastError> {
728		if let Self::F32(ret) = self {
729			Ok(ret)
730		} else {
731			Err(DowncastError {
732				from: self.type_name(),
733				to: "f32",
734			})
735		}
736	}
737
738	/// Returns true if the `Value` is an F64. Returns false otherwise.
739	pub fn is_f64(&self) -> bool {
740		matches!(self, Self::F64(_))
741	}
742
743	/// If the `Value` is an F64, return a reference to it. Returns Err otherwise.
744	pub fn as_f64(&self) -> Result<f64, DowncastError> {
745		if let Self::F64(ret) = self {
746			Ok(*ret)
747		} else {
748			Err(DowncastError {
749				from: self.type_name(),
750				to: "f64",
751			})
752		}
753	}
754
755	/// If the `Value` is an F64, return it. Returns Err otherwise.
756	pub fn into_f64(self) -> Result<f64, DowncastError> {
757		if let Self::F64(ret) = self {
758			Ok(ret)
759		} else {
760			Err(DowncastError {
761				from: self.type_name(),
762				to: "f64",
763			})
764		}
765	}
766
767	/// Returns true if the `Value` is an Date. Returns false otherwise.
768	pub fn is_date(&self) -> bool {
769		matches!(self, Self::Date(_))
770	}
771
772	/// If the `Value` is an Date, return a reference to it. Returns Err otherwise.
773	pub fn as_date(&self) -> Result<&Date, DowncastError> {
774		if let Self::Date(ret) = self {
775			Ok(ret)
776		} else {
777			Err(DowncastError {
778				from: self.type_name(),
779				to: "date",
780			})
781		}
782	}
783
784	/// If the `Value` is an Date, return it. Returns Err otherwise.
785	pub fn into_date(self) -> Result<Date, DowncastError> {
786		if let Self::Date(ret) = self {
787			Ok(ret)
788		} else {
789			Err(DowncastError {
790				from: self.type_name(),
791				to: "date",
792			})
793		}
794	}
795
796	/// Returns true if the `Value` is an DateWithoutTimezone. Returns false otherwise.
797	pub fn is_date_without_timezone(&self) -> bool {
798		matches!(self, Self::DateWithoutTimezone(_))
799	}
800
801	/// If the `Value` is an DateWithoutTimezone, return a reference to it. Returns Err otherwise.
802	pub fn as_date_without_timezone(&self) -> Result<&DateWithoutTimezone, DowncastError> {
803		if let Self::DateWithoutTimezone(ret) = self {
804			Ok(ret)
805		} else {
806			Err(DowncastError {
807				from: self.type_name(),
808				to: "date_without_timezone",
809			})
810		}
811	}
812
813	/// If the `Value` is an DateWithoutTimezone, return it. Returns Err otherwise.
814	pub fn into_date_without_timezone(self) -> Result<DateWithoutTimezone, DowncastError> {
815		if let Self::DateWithoutTimezone(ret) = self {
816			Ok(ret)
817		} else {
818			Err(DowncastError {
819				from: self.type_name(),
820				to: "date_without_timezone",
821			})
822		}
823	}
824
825	/// Returns true if the `Value` is an Time. Returns false otherwise.
826	pub fn is_time(&self) -> bool {
827		matches!(self, Self::Time(_))
828	}
829
830	/// If the `Value` is an Time, return a reference to it. Returns Err otherwise.
831	pub fn as_time(&self) -> Result<&Time, DowncastError> {
832		if let Self::Time(ret) = self {
833			Ok(ret)
834		} else {
835			Err(DowncastError {
836				from: self.type_name(),
837				to: "time",
838			})
839		}
840	}
841
842	/// If the `Value` is an Time, return it. Returns Err otherwise.
843	pub fn into_time(self) -> Result<Time, DowncastError> {
844		if let Self::Time(ret) = self {
845			Ok(ret)
846		} else {
847			Err(DowncastError {
848				from: self.type_name(),
849				to: "time",
850			})
851		}
852	}
853
854	/// Returns true if the `Value` is an TimeWithoutTimezone. Returns false otherwise.
855	pub fn is_time_without_timezone(&self) -> bool {
856		matches!(self, Self::TimeWithoutTimezone(_))
857	}
858
859	/// If the `Value` is an TimeWithoutTimezone, return a reference to it. Returns Err otherwise.
860	pub fn as_time_without_timezone(&self) -> Result<&TimeWithoutTimezone, DowncastError> {
861		if let Self::TimeWithoutTimezone(ret) = self {
862			Ok(ret)
863		} else {
864			Err(DowncastError {
865				from: self.type_name(),
866				to: "time_without_timezone",
867			})
868		}
869	}
870
871	/// If the `Value` is an TimeWithoutTimezone, return it. Returns Err otherwise.
872	pub fn into_time_without_timezone(self) -> Result<TimeWithoutTimezone, DowncastError> {
873		if let Self::TimeWithoutTimezone(ret) = self {
874			Ok(ret)
875		} else {
876			Err(DowncastError {
877				from: self.type_name(),
878				to: "time_without_timezone",
879			})
880		}
881	}
882
883	/// Returns true if the `Value` is an DateTime. Returns false otherwise.
884	pub fn is_date_time(&self) -> bool {
885		matches!(self, Self::DateTime(_))
886	}
887
888	/// If the `Value` is an DateTime, return a reference to it. Returns Err otherwise.
889	pub fn as_date_time(&self) -> Result<&DateTime, DowncastError> {
890		if let Self::DateTime(ret) = self {
891			Ok(ret)
892		} else {
893			Err(DowncastError {
894				from: self.type_name(),
895				to: "date_time",
896			})
897		}
898	}
899
900	/// If the `Value` is an DateTime, return it. Returns Err otherwise.
901	pub fn into_date_time(self) -> Result<DateTime, DowncastError> {
902		if let Self::DateTime(ret) = self {
903			Ok(ret)
904		} else {
905			Err(DowncastError {
906				from: self.type_name(),
907				to: "date_time",
908			})
909		}
910	}
911
912	/// Returns true if the `Value` is an DateTimeWithoutTimezone. Returns false otherwise.
913	pub fn is_date_time_without_timezone(&self) -> bool {
914		matches!(self, Self::DateTimeWithoutTimezone(_))
915	}
916
917	/// If the `Value` is an DateTimeWithoutTimezone, return a reference to it. Returns Err otherwise.
918	pub fn as_date_time_without_timezone(&self) -> Result<&DateTimeWithoutTimezone, DowncastError> {
919		if let Self::DateTimeWithoutTimezone(ret) = self {
920			Ok(ret)
921		} else {
922			Err(DowncastError {
923				from: self.type_name(),
924				to: "date_time_without_timezone",
925			})
926		}
927	}
928
929	/// If the `Value` is an DateTimeWithoutTimezone, return it. Returns Err otherwise.
930	pub fn into_date_time_without_timezone(self) -> Result<DateTimeWithoutTimezone, DowncastError> {
931		if let Self::DateTimeWithoutTimezone(ret) = self {
932			Ok(ret)
933		} else {
934			Err(DowncastError {
935				from: self.type_name(),
936				to: "date_time_without_timezone",
937			})
938		}
939	}
940
941	/// Returns true if the `Value` is an Timezone. Returns false otherwise.
942	pub fn is_timezone(&self) -> bool {
943		matches!(self, Self::Timezone(_))
944	}
945
946	/// If the `Value` is an Timezone, return a reference to it. Returns Err otherwise.
947	pub fn as_timezone(&self) -> Result<&Timezone, DowncastError> {
948		if let Self::Timezone(ret) = self {
949			Ok(ret)
950		} else {
951			Err(DowncastError {
952				from: self.type_name(),
953				to: "timezone",
954			})
955		}
956	}
957
958	/// If the `Value` is an Timezone, return it. Returns Err otherwise.
959	pub fn into_timezone(self) -> Result<Timezone, DowncastError> {
960		if let Self::Timezone(ret) = self {
961			Ok(ret)
962		} else {
963			Err(DowncastError {
964				from: self.type_name(),
965				to: "timezone",
966			})
967		}
968	}
969
970	/// Returns true if the `Value` is an Decimal. Returns false otherwise.
971	pub fn is_decimal(&self) -> bool {
972		matches!(self, Self::Decimal(_))
973	}
974
975	/// If the `Value` is an Decimal, return a reference to it. Returns Err otherwise.
976	pub fn as_decimal(&self) -> Result<&Decimal, DowncastError> {
977		if let Self::Decimal(ret) = self {
978			Ok(ret)
979		} else {
980			Err(DowncastError {
981				from: self.type_name(),
982				to: "decimal",
983			})
984		}
985	}
986
987	/// If the `Value` is an Decimal, return it. Returns Err otherwise.
988	pub fn into_decimal(self) -> Result<Decimal, DowncastError> {
989		if let Self::Decimal(ret) = self {
990			Ok(ret)
991		} else {
992			Err(DowncastError {
993				from: self.type_name(),
994				to: "decimal",
995			})
996		}
997	}
998
999	/// Returns true if the `Value` is an Bson. Returns false otherwise.
1000	pub fn is_bson(&self) -> bool {
1001		matches!(self, Self::Bson(_))
1002	}
1003
1004	/// If the `Value` is an Bson, return a reference to it. Returns Err otherwise.
1005	pub fn as_bson(&self) -> Result<&Bson, DowncastError> {
1006		if let Self::Bson(ret) = self {
1007			Ok(ret)
1008		} else {
1009			Err(DowncastError {
1010				from: self.type_name(),
1011				to: "bson",
1012			})
1013		}
1014	}
1015
1016	/// If the `Value` is an Bson, return it. Returns Err otherwise.
1017	pub fn into_bson(self) -> Result<Bson, DowncastError> {
1018		if let Self::Bson(ret) = self {
1019			Ok(ret)
1020		} else {
1021			Err(DowncastError {
1022				from: self.type_name(),
1023				to: "bson",
1024			})
1025		}
1026	}
1027
1028	/// Returns true if the `Value` is an String. Returns false otherwise.
1029	pub fn is_string(&self) -> bool {
1030		matches!(self, Self::String(_))
1031	}
1032
1033	/// If the `Value` is an String, return a reference to it. Returns Err otherwise.
1034	pub fn as_string(&self) -> Result<&String, DowncastError> {
1035		if let Self::String(ret) = self {
1036			Ok(ret)
1037		} else {
1038			Err(DowncastError {
1039				from: self.type_name(),
1040				to: "string",
1041			})
1042		}
1043	}
1044
1045	/// If the `Value` is an String, return it. Returns Err otherwise.
1046	pub fn into_string(self) -> Result<String, DowncastError> {
1047		if let Self::String(ret) = self {
1048			Ok(ret)
1049		} else {
1050			Err(DowncastError {
1051				from: self.type_name(),
1052				to: "string",
1053			})
1054		}
1055	}
1056
1057	/// Returns true if the `Value` is an Json. Returns false otherwise.
1058	pub fn is_json(&self) -> bool {
1059		matches!(self, Self::Json(_))
1060	}
1061
1062	/// If the `Value` is an Json, return a reference to it. Returns Err otherwise.
1063	pub fn as_json(&self) -> Result<&Json, DowncastError> {
1064		if let Self::Json(ret) = self {
1065			Ok(ret)
1066		} else {
1067			Err(DowncastError {
1068				from: self.type_name(),
1069				to: "json",
1070			})
1071		}
1072	}
1073
1074	/// If the `Value` is an Json, return it. Returns Err otherwise.
1075	pub fn into_json(self) -> Result<Json, DowncastError> {
1076		if let Self::Json(ret) = self {
1077			Ok(ret)
1078		} else {
1079			Err(DowncastError {
1080				from: self.type_name(),
1081				to: "json",
1082			})
1083		}
1084	}
1085
1086	/// Returns true if the `Value` is an Enum. Returns false otherwise.
1087	pub fn is_enum(&self) -> bool {
1088		matches!(self, Self::Enum(_))
1089	}
1090
1091	/// If the `Value` is an Enum, return a reference to it. Returns Err otherwise.
1092	pub fn as_enum(&self) -> Result<&Enum, DowncastError> {
1093		if let Self::Enum(ret) = self {
1094			Ok(ret)
1095		} else {
1096			Err(DowncastError {
1097				from: self.type_name(),
1098				to: "enum",
1099			})
1100		}
1101	}
1102
1103	/// If the `Value` is an Enum, return it. Returns Err otherwise.
1104	pub fn into_enum(self) -> Result<Enum, DowncastError> {
1105		if let Self::Enum(ret) = self {
1106			Ok(ret)
1107		} else {
1108			Err(DowncastError {
1109				from: self.type_name(),
1110				to: "enum",
1111			})
1112		}
1113	}
1114
1115	/// Returns true if the `Value` is an Url. Returns false otherwise.
1116	pub fn is_url(&self) -> bool {
1117		matches!(self, Self::Url(_))
1118	}
1119
1120	/// If the `Value` is an Url, return a reference to it. Returns Err otherwise.
1121	pub fn as_url(&self) -> Result<&Url, DowncastError> {
1122		if let Self::Url(ret) = self {
1123			Ok(ret)
1124		} else {
1125			Err(DowncastError {
1126				from: self.type_name(),
1127				to: "url",
1128			})
1129		}
1130	}
1131
1132	/// If the `Value` is an Url, return it. Returns Err otherwise.
1133	pub fn into_url(self) -> Result<Url, DowncastError> {
1134		if let Self::Url(ret) = self {
1135			Ok(ret)
1136		} else {
1137			Err(DowncastError {
1138				from: self.type_name(),
1139				to: "url",
1140			})
1141		}
1142	}
1143
1144	/// Returns true if the `Value` is an Webpage. Returns false otherwise.
1145	pub fn is_webpage(&self) -> bool {
1146		matches!(self, Self::Webpage(_))
1147	}
1148
1149	/// If the `Value` is an Webpage, return a reference to it. Returns Err otherwise.
1150	pub fn as_webpage(&self) -> Result<&Webpage, DowncastError> {
1151		if let Self::Webpage(ret) = self {
1152			Ok(ret)
1153		} else {
1154			Err(DowncastError {
1155				from: self.type_name(),
1156				to: "webpage",
1157			})
1158		}
1159	}
1160
1161	/// If the `Value` is an Webpage, return it. Returns Err otherwise.
1162	pub fn into_webpage(self) -> Result<Webpage<'static>, DowncastError> {
1163		if let Self::Webpage(ret) = self {
1164			Ok(ret)
1165		} else {
1166			Err(DowncastError {
1167				from: self.type_name(),
1168				to: "webpage",
1169			})
1170		}
1171	}
1172
1173	/// Returns true if the `Value` is an IpAddr. Returns false otherwise.
1174	pub fn is_ip_addr(&self) -> bool {
1175		matches!(self, Self::IpAddr(_))
1176	}
1177
1178	/// If the `Value` is an IpAddr, return a reference to it. Returns Err otherwise.
1179	pub fn as_ip_addr(&self) -> Result<&IpAddr, DowncastError> {
1180		if let Self::IpAddr(ret) = self {
1181			Ok(ret)
1182		} else {
1183			Err(DowncastError {
1184				from: self.type_name(),
1185				to: "ip_addr",
1186			})
1187		}
1188	}
1189
1190	/// If the `Value` is an IpAddr, return it. Returns Err otherwise.
1191	pub fn into_ip_addr(self) -> Result<IpAddr, DowncastError> {
1192		if let Self::IpAddr(ret) = self {
1193			Ok(ret)
1194		} else {
1195			Err(DowncastError {
1196				from: self.type_name(),
1197				to: "ip_addr",
1198			})
1199		}
1200	}
1201
1202	/// Returns true if the `Value` is an List. Returns false otherwise.
1203	pub fn is_list(&self) -> bool {
1204		matches!(self, Self::List(_))
1205	}
1206
1207	/// If the `Value` is an List, return a reference to it. Returns Err otherwise.
1208	pub fn as_list(&self) -> Result<&List<Self>, DowncastError> {
1209		if let Self::List(ret) = self {
1210			Ok(ret)
1211		} else {
1212			Err(DowncastError {
1213				from: self.type_name(),
1214				to: "list",
1215			})
1216		}
1217	}
1218
1219	/// If the `Value` is an List, return it. Returns Err otherwise.
1220	pub fn into_list(self) -> Result<List<Self>, DowncastError> {
1221		if let Self::List(ret) = self {
1222			Ok(ret)
1223		} else {
1224			Err(DowncastError {
1225				from: self.type_name(),
1226				to: "list",
1227			})
1228		}
1229	}
1230
1231	/// Returns true if the `Value` is an Map. Returns false otherwise.
1232	pub fn is_map(&self) -> bool {
1233		matches!(self, Self::Map(_))
1234	}
1235
1236	/// If the `Value` is an Map, return a reference to it. Returns Err otherwise.
1237	pub fn as_map(&self) -> Result<&HashMap<Self, Self>, DowncastError> {
1238		if let Self::Map(ret) = self {
1239			Ok(ret)
1240		} else {
1241			Err(DowncastError {
1242				from: self.type_name(),
1243				to: "map",
1244			})
1245		}
1246	}
1247
1248	/// If the `Value` is an Map, return it. Returns Err otherwise.
1249	pub fn into_map(self) -> Result<HashMap<Self, Self>, DowncastError> {
1250		if let Self::Map(ret) = self {
1251			Ok(ret)
1252		} else {
1253			Err(DowncastError {
1254				from: self.type_name(),
1255				to: "map",
1256			})
1257		}
1258	}
1259
1260	/// Returns true if the `Value` is an Group. Returns false otherwise.
1261	pub fn is_group(&self) -> bool {
1262		matches!(self, Self::Group(_))
1263	}
1264
1265	/// If the `Value` is an Group, return a reference to it. Returns Err otherwise.
1266	pub fn as_group(&self) -> Result<&Group, DowncastError> {
1267		if let Self::Group(ret) = self {
1268			Ok(ret)
1269		} else {
1270			Err(DowncastError {
1271				from: self.type_name(),
1272				to: "group",
1273			})
1274		}
1275	}
1276
1277	/// If the `Value` is an Group, return it. Returns Err otherwise.
1278	pub fn into_group(self) -> Result<Group, DowncastError> {
1279		if let Self::Group(ret) = self {
1280			Ok(ret)
1281		} else {
1282			Err(DowncastError {
1283				from: self.type_name(),
1284				to: "group",
1285			})
1286		}
1287	}
1288
1289	/// Returns true if the `Value` is an Option. Returns false otherwise.
1290	pub fn is_option(&self) -> bool {
1291		matches!(self, Self::Option(_))
1292	}
1293
1294	/// If the `Value` is an Option, return a reference to it. Returns Err otherwise.
1295	fn as_option(&self) -> Result<&Option<ValueRequired>, DowncastError> {
1296		if let Self::Option(ret) = self {
1297			Ok(ret)
1298		} else {
1299			Err(DowncastError {
1300				from: self.type_name(),
1301				to: "option",
1302			})
1303		}
1304	}
1305
1306	/// If the `Value` is an Option, return it. Returns Err otherwise.
1307	pub fn into_option(self) -> Result<Option<Self>, DowncastError> {
1308		if let Self::Option(ret) = self {
1309			Ok(ret.map(Into::into))
1310		} else {
1311			Err(DowncastError {
1312				from: self.type_name(),
1313				to: "option",
1314			})
1315		}
1316	}
1317}
1318
1319impl From<bool> for Value {
1320	fn from(value: bool) -> Self {
1321		Self::Bool(value)
1322	}
1323}
1324impl From<u8> for Value {
1325	fn from(value: u8) -> Self {
1326		Self::U8(value)
1327	}
1328}
1329impl From<i8> for Value {
1330	fn from(value: i8) -> Self {
1331		Self::I8(value)
1332	}
1333}
1334impl From<u16> for Value {
1335	fn from(value: u16) -> Self {
1336		Self::U16(value)
1337	}
1338}
1339impl From<i16> for Value {
1340	fn from(value: i16) -> Self {
1341		Self::I16(value)
1342	}
1343}
1344impl From<u32> for Value {
1345	fn from(value: u32) -> Self {
1346		Self::U32(value)
1347	}
1348}
1349impl From<i32> for Value {
1350	fn from(value: i32) -> Self {
1351		Self::I32(value)
1352	}
1353}
1354impl From<u64> for Value {
1355	fn from(value: u64) -> Self {
1356		Self::U64(value)
1357	}
1358}
1359impl From<i64> for Value {
1360	fn from(value: i64) -> Self {
1361		Self::I64(value)
1362	}
1363}
1364impl From<f32> for Value {
1365	fn from(value: f32) -> Self {
1366		Self::F32(value)
1367	}
1368}
1369impl From<f64> for Value {
1370	fn from(value: f64) -> Self {
1371		Self::F64(value)
1372	}
1373}
1374impl From<Date> for Value {
1375	fn from(value: Date) -> Self {
1376		Self::Date(value)
1377	}
1378}
1379impl From<DateWithoutTimezone> for Value {
1380	fn from(value: DateWithoutTimezone) -> Self {
1381		Self::DateWithoutTimezone(value)
1382	}
1383}
1384impl From<Time> for Value {
1385	fn from(value: Time) -> Self {
1386		Self::Time(value)
1387	}
1388}
1389impl From<TimeWithoutTimezone> for Value {
1390	fn from(value: TimeWithoutTimezone) -> Self {
1391		Self::TimeWithoutTimezone(value)
1392	}
1393}
1394impl From<DateTime> for Value {
1395	fn from(value: DateTime) -> Self {
1396		Self::DateTime(value)
1397	}
1398}
1399impl From<DateTimeWithoutTimezone> for Value {
1400	fn from(value: DateTimeWithoutTimezone) -> Self {
1401		Self::DateTimeWithoutTimezone(value)
1402	}
1403}
1404impl From<Timezone> for Value {
1405	fn from(value: Timezone) -> Self {
1406		Self::Timezone(value)
1407	}
1408}
1409impl From<Decimal> for Value {
1410	fn from(value: Decimal) -> Self {
1411		Self::Decimal(value)
1412	}
1413}
1414impl From<Bson> for Value {
1415	fn from(value: Bson) -> Self {
1416		Self::Bson(value)
1417	}
1418}
1419impl From<String> for Value {
1420	fn from(value: String) -> Self {
1421		Self::String(value)
1422	}
1423}
1424impl From<Json> for Value {
1425	fn from(value: Json) -> Self {
1426		Self::Json(value)
1427	}
1428}
1429impl From<Enum> for Value {
1430	fn from(value: Enum) -> Self {
1431		Self::Enum(value)
1432	}
1433}
1434impl From<Url> for Value {
1435	fn from(value: Url) -> Self {
1436		Self::Url(value)
1437	}
1438}
1439impl From<Webpage<'static>> for Value {
1440	fn from(value: Webpage<'static>) -> Self {
1441		Self::Webpage(value)
1442	}
1443}
1444impl From<IpAddr> for Value {
1445	fn from(value: IpAddr) -> Self {
1446		Self::IpAddr(value)
1447	}
1448}
1449impl<T: Data> From<List<T>> for Value
1450where
1451	T: Into<Self>,
1452{
1453	fn from(value: List<T>) -> Self {
1454		Self::List(value.map(Into::into))
1455	}
1456}
1457impl<K, V, S> From<HashMap<K, V, S>> for Value
1458where
1459	K: Into<Self> + Hash + Eq,
1460	V: Into<Self>,
1461	S: BuildHasher,
1462{
1463	fn from(value: HashMap<K, V, S>) -> Self {
1464		Self::Map(
1465			value
1466				.into_iter()
1467				.map(|(k, v)| (k.into(), v.into()))
1468				.collect(),
1469		)
1470	}
1471}
1472impl From<Group> for Value {
1473	fn from(value: Group) -> Self {
1474		Self::Group(value)
1475	}
1476}
1477impl<T> From<Option<T>> for Value
1478where
1479	T: Into<Self>,
1480{
1481	fn from(value: Option<T>) -> Self {
1482		Self::Option(
1483			value
1484				.map(Into::into)
1485				.map(|x| <Option<ValueRequired> as From<Self>>::from(x).unwrap()),
1486		)
1487	}
1488}
1489impl<T> From<Box<T>> for Value
1490where
1491	T: Into<Self>,
1492{
1493	fn from(value: Box<T>) -> Self {
1494		(*value).into()
1495	}
1496}
1497macro_rules! array_from {
1498	($($i:tt)*) => {$(
1499		impl<T: Data> From<[T; $i]> for Value
1500		where
1501			T: Into<Self>
1502		{
1503			fn from(value: [T; $i]) -> Self {
1504				let x: Box<[T]> = Box::new(value);
1505				let x: List<T> = x.into();
1506				x.into()
1507			}
1508		}
1509	)*}
1510}
1511array!(array_from);
1512macro_rules! tuple_from {
1513	($len:tt $($t:ident $i:tt)*) => (
1514		impl<$($t,)*> From<($($t,)*)> for Value where $($t: Into<Value>,)* {
1515			#[allow(unused_variables)]
1516			fn from(value: ($($t,)*)) -> Self {
1517				Value::Group(Group::new(vec![$(value.$i.into(),)*], None))
1518			}
1519		}
1520	);
1521}
1522tuple!(tuple_from);
1523
1524// Downcast implementations for Value so we can try downcasting it to a specific type if
1525// we know it.
1526
1527impl DowncastFrom<Self> for Value {
1528	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1529		Ok(self_)
1530	}
1531}
1532impl DowncastFrom<Value> for bool {
1533	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1534		self_.into_bool()
1535	}
1536}
1537impl DowncastFrom<Value> for u8 {
1538	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1539		self_.into_u8()
1540	}
1541}
1542impl DowncastFrom<Value> for i8 {
1543	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1544		self_.into_i8()
1545	}
1546}
1547impl DowncastFrom<Value> for u16 {
1548	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1549		self_.into_u16()
1550	}
1551}
1552impl DowncastFrom<Value> for i16 {
1553	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1554		self_.into_i16()
1555	}
1556}
1557impl DowncastFrom<Value> for u32 {
1558	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1559		self_.into_u32()
1560	}
1561}
1562impl DowncastFrom<Value> for i32 {
1563	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1564		self_.into_i32()
1565	}
1566}
1567impl DowncastFrom<Value> for u64 {
1568	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1569		self_.into_u64()
1570	}
1571}
1572impl DowncastFrom<Value> for i64 {
1573	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1574		self_.into_i64()
1575	}
1576}
1577impl DowncastFrom<Value> for f32 {
1578	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1579		self_.into_f32()
1580	}
1581}
1582impl DowncastFrom<Value> for f64 {
1583	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1584		self_.into_f64()
1585	}
1586}
1587impl DowncastFrom<Value> for Date {
1588	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1589		self_.into_date()
1590	}
1591}
1592impl DowncastFrom<Value> for DateWithoutTimezone {
1593	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1594		self_.into_date_without_timezone()
1595	}
1596}
1597impl DowncastFrom<Value> for Time {
1598	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1599		self_.into_time()
1600	}
1601}
1602impl DowncastFrom<Value> for TimeWithoutTimezone {
1603	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1604		self_.into_time_without_timezone()
1605	}
1606}
1607impl DowncastFrom<Value> for DateTime {
1608	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1609		self_.into_date_time()
1610	}
1611}
1612impl DowncastFrom<Value> for DateTimeWithoutTimezone {
1613	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1614		self_.into_date_time_without_timezone()
1615	}
1616}
1617impl DowncastFrom<Value> for Timezone {
1618	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1619		self_.into_timezone()
1620	}
1621}
1622impl DowncastFrom<Value> for Decimal {
1623	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1624		self_.into_decimal()
1625	}
1626}
1627impl DowncastFrom<Value> for Bson {
1628	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1629		self_.into_bson()
1630	}
1631}
1632impl DowncastFrom<Value> for String {
1633	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1634		self_.into_string()
1635	}
1636}
1637impl DowncastFrom<Value> for Json {
1638	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1639		self_.into_json()
1640	}
1641}
1642impl DowncastFrom<Value> for Enum {
1643	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1644		self_.into_enum()
1645	}
1646}
1647impl DowncastFrom<Value> for Url {
1648	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1649		self_.into_url()
1650	}
1651}
1652impl DowncastFrom<Value> for Webpage<'static> {
1653	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1654		self_.into_webpage()
1655	}
1656}
1657impl DowncastFrom<Value> for IpAddr {
1658	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1659		self_.into_ip_addr()
1660	}
1661}
1662impl<T: Data> DowncastFrom<Value> for List<T>
1663where
1664	T: DowncastFrom<Value>,
1665{
1666	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1667		self_
1668			.into_list()
1669			.and_then(|list| list.try_map(Downcast::downcast))
1670	}
1671}
1672impl<K, V, S> DowncastFrom<Value> for HashMap<K, V, S>
1673where
1674	K: DowncastFrom<Value> + Hash + Eq,
1675	V: DowncastFrom<Value>,
1676	S: BuildHasher + Default,
1677{
1678	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1679		self_.into_map().and_then(|map| {
1680			map.into_iter()
1681				.map(|(k, v)| Ok((k.downcast()?, v.downcast()?)))
1682				.collect()
1683		})
1684	}
1685}
1686impl DowncastFrom<Value> for Group {
1687	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1688		self_.into_group()
1689	}
1690}
1691impl<T> DowncastFrom<Value> for Option<T>
1692where
1693	T: DowncastFrom<Value>,
1694{
1695	fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1696		match self_.into_option()? {
1697			Some(t) => t.downcast().map(Some),
1698			None => Ok(None),
1699		}
1700	}
1701}
1702macro_rules! array_downcast {
1703	($($i:tt)*) => {$(
1704		impl<T: Data> DowncastFrom<Value> for [T; $i]
1705		where
1706			T: DowncastFrom<Value>
1707		{
1708			fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1709				let err = DowncastError {
1710					from: self_.type_name(),
1711					to: stringify!([T; $i]),
1712				};
1713				let x: Box<[T]> = <List<T>>::downcast_from(self_).map_err(|_| err)?.into_boxed_slice();
1714				let x: Box<Self> = x.try_into().map_err(|_| err)?;
1715				Ok(*x)
1716			}
1717		}
1718	)*}
1719}
1720array!(array_downcast);
1721macro_rules! tuple_downcast {
1722	($len:tt $($t:ident $i:tt)*) => (
1723		 impl<$($t,)*> DowncastFrom<Value> for ($($t,)*) where $($t: DowncastFrom<Value>,)* {
1724			fn downcast_from(self_: Value) -> Result<Self, DowncastError> {
1725				#[allow(unused_mut, unused_variables)]
1726				let mut fields = self_.into_group()?.into_fields().into_iter();
1727				if fields.len() != $len {
1728					return Err(DowncastError{from:"",to:""});
1729				}
1730				Ok(($({let _ = $i;fields.next().unwrap().downcast()?},)*))
1731			}
1732		}
1733	);
1734}
1735tuple!(tuple_downcast);
1736
1737// PartialEq implementations for Value so we can compare it with typed values
1738
1739impl PartialEq<bool> for Value {
1740	fn eq(&self, other: &bool) -> bool {
1741		self.as_bool().map(|bool| &bool == other).unwrap_or(false)
1742	}
1743}
1744impl PartialEq<u8> for Value {
1745	fn eq(&self, other: &u8) -> bool {
1746		self.as_u8().map(|u8| &u8 == other).unwrap_or(false)
1747	}
1748}
1749impl PartialEq<i8> for Value {
1750	fn eq(&self, other: &i8) -> bool {
1751		self.as_i8().map(|i8| &i8 == other).unwrap_or(false)
1752	}
1753}
1754impl PartialEq<u16> for Value {
1755	fn eq(&self, other: &u16) -> bool {
1756		self.as_u16().map(|u16| &u16 == other).unwrap_or(false)
1757	}
1758}
1759impl PartialEq<i16> for Value {
1760	fn eq(&self, other: &i16) -> bool {
1761		self.as_i16().map(|i16| &i16 == other).unwrap_or(false)
1762	}
1763}
1764impl PartialEq<u32> for Value {
1765	fn eq(&self, other: &u32) -> bool {
1766		self.as_u32().map(|u32| &u32 == other).unwrap_or(false)
1767	}
1768}
1769impl PartialEq<i32> for Value {
1770	fn eq(&self, other: &i32) -> bool {
1771		self.as_i32().map(|i32| &i32 == other).unwrap_or(false)
1772	}
1773}
1774impl PartialEq<u64> for Value {
1775	fn eq(&self, other: &u64) -> bool {
1776		self.as_u64().map(|u64| &u64 == other).unwrap_or(false)
1777	}
1778}
1779impl PartialEq<i64> for Value {
1780	fn eq(&self, other: &i64) -> bool {
1781		self.as_i64().map(|i64| &i64 == other).unwrap_or(false)
1782	}
1783}
1784impl PartialEq<f32> for Value {
1785	fn eq(&self, other: &f32) -> bool {
1786		self.as_f32().map(|f32| &f32 == other).unwrap_or(false)
1787	}
1788}
1789impl PartialEq<f64> for Value {
1790	fn eq(&self, other: &f64) -> bool {
1791		self.as_f64().map(|f64| &f64 == other).unwrap_or(false)
1792	}
1793}
1794impl PartialEq<Date> for Value {
1795	fn eq(&self, other: &Date) -> bool {
1796		self.as_date().map(|date| date == other).unwrap_or(false)
1797	}
1798}
1799impl PartialEq<DateWithoutTimezone> for Value {
1800	fn eq(&self, other: &DateWithoutTimezone) -> bool {
1801		self.as_date_without_timezone()
1802			.map(|date_without_timezone| date_without_timezone == other)
1803			.unwrap_or(false)
1804	}
1805}
1806impl PartialEq<Time> for Value {
1807	fn eq(&self, other: &Time) -> bool {
1808		self.as_time().map(|time| time == other).unwrap_or(false)
1809	}
1810}
1811impl PartialEq<TimeWithoutTimezone> for Value {
1812	fn eq(&self, other: &TimeWithoutTimezone) -> bool {
1813		self.as_time_without_timezone()
1814			.map(|time_without_timezone| time_without_timezone == other)
1815			.unwrap_or(false)
1816	}
1817}
1818impl PartialEq<DateTime> for Value {
1819	fn eq(&self, other: &DateTime) -> bool {
1820		self.as_date_time()
1821			.map(|date_time| date_time == other)
1822			.unwrap_or(false)
1823	}
1824}
1825impl PartialEq<DateTimeWithoutTimezone> for Value {
1826	fn eq(&self, other: &DateTimeWithoutTimezone) -> bool {
1827		self.as_date_time_without_timezone()
1828			.map(|date_time_without_timezone| date_time_without_timezone == other)
1829			.unwrap_or(false)
1830	}
1831}
1832impl PartialEq<Timezone> for Value {
1833	fn eq(&self, other: &Timezone) -> bool {
1834		self.as_timezone()
1835			.map(|timezone| timezone == other)
1836			.unwrap_or(false)
1837	}
1838}
1839impl PartialEq<Decimal> for Value {
1840	fn eq(&self, other: &Decimal) -> bool {
1841		self.as_decimal()
1842			.map(|decimal| decimal == other)
1843			.unwrap_or(false)
1844	}
1845}
1846impl PartialEq<Bson> for Value {
1847	fn eq(&self, other: &Bson) -> bool {
1848		self.as_bson().map(|bson| bson == other).unwrap_or(false)
1849	}
1850}
1851impl PartialEq<String> for Value {
1852	fn eq(&self, other: &String) -> bool {
1853		self.as_string()
1854			.map(|string| string == other)
1855			.unwrap_or(false)
1856	}
1857}
1858impl PartialEq<Json> for Value {
1859	fn eq(&self, other: &Json) -> bool {
1860		self.as_json().map(|json| json == other).unwrap_or(false)
1861	}
1862}
1863impl PartialEq<Enum> for Value {
1864	fn eq(&self, other: &Enum) -> bool {
1865		self.as_enum().map(|enum_| enum_ == other).unwrap_or(false)
1866	}
1867}
1868impl PartialEq<Url> for Value {
1869	fn eq(&self, other: &Url) -> bool {
1870		self.as_url().map(|url| url == other).unwrap_or(false)
1871	}
1872}
1873impl<'a> PartialEq<Webpage<'a>> for Value {
1874	fn eq(&self, other: &Webpage<'a>) -> bool {
1875		self.as_webpage()
1876			.map(|webpage| webpage == other)
1877			.unwrap_or(false)
1878	}
1879}
1880impl PartialEq<IpAddr> for Value {
1881	fn eq(&self, other: &IpAddr) -> bool {
1882		self.as_ip_addr()
1883			.map(|ip_addr| ip_addr == other)
1884			.unwrap_or(false)
1885	}
1886}
1887impl<T: Data> PartialEq<List<T>> for Value
1888where
1889	Value: PartialEq<T>,
1890{
1891	fn eq(&self, other: &List<T>) -> bool {
1892		self.as_list().map(|list| list == other).unwrap_or(false)
1893	}
1894}
1895impl<K, V, S> PartialEq<HashMap<K, V, S>> for Value
1896where
1897	Value: PartialEq<K> + PartialEq<V>,
1898	K: Hash + Eq + Clone + Into<Value>,
1899	S: BuildHasher,
1900{
1901	fn eq(&self, other: &HashMap<K, V, S>) -> bool {
1902		self.as_map()
1903			.map(|map| {
1904				if map.len() != other.len() {
1905					return false;
1906				}
1907
1908				// This comparison unfortunately requires a bit of a hack. This could be
1909				// eliminated by ensuring that Value::X hashes identically to X. TODO.
1910				let other = other
1911					.iter()
1912					.map(|(k, v)| (k.clone().into(), v))
1913					.collect::<HashMap<Self, _>>();
1914
1915				map.iter()
1916					.all(|(key, value)| other.get(key).map_or(false, |v| value == *v))
1917			})
1918			.unwrap_or(false)
1919	}
1920}
1921impl PartialEq<Group> for Value {
1922	fn eq(&self, other: &Group) -> bool {
1923		self.as_group().map(|group| group == other).unwrap_or(false)
1924	}
1925}
1926impl<T> PartialEq<Option<T>> for Value
1927where
1928	Value: PartialEq<T>,
1929{
1930	fn eq(&self, other: &Option<T>) -> bool {
1931		self.as_option()
1932			.map(|option| match (&option, other) {
1933				(Some(a), Some(b)) => match a {
1934					ValueRequired::Bool(value) => &Value::Bool(*value) == b,
1935					ValueRequired::U8(value) => &Value::U8(*value) == b,
1936					ValueRequired::I8(value) => &Value::I8(*value) == b,
1937					ValueRequired::U16(value) => &Value::U16(*value) == b,
1938					ValueRequired::I16(value) => &Value::I16(*value) == b,
1939					ValueRequired::U32(value) => &Value::U32(*value) == b,
1940					ValueRequired::I32(value) => &Value::I32(*value) == b,
1941					ValueRequired::U64(value) => &Value::U64(*value) == b,
1942					ValueRequired::I64(value) => &Value::I64(*value) == b,
1943					ValueRequired::F32(value) => &Value::F32(*value) == b,
1944					ValueRequired::F64(value) => &Value::F64(*value) == b,
1945					ValueRequired::Date(value) => &Value::Date(*value) == b,
1946					ValueRequired::DateWithoutTimezone(value) => {
1947						&Value::DateWithoutTimezone(*value) == b
1948					}
1949					ValueRequired::Time(value) => &Value::Time(*value) == b,
1950					ValueRequired::TimeWithoutTimezone(value) => {
1951						&Value::TimeWithoutTimezone(*value) == b
1952					}
1953					ValueRequired::DateTime(value) => &Value::DateTime(*value) == b,
1954					ValueRequired::DateTimeWithoutTimezone(value) => {
1955						&Value::DateTimeWithoutTimezone(*value) == b
1956					}
1957					ValueRequired::Timezone(value) => &Value::Timezone(*value) == b,
1958					ValueRequired::Decimal(value) => &Value::Decimal(value.clone()) == b,
1959					ValueRequired::Bson(value) => &Value::Bson(value.clone()) == b,
1960					ValueRequired::String(value) => &Value::String(value.clone()) == b,
1961					ValueRequired::Json(value) => &Value::Json(value.clone()) == b,
1962					ValueRequired::Enum(value) => &Value::Enum(value.clone()) == b,
1963					ValueRequired::Url(value) => &Value::Url(value.clone()) == b,
1964					ValueRequired::Webpage(value) => &Value::Webpage(value.clone()) == b,
1965					ValueRequired::IpAddr(value) => &Value::IpAddr(*value) == b,
1966					ValueRequired::List(value) => &Value::List(value.clone()) == b,
1967					ValueRequired::Map(value) => &Value::Map(value.clone()) == b,
1968					ValueRequired::Group(value) => &Value::Group(value.clone()) == b,
1969				},
1970				(None, None) => true,
1971				_ => false,
1972			})
1973			.unwrap_or(false)
1974	}
1975}
1976impl<T> PartialEq<Box<T>> for Value
1977where
1978	Value: PartialEq<T>,
1979{
1980	fn eq(&self, other: &Box<T>) -> bool {
1981		self == other
1982	}
1983}
1984
1985macro_rules! tuple_partialeq {
1986	($len:tt $($t:ident $i:tt)*) => (
1987		impl<$($t,)*> PartialEq<($($t,)*)> for Value where Value: $(PartialEq<$t> +)* {
1988			#[allow(unused_variables)]
1989			fn eq(&self, other: &($($t,)*)) -> bool {
1990				self.is_group() $(&& self.as_group().unwrap()[$i] == other.$i)*
1991			}
1992		}
1993		impl<$($t,)*> PartialEq<($($t,)*)> for Group where Value: $(PartialEq<$t> +)* {
1994			#[allow(unused_variables)]
1995			fn eq(&self, other: &($($t,)*)) -> bool {
1996				$(self[$i] == other.$i && )* true
1997			}
1998		}
1999	);
2000}
2001tuple!(tuple_partialeq);
2002
2003// impl Serialize for Value {
2004// 	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2005// 	where
2006// 		S: Serializer,
2007// 	{
2008// 		Self::serde_serialize(self, serializer)
2009// 	}
2010// }
2011// impl<'de> Deserialize<'de> for Value {
2012// 	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2013// 	where
2014// 		D: Deserializer<'de>,
2015// 	{
2016// 		Self::serde_deserialize(deserializer, None)
2017// 	}
2018// }
2019
2020// /// A Reader that wraps a Reader, wrapping the read value in a `Record`.
2021// pub struct ValueReader<T>(T);
2022// impl<T> internal::record::Reader for ValueReader<T>
2023// where
2024// 	T: internal::record::Reader<Item = internal::record::types::Value>,
2025// {
2026// 	type Item = Value;
2027
2028// 	#[inline]
2029// 	fn read(&mut self, def_level: i16, rep_level: i16) -> Result<Self::Item, ParquetError> {
2030// 		self.0.read(def_level, rep_level).map(Into::into)
2031// 	}
2032
2033// 	#[inline]
2034// 	fn advance_columns(&mut self) -> Result<(), ParquetError> {
2035// 		self.0.advance_columns()
2036// 	}
2037
2038// 	#[inline]
2039// 	fn has_next(&self) -> bool {
2040// 		self.0.has_next()
2041// 	}
2042
2043// 	#[inline]
2044// 	fn current_def_level(&self) -> i16 {
2045// 		self.0.current_def_level()
2046// 	}
2047
2048// 	#[inline]
2049// 	fn current_rep_level(&self) -> i16 {
2050// 		self.0.current_rep_level()
2051// 	}
2052// }
2053
2054impl From<Vec<u8>> for List<Value> {
2055	#[inline(always)]
2056	fn from(vec: Vec<u8>) -> Self {
2057		Self::from_(ValueVec::U8(vec))
2058	}
2059}
2060impl FromIterator<u8> for List<Value> {
2061	#[inline(always)]
2062	fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self {
2063		Vec::from_iter(iter).into()
2064	}
2065}
2066impl Extend<u8> for List<Value> {
2067	#[inline(always)]
2068	fn extend<I: IntoIterator<Item = u8>>(&mut self, _iter: I) {
2069		unimplemented!()
2070	}
2071}
2072
2073// #[derive(Clone, Debug)]
2074// enum Value {
2075// 	U8(u8),
2076// 	U16(u16),
2077// 	List(Box<List<Value>>),
2078// }
2079// impl Value {
2080// 	fn u8(self) -> u8 {
2081// 		if let Self::U8(ret) = self {
2082// 			ret
2083// 		} else {
2084// 			panic!()
2085// 		}
2086// 	}
2087// 	fn u16(self) -> u16 {
2088// 		if let Self::U16(ret) = self {
2089// 			ret
2090// 		} else {
2091// 			panic!()
2092// 		}
2093// 	}
2094// 	fn list(self) -> List<Value> {
2095// 		if let Self::List(ret) = self {
2096// 			*ret
2097// 		} else {
2098// 			panic!()
2099// 		}
2100// 	}
2101// }
2102#[doc(hidden)]
2103#[derive(Clone, Debug)]
2104pub enum ValueType {
2105	U8,
2106	U16,
2107	List,
2108}
2109#[doc(hidden)]
2110#[derive(Clone, Hash, Serialize, Deserialize)]
2111pub enum ValueVec {
2112	U8(Vec<u8>),
2113	U16(Vec<u16>),
2114	List(Vec<List<Value>>),
2115	Value(Vec<Value>),
2116}
2117impl Debug for ValueVec {
2118	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2119		match self {
2120			Self::U8(self_) => fmt.debug_tuple("U8").field(self_).finish(),
2121			Self::U16(self_) => fmt.debug_tuple("U16").field(self_).finish(),
2122			Self::List(self_) => fmt.debug_tuple("List").field(self_).finish(),
2123			Self::Value(self_) => self_.fmt(fmt),
2124		}
2125	}
2126}
2127impl Data for Value {
2128	type Vec = ValueVec;
2129	type DynamicType = ValueType;
2130
2131	fn new_vec(type_: Self::DynamicType) -> Self::Vec {
2132		match type_ {
2133			ValueType::U8 => ValueVec::U8(ListVec::new()),
2134			ValueType::U16 => ValueVec::U16(ListVec::new()),
2135			ValueType::List => ValueVec::List(ListVec::new()),
2136		}
2137	}
2138}
2139impl ListVec<Value> for ValueVec {
2140	type IntoIter = std::vec::IntoIter<Value>;
2141
2142	#[inline(always)]
2143	fn new() -> Self {
2144		Self::Value(vec![])
2145	}
2146	#[inline(always)]
2147	fn push(&mut self, t: Value) {
2148		match self {
2149			ValueVec::U8(list) => list.push(t.into_u8().unwrap()),
2150			ValueVec::U16(list) => list.push(t.into_u16().unwrap()),
2151			ValueVec::List(list) => list.push(t.into_list().unwrap()),
2152			ValueVec::Value(list) => list.push(t),
2153		}
2154	}
2155	fn pop(&mut self) -> Option<Value> {
2156		match self {
2157			ValueVec::U8(list) => list.pop().map(Value::U8),
2158			ValueVec::U16(list) => list.pop().map(Value::U16),
2159			ValueVec::List(list) => list.pop().map(Value::List),
2160			ValueVec::Value(list) => list.pop(),
2161		}
2162	}
2163	#[inline(always)]
2164	fn len(&self) -> usize {
2165		match self {
2166			ValueVec::U8(list) => list.len(),
2167			ValueVec::U16(list) => list.len(),
2168			ValueVec::List(list) => list.len(),
2169			ValueVec::Value(list) => list.len(),
2170		}
2171	}
2172	#[inline(always)]
2173	fn from_vec(vec: Vec<Value>) -> Self {
2174		Self::Value(vec)
2175	}
2176	#[inline(always)]
2177	fn into_vec(self) -> Vec<Value> {
2178		match self {
2179			Self::U8(vec) => vec.map(Into::into),
2180			Self::U16(vec) => vec.map(Into::into),
2181			Self::List(vec) => vec.map(Into::into),
2182			Self::Value(vec) => vec,
2183		}
2184	}
2185	#[inline(always)]
2186	fn into_iter_a(self) -> Self::IntoIter {
2187		// TODO
2188		self.into_vec().into_iter()
2189	}
2190	// #[inline(always)]
2191	// fn iter_a<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Value> + 'a> {
2192	// 	todo!()
2193	// 	// Box::new(match self {
2194	// 	// 	Self::U8(vec) => vec.iter().map(Value::U8),
2195	// 	// 	Self::U16(vec) => vec.iter().map(Value::U16),
2196	// 	// 	Self::List(vec) => vec.iter().map(Value::List),
2197	// 	// 	Self::Value(vec) => vec.iter(),
2198	// 	// })
2199	// }
2200	#[inline(always)]
2201	fn clone_a(&self) -> Self
2202	where
2203		Value: Clone,
2204	{
2205		self.clone()
2206	}
2207	#[inline(always)]
2208	fn hash_a<H>(&self, state: &mut H)
2209	where
2210		H: Hasher,
2211		Value: Hash,
2212	{
2213		self.hash(state)
2214	}
2215	#[inline(always)]
2216	fn serialize_a<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2217	where
2218		S: Serializer,
2219		Value: Serialize,
2220	{
2221		self.serialize(serializer)
2222	}
2223	#[inline(always)]
2224	fn deserialize_a<'de, D>(deserializer: D) -> Result<Self, D::Error>
2225	where
2226		D: Deserializer<'de>,
2227		Value: Deserialize<'de>,
2228		Self: Sized,
2229	{
2230		Self::deserialize(deserializer)
2231	}
2232	fn fmt_a(&self, fmt: &mut fmt::Formatter) -> fmt::Result
2233	where
2234		Value: Debug,
2235	{
2236		self.fmt(fmt)
2237	}
2238}
2239
2240// struct Iter<'a, T>(std::slice::Iter<'a, T>);
2241// impl<'a> Iterator for Iter<'a, u8> {
2242// 	type Item = ValueRef<'a, Value>;
2243
2244// 	#[inline(always)]
2245// 	fn next(&mut self) -> Option<Self::Item> {
2246// 		self.0
2247// 			.next()
2248// 			.map(|next| ValueRef(ManuallyDrop::new(Value::U8(*next)), &()))
2249// 	}
2250// }
2251// impl<'a> Iterator for Iter<'a, u16> {
2252// 	type Item = ValueRef<'a, Value>;
2253
2254// 	#[inline(always)]
2255// 	fn next(&mut self) -> Option<Self::Item> {
2256// 		self.0
2257// 			.next()
2258// 			.map(|next| ValueRef(ManuallyDrop::new(Value::U16(*next)), &()))
2259// 	}
2260// }
2261// impl<'a> Iterator for Iter<'a, List<Value>> {
2262// 	type Item = ValueRef<'a, Value>;
2263
2264// 	#[inline(always)]
2265// 	fn next(&mut self) -> Option<Self::Item> {
2266// 		self.0.next().map(|next| {
2267// 			ValueRef(
2268// 				ManuallyDrop::new(Value::List(Box::new(unsafe { ptr::read(next) }))),
2269// 				&(),
2270// 			)
2271// 		})
2272// 	}
2273// }
2274// impl<'a> Iterator for Iter<'a, Value> {
2275// 	type Item = ValueRef<'a, Value>;
2276
2277// 	#[inline(always)]
2278// 	fn next(&mut self) -> Option<Self::Item> {
2279// 		self.0
2280// 			.next()
2281// 			.map(|next| ValueRef(ManuallyDrop::new(unsafe { ptr::read(next) }), &()))
2282// 	}
2283// }
2284
2285// // TODO! Iterating a List<Value>{vec:ValueVec::List(..)} will leak a load of boxes.
2286
2287// // impl<'a,T> Drop for ValueRef<'a,T> {
2288// // 	fn drop(&mut self) {
2289// // 		if let Some(self_) = try_type_coerce::<&mut ValueRef<'a,T>,&mut ValueRef<'a,Value>>(self) {
2290// // 			match unsafe { ManuallyDrop::take(&mut self_.0) } {
2291// // 				Value::Bool(x) => assert_copy(x),
2292// // 				Value::U8(x) => assert_copy(x),
2293// // 				Value::I8(x) => assert_copy(x),
2294// // 				Value::U16(x) => assert_copy(x),
2295// // 				Value::I16(x) => assert_copy(x),
2296// // 				Value::U32(x) => assert_copy(x),
2297// // 				Value::I32(x) => assert_copy(x),
2298// // 				Value::U64(x) => assert_copy(x),
2299// // 				Value::I64(x) => assert_copy(x),
2300// // 				Value::F32(x) => assert_copy(x),
2301// // 				Value::F64(x) => assert_copy(x),
2302// // 				Value::Date(x) => assert_copy(x),
2303// // 				Value::DateWithoutTimezone(x) => assert_copy(x),
2304// // 				Value::Time(x) => assert_copy(x),
2305// // 				Value::TimeWithoutTimezone(x) => assert_copy(x),
2306// // 				Value::DateTime(x) => assert_copy(x),
2307// // 				Value::DateTimeWithoutTimezone(x) => assert_copy(x),
2308// // 				Value::Timezone(x) => assert_copy(x),
2309// // 				Value::Decimal(x) => forget(x),
2310// // 				Value::Bson(x) => forget(x),
2311// // 				Value::String(x) => forget(x),
2312// // 				Value::Json(x) => forget(x),
2313// // 				Value::Enum(x) => forget(x),
2314// // 				Value::Url(x) => forget(x),
2315// // 				Value::Webpage(x) => forget(x),
2316// // 				Value::IpAddr(x) => assert_copy(x),
2317// // 				Value::List(x) => forget(*x),
2318// // 				Value::Map(x) => forget(x),
2319// // 				Value::Group(x) => forget(x),
2320// // 				Value::Option(x) => forget(x),
2321// // 			}
2322// // 		}
2323// // 	}
2324// // }
2325// // fn assert_copy<T: Copy>(_t: T) {}
2326
2327// impl<'a, T> Serialize for ValueRef<'a, T>
2328// where
2329// 	T: Serialize,
2330// {
2331// 	#[inline(always)]
2332// 	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2333// 	where
2334// 		S: Serializer,
2335// 	{
2336// 		(**self).serialize(serializer)
2337// 	}
2338// }
2339// impl<'a, T> AmadeusOrd for ValueRef<'a, T>
2340// where
2341// 	T: AmadeusOrd,
2342// {
2343// 	#[inline(always)]
2344// 	fn amadeus_cmp(&self, _other: &Self) -> Ordering {
2345// 		unimplemented!()
2346// 	}
2347// }