xsd_types/
lib.rs

1//! This crate aims at providing safe representations
2//! of [XSD built-in data types][xsd].
3//!
4//! [xsd]: <https://www.w3.org/TR/xmlschema-2/#built-in-datatypes>
5//!
6//! # Usage
7//!
8//! For each XSD datatype, this library provides two families of types:
9//! one representing the *lexical space* of the datatype (see the [`lexical`]
10//! module), and one representing the *value space* of the datatype (see the
11//! [`value`] module).
12//!
13//! For instance, assume we wish to store the lexical representation of an
14//! [XSD decimal][xsd-decimal] datatype value. We can use the
15//! [`lexical::Decimal`] type (or its owned variant, [`lexical::DecimalBuf`])
16//!
17//! [`lexical`]: crate::lexical
18//! [`value`]: crate::value
19//! [xsd-decimal]: <https://www.w3.org/TR/xmlschema11-2/#decimal>
20//! [`lexical::Decimal`]: crate::lexical::Decimal
21//! [`lexical::DecimalBuf`]: crate::lexical::DecimalBuf
22//!
23//! ```
24//! let string = "3.141592653589793";
25//!
26//! // Parse the lexical representation (lexical domain).
27//! let lexical_repr = xsd_types::lexical::Decimal::new(string).unwrap();
28//!
29//! // Interprets the lexical representation (value domain).
30//! use xsd_types::lexical::LexicalFormOf;
31//! let value_repr: xsd_types::Decimal = lexical_repr.try_as_value().unwrap();
32//! ```
33//!
34//! Of course it is possible to parse the value directly into the value domain
35//! using [`FromStr`](::core::str::FromStr):
36//! ```
37//! let value_repr: xsd_types::Decimal = "3.141592653589793".parse().unwrap();
38//! ```
39//!
40//! ## Any value
41//!
42//! The [`Value`] type provides a simple way to represent *any* XSD value.
43//!
44//! ```
45//! use xsd_types::{XSD_DATE, Datatype, Value};
46//! let dt = Datatype::from_iri(XSD_DATE).unwrap();
47//! let value: Value = dt.parse("1758-12-25").unwrap(); // Halley is back!
48//! ```
49//!
50//! [`Value`]: crate::Value
51use iref::Iri;
52use static_iref::iri;
53
54/// Lexical domain types.
55pub mod lexical;
56pub(crate) mod utils;
57
58/// Value domain types.
59pub mod value;
60
61use lexical::{Lexical, LexicalFormOf};
62pub use value::*;
63
64mod types;
65
66pub use types::*;
67
68/// XSD primitive datatype.
69pub enum PrimitiveDatatype {
70	String,
71	Boolean,
72	Decimal,
73	Float,
74	Double,
75	Duration,
76	DateTime,
77	Time,
78	Date,
79	GYearMonth,
80	GYear,
81	GMonthDay,
82	GDay,
83	GMonth,
84	HexBinary,
85	Base64Binary,
86	AnyUri,
87	QName,
88	Notation,
89}
90
91/// <http://www.w3.org/2001/XMLSchema#duration> datatype IRI.
92pub const XSD_DURATION: &Iri = iri!("http://www.w3.org/2001/XMLSchema#duration");
93
94/// <http://www.w3.org/2001/XMLSchema#dayTimeDuration> datatype IRI.
95pub const XSD_DAY_TIME_DURATION: &Iri = iri!("http://www.w3.org/2001/XMLSchema#dayTimeDuration");
96
97/// <http://www.w3.org/2001/XMLSchema#yearMonthDuration> datatype IRI.
98pub const XSD_YEAR_MONTH_DURATION: &Iri =
99	iri!("http://www.w3.org/2001/XMLSchema#yearMonthDuration");
100
101/// <http://www.w3.org/2001/XMLSchema#dateTime> datatype IRI.
102pub const XSD_DATE_TIME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#dateTime");
103
104/// <http://www.w3.org/2001/XMLSchema#dateTimeStamp> datatype IRI.
105pub const XSD_DATE_TIME_STAMP: &Iri = iri!("http://www.w3.org/2001/XMLSchema#dateTimeStamp");
106
107/// <http://www.w3.org/2001/XMLSchema#time> datatype IRI.
108pub const XSD_TIME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#time");
109
110/// <http://www.w3.org/2001/XMLSchema#date> datatype IRI.
111pub const XSD_DATE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#date");
112
113/// <http://www.w3.org/2001/XMLSchema#gYearMonth> datatype IRI.
114pub const XSD_G_YEAR_MONTH: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gYearMonth");
115
116/// <http://www.w3.org/2001/XMLSchema#gYear> datatype IRI.
117pub const XSD_G_YEAR: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gYear");
118
119/// <http://www.w3.org/2001/XMLSchema#gMonthDay> datatype IRI.
120pub const XSD_G_MONTH_DAY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gMonthDay");
121
122/// <http://www.w3.org/2001/XMLSchema#gDay> datatype IRI.
123pub const XSD_G_DAY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gDay");
124
125/// <http://www.w3.org/2001/XMLSchema#gMonth> datatype IRI.
126pub const XSD_G_MONTH: &Iri = iri!("http://www.w3.org/2001/XMLSchema#gMonth");
127
128/// <http://www.w3.org/2001/XMLSchema#string> datatype IRI.
129pub const XSD_STRING: &Iri = iri!("http://www.w3.org/2001/XMLSchema#string");
130
131/// <http://www.w3.org/2001/XMLSchema#boolean> datatype IRI.
132pub const XSD_BOOLEAN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#boolean");
133
134/// <http://www.w3.org/2001/XMLSchema#base64Binary> datatype IRI.
135pub const XSD_BASE64_BINARY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#base64Binary");
136
137/// <http://www.w3.org/2001/XMLSchema#hexBinary> datatype IRI.
138pub const XSD_HEX_BINARY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#hexBinary");
139
140/// <http://www.w3.org/2001/XMLSchema#float> datatype IRI.
141pub const XSD_FLOAT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#float");
142
143/// <http://www.w3.org/2001/XMLSchema#decimal> datatype IRI.
144pub const XSD_DECIMAL: &Iri = iri!("http://www.w3.org/2001/XMLSchema#decimal");
145
146/// <http://www.w3.org/2001/XMLSchema#double> datatype IRI.
147pub const XSD_DOUBLE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#double");
148
149/// <http://www.w3.org/2001/XMLSchema#anyURI> datatype IRI.
150pub const XSD_ANY_URI: &Iri = iri!("http://www.w3.org/2001/XMLSchema#anyURI");
151
152/// <http://www.w3.org/2001/XMLSchema#QName> datatype IRI.
153pub const XSD_Q_NAME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#QName");
154
155/// <http://www.w3.org/2001/XMLSchema#NOTATION> datatype IRI.
156pub const XSD_NOTATION: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NOTATION");
157
158/// <http://www.w3.org/2001/XMLSchema#normalizedString> datatype IRI.
159pub const XSD_NORMALIZED_STRING: &Iri = iri!("http://www.w3.org/2001/XMLSchema#normalizedString");
160
161/// <http://www.w3.org/2001/XMLSchema#token> datatype IRI.
162pub const XSD_TOKEN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#token");
163
164/// <http://www.w3.org/2001/XMLSchema#language> datatype IRI.
165pub const XSD_LANGUAGE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#language");
166
167/// <http://www.w3.org/2001/XMLSchema#Name> datatype IRI.
168pub const XSD_NAME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#Name");
169
170/// <http://www.w3.org/2001/XMLSchema#NMTOKEN> datatype IRI.
171pub const XSD_NMTOKEN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NMTOKEN");
172
173/// <http://www.w3.org/2001/XMLSchema#NCName> datatype IRI.
174pub const XSD_NC_NAME: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NCName");
175
176/// <http://www.w3.org/2001/XMLSchema#NMTOKENS> datatype IRI.
177pub const XSD_NMTOKENS: &Iri = iri!("http://www.w3.org/2001/XMLSchema#NMTOKENS");
178
179/// <http://www.w3.org/2001/XMLSchema#ID> datatype IRI.
180pub const XSD_ID: &Iri = iri!("http://www.w3.org/2001/XMLSchema#ID");
181
182/// <http://www.w3.org/2001/XMLSchema#IDREF> datatype IRI.
183pub const XSD_IDREF: &Iri = iri!("http://www.w3.org/2001/XMLSchema#IDREF");
184
185/// <http://www.w3.org/2001/XMLSchema#ENTITY> datatype IRI.
186pub const XSD_ENTITY: &Iri = iri!("http://www.w3.org/2001/XMLSchema#ENTITY");
187
188/// <http://www.w3.org/2001/XMLSchema#IDREFS> datatype IRI.
189pub const XSD_IDREFS: &Iri = iri!("http://www.w3.org/2001/XMLSchema#IDREFS");
190
191/// <http://www.w3.org/2001/XMLSchema#ENTITIES> datatype IRI.
192pub const XSD_ENTITIES: &Iri = iri!("http://www.w3.org/2001/XMLSchema#ENTITIES");
193
194/// <http://www.w3.org/2001/XMLSchema#integer> datatype IRI.
195pub const XSD_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#integer");
196
197/// <http://www.w3.org/2001/XMLSchema#nonPositiveInteger> datatype IRI.
198pub const XSD_NON_POSITIVE_INTEGER: &Iri =
199	iri!("http://www.w3.org/2001/XMLSchema#nonPositiveInteger");
200
201/// <http://www.w3.org/2001/XMLSchema#negativeInteger> datatype IRI.
202pub const XSD_NEGATIVE_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#negativeInteger");
203
204/// <http://www.w3.org/2001/XMLSchema#long> datatype IRI.
205pub const XSD_LONG: &Iri = iri!("http://www.w3.org/2001/XMLSchema#long");
206
207/// <http://www.w3.org/2001/XMLSchema#int> datatype IRI.
208pub const XSD_INT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#int");
209
210/// <http://www.w3.org/2001/XMLSchema#short> datatype IRI.
211pub const XSD_SHORT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#short");
212
213/// <http://www.w3.org/2001/XMLSchema#byte> datatype IRI.
214pub const XSD_BYTE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#byte");
215
216/// <http://www.w3.org/2001/XMLSchema#nonNegativeInteger> datatype IRI.
217pub const XSD_NON_NEGATIVE_INTEGER: &Iri =
218	iri!("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
219
220/// <http://www.w3.org/2001/XMLSchema#unsignedLong> datatype IRI.
221pub const XSD_UNSIGNED_LONG: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedLong");
222
223/// <http://www.w3.org/2001/XMLSchema#unsignedInt> datatype IRI.
224pub const XSD_UNSIGNED_INT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedInt");
225
226/// <http://www.w3.org/2001/XMLSchema#unsignedShort> datatype IRI.
227pub const XSD_UNSIGNED_SHORT: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedShort");
228
229/// <http://www.w3.org/2001/XMLSchema#unsignedByte> datatype IRI.
230pub const XSD_UNSIGNED_BYTE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#unsignedByte");
231
232/// <http://www.w3.org/2001/XMLSchema#positiveInteger> datatype IRI.
233pub const XSD_POSITIVE_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#positiveInteger");
234
235/// Parse a value directly from its XSD lexical form.
236pub trait ParseXsd: Sized {
237	type LexicalForm: LexicalFormOf<Self> + ?Sized;
238
239	fn parse_xsd(lexical_value: &str) -> ParseXsdResult<Self, Self::LexicalForm> {
240		Self::LexicalForm::parse(lexical_value)
241			.map_err(ParseXsdError::InvalidLexicalForm)?
242			.try_as_value()
243			.map_err(ParseXsdError::InvalidValue)
244	}
245}
246
247/// XSD lexical parse result.
248pub type ParseXsdResult<T, L> =
249	Result<T, ParseXsdError<<L as Lexical>::Error, <L as LexicalFormOf<T>>::ValueError>>;
250
251/// XSD lexical parse error.
252pub enum ParseXsdError<L, V> {
253	InvalidLexicalForm(L),
254	InvalidValue(V),
255}