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
//! This crate aims at providing safe representations
//! of [XSD built-in data types][xsd].
//!
//! [xsd]: <https://www.w3.org/TR/xmlschema-2/#built-in-datatypes>
//!
//! # Usage
//!
//! For each XSD datatype, this library provides two families of types:
//! one representing the *lexical space* of the datatype (see the [`lexical`]
//! module), and one representing the *value space* of the datatype (see the
//! [`value`] module).
//!
//! For instance, assume we wish to store the lexical representation of an
//! [XSD decimal][xsd-decimal] datatype value. We can use the
//! [`lexical::Decimal`] type (or its owned variant, [`lexical::DecimalBuf`])
//!
//! [`lexical`]: crate::lexical
//! [`value`]: crate::value
//! [xsd-decimal]: <https://www.w3.org/TR/xmlschema11-2/#decimal>
//! [`lexical::Decimal`]: crate::lexical::Decimal
//! [`lexical::DecimalBuf`]: crate::lexical::DecimalBuf
//!
//! ```
//! let string = "3.141592653589793";
//!
//! // Parse the lexical representation (lexical domain).
//! let lexical_repr = xsd_types::lexical::Decimal::new(string).unwrap();
//!
//! // Interprets the lexical representation (value domain).
//! use xsd_types::lexical::LexicalFormOf;
//! let value_repr: xsd_types::Decimal = lexical_repr.try_as_value().unwrap();
//! ```
//!
//! Of course it is possible to parse the value directly into the value domain
//! using [`FromStr`](::core::str::FromStr):
//! ```
//! let value_repr: xsd_types::Decimal = "3.141592653589793".parse().unwrap();
//! ```
//!
//! ## Any value
//!
//! The [`Value`] type provides a simple way to represent *any* XSD value.
//!
//! ```
//! use xsd_types::{XSD_DATE, Datatype, Value};
//! let dt = Datatype::from_iri(XSD_DATE).unwrap();
//! let value: Value = dt.parse("1758-12-25").unwrap(); // Halley is back!
//! ```
//!
//! [`Value`]: crate::Value
use iref::Iri;
use static_iref::iri;

/// Lexical domain types.
pub mod lexical;
pub(crate) mod utils;

/// Value domain types.
pub mod value;

use lexical::{Lexical, LexicalFormOf};
pub use value::*;

mod types;

pub use types::*;

/// XSD primitive datatype.
pub enum PrimitiveDatatype {
	String,
	Boolean,
	Decimal,
	Float,
	Double,
	Duration,
	DateTime,
	Time,
	Date,
	GYearMonth,
	GYear,
	GMonthDay,
	GDay,
	GMonth,
	HexBinary,
	Base64Binary,
	AnyUri,
	QName,
	Notation,
}

/// <http://www.w3.org/2001/XMLSchema#duration> datatype IRI.
pub const XSD_DURATION: &Iri = iri!("http://www.w3.org/2001/XMLSchema#duration");

/// <http://www.w3.org/2001/XMLSchema#dayTimeDuration> datatype IRI.
pub const XSD_DAY_TIME_DURATION: &Iri = iri!("http://www.w3.org/2001/XMLSchema#dayTimeDuration");

/// <http://www.w3.org/2001/XMLSchema#yearMonthDuration> datatype IRI.
pub const XSD_YEAR_MONTH_DURATION: &Iri =
	iri!("http://www.w3.org/2001/XMLSchema#yearMonthDuration");

/// <http://www.w3.org/2001/XMLSchema#dateTime> datatype IRI.
pub const XSD_DATE_TIME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#dateTime");

/// <http://www.w3.org/2001/XMLSchema#dateTimeStamp> datatype IRI.
pub const XSD_DATE_TIME_STAMP: &Iri = iri!("http://www.w3.org/2001/XMLSchema#dateTimeStamp");

/// <http://www.w3.org/2001/XMLSchema#time> datatype IRI.
pub const XSD_TIME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#time");

/// <http://www.w3.org/2001/XMLSchema#date> datatype IRI.
pub const XSD_DATE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#date");

/// <http://www.w3.org/2001/XMLSchema#gYearMonth> datatype IRI.
pub const XSD_G_YEAR_MONTH: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gYearMonth");

/// <http://www.w3.org/2001/XMLSchema#gYear> datatype IRI.
pub const XSD_G_YEAR: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gYear");

/// <http://www.w3.org/2001/XMLSchema#gMonthDay> datatype IRI.
pub const XSD_G_MONTH_DAY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gMonthDay");

/// <http://www.w3.org/2001/XMLSchema#gDay> datatype IRI.
pub const XSD_G_DAY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gDay");

/// <http://www.w3.org/2001/XMLSchema#gMonth> datatype IRI.
pub const XSD_G_MONTH: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gMonth");

/// <http://www.w3.org/2001/XMLSchema#string> datatype IRI.
pub const XSD_STRING: &Iri = iri!("http://www.w3.org/2001/XMLSchema#string");

/// <http://www.w3.org/2001/XMLSchema#boolean> datatype IRI.
pub const XSD_BOOLEAN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#boolean");

/// <http://www.w3.org/2001/XMLSchema#base64Binary> datatype IRI.
pub const XSD_BASE64_BINARY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#base64Binary");

/// <http://www.w3.org/2001/XMLSchema#hexBinary> datatype IRI.
pub const XSD_HEX_BINARY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#hexBinary");

/// <http://www.w3.org/2001/XMLSchema#float> datatype IRI.
pub const XSD_FLOAT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#float");

