1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// SPDX-License-Identifier: GPL-3.0-or-later
//! This LIBRARY consists of Rust bindings for the [IEEE Std 9274.1.1][101], IEEE Standard
//! for Learning Technology— JavaScript Object Notation (JSON) Data Model Format and
//! Representational State Transfer (RESTful) Web Service for Learner Experience Data Tracking
//! and Access.
//!
//! The standard describes a JSON[^101] data model format and a RESTful[^102] Web Service
//! API[^103] for communication between Activities experienced by an individual, group, or other
//! entity and an LRS[^104]. The LRS is a system that exposes the RESTful Web Service API for
//! the purpose of tracking and accessing experiential data, especially in learning and human
//! performance.
//!
//! In this document, "xAPI" means the collection of published documents found
//! [here](https://opensource.ieee.org/xapi>).
//!
//!
//! ## The [`Validate`] trait
//! Types defined in this library rely on [`serde`][102] and a couple of other libraries to
//! deserialize xAPI data. Unit and Integration Tests for these types use the published
//! [examples][103] to _partly_ ensure their correctness in at least they will consume
//! the input stream and will produce instances of those types that can be later manipulated.
//!
//! I said partly b/c not all the _rules_ specified by the specifications can or are encoded
//! in the techniques used for unmarshelling the input data stream.
//!
//! For example when xAPI specifies that a property must be an IRL the corresponding field is
//! defined as an [`IriString`][104]. But an _IRL_ is not just an _IRI_! As xAPI (3. Definitions,
//! acronyms, and abbreviations) states...
//!
//! > _...an IRL is an IRI that when translated into a URI (per the IRI to
//! > URI rules), is a URL._
//!
//! Unfortunately the [`iri-string`][105] library does not offer out-of-the-box support for
//! _IRLs_.
//!
//! Another example of the limitations of solely relying on [`serde`][102] for instantiating
//! _correct_ types is the email address (`mbox`) property of [Agent]s and [Group]s. xAPI (4.2.2.1
//! Actor) states that an `mbox` is a...
//!
//! > _mailto IRI. The required format is "mailto:email address"._
//!
//! For those reasons a [`Validate`] trait is defined and implemented to ensure that an instance
//! of a type that implements this trait **is** valid, in that it satisfies the xAPI constraints,
//! if + when it passes the `validate()` call.
//!
//! If a `validate()` call returns `None` when it shouldn't it's a bug.
//!
//! ## Equality and Equivalence - The [`Fingerprint`] trait
//! There's the classical _Equality_ concept ubiquitous in software that deals w/ object Equality.
//! That concept affects (in Rust) the `Hash`, `PartialEq` and `Eq` _Traits_. Ensuring that our
//! xAPI Data Types implement those Traits mean they can be used as Keys in `HashMap`s and distinct
//! elements in `HashSet`s.
//!
//! The xAPI describes (and relies) on a concept of _Equivalence_ that determines if two instances
//! of the same Data Type (say [Statement]s) are equal. That _Equivalence_ is **different** from
//! _Equality_ introduced earlier. So it's possible to have two [Statement]s that have different
//! `hash`[^12] values yet are _equivalent_. Note though that if two [Statement]s (and more
//! generally two instances of the same Data Type) are **equal** then they're also **equivalent**.
//! In other words, _Equality_ implies _Equivalence_ but not the other way around.
//!
//! To satisfy _Equivalence_ between instances of a Data Type we introduce a [Fingerprint] Trait.
//! The _required_ function of this Trait; i.e. [`fingerprint()`][crate::Fingerprint#fingerprint],
//! is used to test for _Equivalence_ between two instances of the same Data Type.
//!
//! For most xAPI Data Types, both the `hash` and `fingerprint` functions yield the same result.
//! When they differ the _Equivalence_ only considers properties the xAPI standard qualifies as
//! **_preserving immutability requirements_**, for example for [Statement]s those are...
//!
//! * [Actor], except the ordering of Group members,
//! * [Verb], except for `display` property,
//! * [Object][107],
//! * Duration, excluding precision beyond 0.01 second.
//!
//! Note though that even when they yield the same result, the implementations take into
//! consideration the following constraints --and hence apply the required conversions before
//! the test for equality...
//!
//! * Case-insensitive string comparisons when the property can be safely compared this way
//! such as the `mbox` (email address) of an [Actor] but not the `name` of an [Account].
//! * IRI Normalization by first splitting it into _Absolute_ and _Fragment_ parts then
//! [_normalizing_][108] the _Absolute_ part before hashing the two in sequence.
//!
//!
//! ## The [`Canonical`] trait
//! xAPI requires LRS implementations to sometimes produce results in _canonical_ form --See
//! [Language Filtering Requirements for Canonical Format Statements][109] for example.
//!
//! This trait defines a method implemented by types required to produce such format.
//!
//!
//! ## Getters, Builders and Setters
//! Once a type is instantiated, access to any of its fields --sometimes referred to in the
//! documentation as _properties_ using the _camel case_ form mostly used in xAPI-- is done
//! through methods that mirror the Rust field names of the structures representing said types.
//!
//! For example the _homePage_ property of an [Account] is obtained by calling the method
//! `home_page()` of an [Account] instance which returns a reference to the IRI string as
//! `&IriStr`.
//!
//! Sometimes however it is convenient to access the field as another type. Using the same
//! example as above, the [Account] implementation offers a `home_page_as_str()` which returns
//! a reference to the same field as `&str`.
//!
//! This pattern is generalized thoughtout this library.
//!
//! The library so far, except for rare use-cases, does NOT offer setters for any type field.
//! Creating new instances of types by hand --as opposed to deserializing (from the wire)-- is
//! done by (a) instantiating a _Builder_ for a type, (b) calling the _Builder_ setters (using
//! the same field names as those of the to-be built type) to set the desired values, and when
//! ready, (c) calling the `build()` method.
//!
//! _Builders_ signal the occurrence of errors by returning a `Result` w/ the error part being
//! a [DataError] instance. Here's an example...
//!
//! ```rust
//! # use core::result::Result;
//! # use xapi_data::{Account, DataError};
//! # fn dummy() -> Result<(), DataError> {
//! let act = Account::builder()
//! .home_page("https://inter.net/login")?
//! .name("example")?
//! .build()?;
//! // ...
//! assert_eq!(act.home_page_as_str(), "https://inter.net/login");
//! assert_eq!(act.name(), "example");
//! # Ok(())
//! # }
//! ```
//!
//!
//! ## Naming
//! Naming properties in xAPI _Objects_ is inconsistent. Sometimes the singular form is used
//! to refer to a collection of items; e.g. _member_ instead of _members_ when referring to a
//! [Group]'s list of [Agent]s. In other places the plural form is correctly used; e.g.
//! [_attachments_][Attachment] in a [SubStatement], or [_extensions_][Extensions] everywhere
//! it's referenced.
//!
//! We tried to be consistent in naming the fields of the corresponding types while ensuring that
//! their serialization to, and deserialization from, streams respect the label assigned to them
//! in xAPI and backed by the accompanying examples. So to access a [Group]'s [Agent]s one would
//! call `members()`. To add an [Agent] to a [Group] one would call `member()` on a [GroupBuilder].
//!
//! [101]: https://opensource.ieee.org/xapi/xapi-base-standard-documentation
//! [102]: https://crates.io/crates/serde
//! [103]: https://opensource.ieee.org/xapi/xapi-base-standard-examples
//! [104]: https://docs.rs/iri-string/0.7.2/iri_string/types/type.IriString.html
//! [105]: https://crates.io/crates/iri-string
//! [106]: https://dotat.at/tmp/ISO_8601-2004_E.pdf
//! [107]: crate::StatementObject
//! [108]: <https://www.rfc-editor.org/rfc/rfc3987#section-5>
//! [109]: https://opensource.ieee.org/xapi/xapi-base-standard-documentation/-/blob/main/9274.1.1%20xAPI%20Base%20Standard%20for%20LRSs.md#language-filtering-requirements-for-canonical-format-statements
//!
//! [^101]: JSON: JavaScript Object Notation.
//! [^102]: REST: Representational State Transfer.
//! [^103]: API: Application Programming Interface.
//! [^104]: LRS: Learning Record Store.
//! [^10]: Durations in [ISO 8601:2004(E)][106] sections 4.4.3.2 and 4.4.3.3.
//! [^12]: Just to be clear, `hash` here means the result of computing a message digest over the non-null values of an object's field(s).
//!
pub use *;
use ;
use Serializer;
use Value;
/// Given `$map` (a [LanguageMap] dictionary) insert `$label` keyed by `$tag`
/// creating the collection in the process if it was `None`.
///
/// Raise [LanguageTag][1] error if the `tag` is invalid.
///
/// Example
/// ```rust
/// # use core::result::Result;
/// # use std::str::FromStr;
/// # use xapi_data::{DataError, add_language, LanguageMap, MyLanguageTag};
/// # fn main() -> Result<(), DataError> {
/// let mut greetings = None;
/// let en = MyLanguageTag::from_str("en")?;
/// add_language!(greetings, &en, "Hello");
///
/// assert_eq!(greetings.unwrap().get(&en).unwrap(), "Hello");
/// # Ok(())
/// # }
/// ```
///
/// [1]: crate::DataError#variant.LanguageTag
/// Both [Agent] and [Group] have an `mbox` property which captures an _email
/// address_. This macro eliminates duplication of the logic involved in (a)
/// parsing an argument `$val` into a valid [EmailAddress][1], (b) raising a
/// [DataError] if an error occurs, (b) assigning the result when successful
/// to the appropriate field of the given `$builder` instance, and (c) resetting
/// the other three IFI (Inverse Functional Identifier) fields to `None`.
///
/// [1]: [email_address::EmailAddress]
/// Given `dst` and `src` as two [BTreeMap][1]s wrapped in [Option], replace
/// or augment `dst`' entries w/ `src`'s.
///
/// [1]: std::collections::BTreeMap
/// Recursively check if a JSON Object contains 'null' values.
/// A Serializer implementation that ensures `stored` timestamps show
/// milli-second precision.
/// Generate a message (in the style of `format!` macro), log it at level
/// _error_ and raise a [runtime error][crate::DataError#variant.Runtime].
/// Log `$err` at level _error_ before returning it.