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
mod any_uri;
pub mod base64_binary;
mod boolean;
mod date;
mod date_time;
mod decimal;
mod double;
mod duration;
mod float;
mod g_day;
mod g_month;
mod g_month_day;
mod g_year;
mod g_year_month;
pub mod hex_binary;
mod notation;
mod q_name;
mod string;
mod time;

use std::fmt;

pub use any_uri::*;
pub use base64_binary::{Base64Binary, Base64BinaryBuf, InvalidBase64};
pub use boolean::*;
pub use date::*;
pub use date_time::*;
pub use decimal::*;
pub use double::*;
pub use duration::*;
pub use float::*;
pub use g_day::*;
pub use g_month::*;
pub use g_month_day::*;
pub use g_year::*;
pub use g_year_month::*;
pub use hex_binary::{HexBinary, HexBinaryBuf, InvalidHex};
pub use notation::*;
pub use q_name::*;
pub use string::*;
pub use time::*;

use crate::{
	Datatype, DecimalDatatype, IntDatatype, IntegerDatatype, LongDatatype,
	NonNegativeIntegerDatatype, NonPositiveIntegerDatatype, ShortDatatype, UnsignedIntDatatype,
	UnsignedLongDatatype, UnsignedShortDatatype,
};

pub trait XsdDatatype {
	/// Returns the XSD datatype that best describes the value.
	fn type_(&self) -> Datatype;
}

/// XSD datatype value.
#[derive(Debug, Clone)]
pub enum Value {
	String(String),
	Boolean(Boolean),
	Decimal(Decimal),
	Integer(Integer),
	NonPositiveInteger(NonPositiveInteger),
	NegativeInteger(NegativeInteger),
	Long(Long),
	Int(Int),
	Short(Short),
	Byte(Byte),
	NonNegativeInteger(NonNegativeInteger),
	UnsignedLong(UnsignedLong),
	UnsignedInt(UnsignedInt),
	UnsignedShort(UnsignedShort),
	UnsignedByte(UnsignedByte),
	PositiveInteger(PositiveInteger),
	Float(Float),
	Double(Double),
	Duration(Duration),
	DateTime(DateTime),
	Time(Time),
	Date(Date),
	GYearMonth(GYearMonth),
	GYear(GYear),
	GMonthDay(GMonthDay),
	GDay(GDay),
	GMonth(GMonth),
	HexBinary(HexBinaryBuf),
	Base64Binary(Base64BinaryBuf),
	AnyUri(AnyUriBuf),
	QName(QName),
	Notation(Notation),
}

impl XsdDatatype for Value {
	fn type_(&self) -> Datatype {
		match self {
			Self::String(_) => Datatype::String(None),
			Self::Boolean(_) => Datatype::Boolean,
			Self::Decimal(_) => Datatype::Decimal(None),
			Self::Integer(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(None))),
			Self::NonPositiveInteger(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonPositiveInteger(None),
			)))),
			Self::NegativeInteger(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonPositiveInteger(Some(
					NonPositiveIntegerDatatype::NegativeInteger,
				)),
			)))),
			Self::Long(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::Long(None),
			)))),
			Self::Int(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::Long(Some(LongDatatype::Int(None))),
			)))),
			Self::Short(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::Long(Some(LongDatatype::Int(Some(IntDatatype::Short(None))))),
			)))),
			Self::Byte(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::Long(Some(LongDatatype::Int(Some(IntDatatype::Short(Some(
					ShortDatatype::Byte,
				)))))),
			)))),
			Self::NonNegativeInteger(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonNegativeInteger(None),
			)))),
			Self::UnsignedLong(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonNegativeInteger(Some(
					NonNegativeIntegerDatatype::UnsignedLong(None),
				)),
			)))),
			Self::UnsignedInt(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonNegativeInteger(Some(
					NonNegativeIntegerDatatype::UnsignedLong(Some(
						UnsignedLongDatatype::UnsignedInt(None),
					)),
				)),
			)))),
			Self::UnsignedShort(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonNegativeInteger(Some(
					NonNegativeIntegerDatatype::UnsignedLong(Some(
						UnsignedLongDatatype::UnsignedInt(Some(
							UnsignedIntDatatype::UnsignedShort(None),
						)),
					)),
				)),
			)))),
			Self::UnsignedByte(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonNegativeInteger(Some(
					NonNegativeIntegerDatatype::UnsignedLong(Some(
						UnsignedLongDatatype::UnsignedInt(Some(
							UnsignedIntDatatype::UnsignedShort(Some(
								UnsignedShortDatatype::UnsignedByte,
							)),
						)),
					)),
				)),
			)))),
			Self::PositiveInteger(_) => Datatype::Decimal(Some(DecimalDatatype::Integer(Some(
				IntegerDatatype::NonNegativeInteger(Some(
					NonNegativeIntegerDatatype::PositiveInteger,
				)),
			)))),
			Self::Float(_) => Datatype::Float,
			Self::Double(_) => Datatype::Double,
			Self::Duration(_) => Datatype::Duration,
			Self::DateTime(_) => Datatype::DateTime,
			Self::Time(_) => Datatype::Time,
			Self::Date(_) => Datatype::Date,
			Self::GYearMonth(_) => Datatype::GYearMonth,
			Self::GYear(_) => Datatype::GYear,
			Self::GMonthDay(_) => Datatype::GMonthDay,
			Self::GDay(_) => Datatype::GDay,
			Self::GMonth(_) => Datatype::GMonth,
			Self::HexBinary(_) => Datatype::HexBinary,
			Self::Base64Binary(_) => Datatype::Base64Binary,
			Self::AnyUri(_) => Datatype::AnyUri,
			Self::QName(_) => Datatype::QName,
			Self::Notation(_) => Datatype::Notation,
		}
	}
}

impl fmt::Display for Value {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::String(v) => v.fmt(f),
			Self::Boolean(v) => v.fmt(f),
			Self::Decimal(v) => v.fmt(f),
			Self::Integer(v) => v.fmt(f),
			Self::NonPositiveInteger(v) => v.fmt(f),
			Self::NegativeInteger(v) => v.fmt(f),
			Self::Long(v) => v.fmt(f),
			Self::Int(v) => v.fmt(f),
			Self::Short(v) => v.fmt(f),
			Self::Byte(v) => v.fmt(f),
			Self::NonNegativeInteger(v) => v.fmt(f),
			Self::UnsignedLong(v) => v.fmt(f),
			Self::UnsignedInt(v) => v.fmt(f),
			Self::UnsignedShort(v) => v.fmt(f),
			Self::UnsignedByte(v) => v.fmt(f),
			Self::PositiveInteger(v) => v.fmt(f),
			Self::Float(v) => v.fmt(f),
			Self::Double(v) => v.fmt(f),
			Self::Duration(v) => v.fmt(f),
			Self::DateTime(v) => v.fmt(f),
			Self::Time(v) => v.fmt(f),
			Self::Date(v) => v.fmt(f),
			Self::GYearMonth(v) => v.fmt(f),
			Self::GYear(v) => v.fmt(f),
			Self::GMonthDay(v) => v.fmt(f),
			Self::GDay(v) => v.fmt(f),
			Self::GMonth(v) => v.fmt(f),
			Self::HexBinary(v) => v.fmt(f),
			Self::Base64Binary(v) => v.fmt(f),
			Self::AnyUri(v) => v.fmt(f),
			Self::QName(v) => v.fmt(f),
			Self::Notation(v) => v.fmt(f),
		}
	}
}

impl From<Value> for std::string::String {
	fn from(value: Value) -> Self {
		value.to_string()
	}
}