/// <http://www.w3.org/2001/XMLSchema#decimal> datatype IRI.
pub const XSD_DECIMAL: &Iri = iri!("http://www.w3.org/2001/XMLSchema#decimal");

/// <http://www.w3.org/2001/XMLSchema#double> datatype IRI.
pub const XSD_DOUBLE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#double");

/// <http://www.w3.org/2001/XMLSchema#anyURI> datatype IRI.
pub const XSD_ANY_URI: &Iri = iri!("http://www.w3.org/2001/XMLSchema#anyURI");

/// <http://www.w3.org/2001/XMLSchema#QName> datatype IRI.
pub const XSD_Q_NAME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#QName");

/// <http://www.w3.org/2001/XMLSchema#NOTATION> datatype IRI.
pub const XSD_NOTATION: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NOTATION");

/// <http://www.w3.org/2001/XMLSchema#normalizedString> datatype IRI.
pub const XSD_NORMALIZED_STRING: &Iri = iri!("http://www.w3.org/2001/XMLSchema#normalizedString");

/// <http://www.w3.org/2001/XMLSchema#token> datatype IRI.
pub const XSD_TOKEN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#token");

/// <http://www.w3.org/2001/XMLSchema#language> datatype IRI.
pub const XSD_LANGUAGE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#language");

/// <http://www.w3.org/2001/XMLSchema#Name> datatype IRI.
pub const XSD_NAME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#Name");

/// <http://www.w3.org/2001/XMLSchema#NMTOKEN> datatype IRI.
pub const XSD_NMTOKEN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NMTOKEN");

/// <http://www.w3.org/2001/XMLSchema#NCName> datatype IRI.
pub const XSD_NC_NAME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NCName");

/// <http://www.w3.org/2001/XMLSchema#NMTOKENS> datatype IRI.
pub const XSD_NMTOKENS: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NMTOKENS");

/// <http://www.w3.org/2001/XMLSchema#ID> datatype IRI.
pub const XSD_ID: &Iri = iri!("http://www.w3.org/2001/XMLSchema#ID");

/// <http://www.w3.org/2001/XMLSchema#IDREF> datatype IRI.
pub const XSD_IDREF: &Iri = iri!("http://www.w3.org/2001/XMLSchema#IDREF");

/// <http://www.w3.org/2001/XMLSchema#ENTITY> datatype IRI.
pub const XSD_ENTITY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#ENTITY");

/// <http://www.w3.org/2001/XMLSchema#IDREFS> datatype IRI.
pub const XSD_IDREFS: &Iri = iri!("http://www.w3.org/2001/XMLSchema#IDREFS");

/// <http://www.w3.org/2001/XMLSchema#ENTITIES> datatype IRI.
pub const XSD_ENTITIES: &Iri = iri!("http://www.w3.org/2001/XMLSchema#ENTITIES");

/// <http://www.w3.org/2001/XMLSchema#integer> datatype IRI.
pub const XSD_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#integer");

/// <http://www.w3.org/2001/XMLSchema#nonPositiveInteger> datatype IRI.
pub const XSD_NON_POSITIVE_INTEGER: &Iri =
	iri!("http://www.w3.org/2001/XMLSchema#nonPositiveInteger");

/// <http://www.w3.org/2001/XMLSchema#negativeInteger> datatype IRI.
pub const XSD_NEGATIVE_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#negativeInteger");

/// <http://www.w3.org/2001/XMLSchema#long> datatype IRI.
pub const XSD_LONG: &Iri = iri!("http://www.w3.org/2001/XMLSchema#long");

/// <http://www.w3.org/2001/XMLSchema#int> datatype IRI.
pub const XSD_INT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#int");

/// <http://www.w3.org/2001/XMLSchema#short> datatype IRI.
pub const XSD_SHORT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#short");

/// <http://www.w3.org/2001/XMLSchema#byte> datatype IRI.
pub const XSD_BYTE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#byte");

/// <http://www.w3.org/2001/XMLSchema#nonNegativeInteger> datatype IRI.
pub const XSD_NON_NEGATIVE_INTEGER: &Iri =
	iri!("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");

/// <http://www.w3.org/2001/XMLSchema#unsignedLong> datatype IRI.
pub const XSD_UNSIGNED_LONG: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedLong");

/// <http://www.w3.org/2001/XMLSchema#unsignedInt> datatype IRI.
pub const XSD_UNSIGNED_INT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedInt");

/// <http://www.w3.org/2001/XMLSchema#unsignedShort> datatype IRI.
pub const XSD_UNSIGNED_SHORT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedShort");

/// <http://www.w3.org/2001/XMLSchema#unsignedByte> datatype IRI.
pub const XSD_UNSIGNED_BYTE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedByte");

/// <http://www.w3.org/2001/XMLSchema#positiveInteger> datatype IRI.
pub const XSD_POSITIVE_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#positiveInteger");

/// Parse a value directly from its XSD lexical form.
pub trait ParseXsd: Sized {
	type LexicalForm: LexicalFormOf<Self> + ?Sized;

	fn parse_xsd(lexical_value: &str) -> ParseXsdResult<Self, Self::LexicalForm> {
		Self::LexicalForm::parse(lexical_value)
			.map_err(ParseXsdError::InvalidLexicalForm)?
			.try_as_value()
			.map_err(ParseXsdError::InvalidValue)
	}
}

/// XSD lexical parse result.
pub type ParseXsdResult<T, L> =
	Result<T, ParseXsdError<<L as Lexical>::Error, <L as LexicalFormOf<T>>::ValueError>>;

/// XSD lexical parse error.
pub enum ParseXsdError<L, V> {
	InvalidLexicalForm(L),
	InvalidValue(V),
}