xapi-rs 0.1.22

A conformant LRS implementation of xAPI 2.0.0
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::{
    MyLanguageTag, add_language,
    data::{
        DataError, LanguageMap, Validate, ValidationError,
        validate::{validate_irl, validate_sha2},
    },
    emit_error,
};
use core::fmt;
use iri_string::types::{IriStr, IriString};
use mime::Mime;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use std::str::FromStr;
use tracing::warn;

/// Mandated 'usageTpe' to use when an [Attachment] is a JWS signature.
pub const SIGNATURE_UT: &str = "http://adlnet.gov/expapi/attachments/signature";
/// Mandated 'contentType' to use when an [Attachment] is a JWS signature.
pub const SIGNATURE_CT: &str = "application/octet-stream";

/// Structure representing an important piece of data that is part of a
/// _Learning Record_. Could be an essay, a video, etc...
///
/// Another example could be the image of a certificate that was granted as a
/// result of an experience.
#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Attachment {
    usage_type: IriString,
    display: LanguageMap,
    description: Option<LanguageMap>,
    #[serde_as(as = "serde_with::DisplayFromStr")]
    content_type: Mime,
    length: i64,
    sha2: String,
    file_url: Option<IriString>,
}

impl Attachment {
    /// Return an [Attachment] _Builder_.
    pub fn builder() -> AttachmentBuilder<'static> {
        AttachmentBuilder::default()
    }

    /// Return `usage_type` as an IRI.
    pub fn usage_type(&self) -> &IriStr {
        self.usage_type.as_ref()
    }

    /// Return `display` for the given language `tag` if it exists; `None` otherwise.
    pub fn display(&self, tag: &MyLanguageTag) -> Option<&str> {
        self.display.get(tag)
    }

    /// Return a reference to [`display`][LanguageMap].
    pub fn display_as_map(&self) -> &LanguageMap {
        &self.display
    }

    /// Return `description` for the given language `tag` if it exists; `None`
    /// otherwise.
    pub fn description(&self, tag: &MyLanguageTag) -> Option<&str> {
        match &self.description {
            Some(map) => map.get(tag),
            None => None,
        }
    }

    /// Return a reference to [`description`][LanguageMap] if set; `None` otherwise.
    pub fn description_as_map(&self) -> Option<&LanguageMap> {
        self.description.as_ref()
    }

    /// Return `content_type`.
    pub fn content_type(&self) -> &Mime {
        &self.content_type
    }

    /// Return `length` (in bytes).
    pub fn length(&self) -> i64 {
        self.length
    }

    /// Return `sha2` (hash sum).
    pub fn sha2(&self) -> &str {
        self.sha2.as_str()
    }

    /// Return `file_url` if set; `None` otherwise.
    pub fn file_url(&self) -> Option<&IriStr> {
        self.file_url.as_deref()
    }

    /// Return `file_url` as string reference if set; `None` otherwise.
    pub fn file_url_as_str(&self) -> Option<&str> {
        if let Some(z_file_url) = self.file_url.as_ref() {
            Some(z_file_url.as_ref())
        } else {
            None
        }
    }

    /// Set the `file_url` field to the given value.
    pub fn set_file_url(&mut self, url: &str) {
        self.file_url = Some(IriString::from_str(url).unwrap());
    }

    /// Return TRUE if this is a JWS signature; FALSE otherwise.
    pub fn is_signature(&self) -> bool {
        // an Attachment is considered a potential JWS Signature iff its
        // usage-type is equal to SIGNATURE_UT and its Content-Type is
        // equal to SIGNATURE_CT
        self.usage_type.as_str() == SIGNATURE_UT && self.content_type.as_ref() == SIGNATURE_CT
    }
}

impl fmt::Display for Attachment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut vec = vec![];

        vec.push(format!("usageType: \"{}\"", self.usage_type));
        vec.push(format!("display: {}", self.display));
        if let Some(z_description) = self.description.as_ref() {
            vec.push(format!("description: {}", z_description));
        }
        vec.push(format!("contentType: \"{}\"", self.content_type));
        vec.push(format!("length: {}", self.length));
        vec.push(format!("sha2: \"{}\"", self.sha2));
        if let Some(z_file_url) = self.file_url.as_ref() {
            vec.push(format!("fileUrl: \"{}\"", z_file_url));
        }

        let res = vec
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(", ");
        write!(f, "Attachment{{ {res} }}")
    }
}

impl Validate for Attachment {
    fn validate(&self) -> Vec<ValidationError> {
        let mut vec = vec![];

        if self.display.is_empty() {
            warn!("Attachment display dictionary is empty")
        }
        if self.content_type.type_().as_str().is_empty() {
            vec.push(ValidationError::Empty("content_type".into()))
        }
        if self.usage_type.is_empty() {
            vec.push(ValidationError::Empty("usage_type".into()))
        } else {
            // NOTE (rsn) 20241112 - before going further ensure if this is for
            // a JWS Signature, both UT and CT properties are consistent...
            if self.usage_type.as_str() == SIGNATURE_UT
                && self.content_type.as_ref() != SIGNATURE_CT
            {
                vec.push(ValidationError::ConstraintViolation(
                    "Attachment has a JWS Signature usage-type but not the expected content-type"
                        .into(),
                ));
            }
        }

        if self.sha2.is_empty() {
            vec.push(ValidationError::Empty("sha2".into()))
        } else {
            match validate_sha2(&self.sha2) {
                Ok(_) => (),
                Err(x) => vec.push(x),
            }
        }
        // length must be greater than 0...
        if self.length < 1 {
            vec.push(ValidationError::ConstraintViolation(
                "'length' should be > 0".into(),
            ))
        }
        if let Some(file_url) = self.file_url.as_ref() {
            if file_url.is_empty() {
                vec.push(ValidationError::ConstraintViolation(
                    "'file_url' when set, must not be empty".into(),
                ))
            } else {
                match validate_irl(file_url) {
                    Ok(_) => (),
                    Err(x) => vec.push(x),
                }
            }
        }

        vec
    }
}

/// A Type that knows how to construct an [Attachment].
#[derive(Debug, Default)]
pub struct AttachmentBuilder<'a> {
    _usage_type: Option<&'a IriStr>,
    _display: Option<LanguageMap>,
    _description: Option<LanguageMap>,
    _content_type: Option<Mime>,
    _length: Option<i64>,
    _sha2: &'a str,
    _file_url: Option<&'a IriStr>,
}

impl<'a> AttachmentBuilder<'a> {
    /// Set the `usage_type` field.
    ///
    /// Raise [DataError] if the input string is empty or when parsed as an
    /// IRI yields an invalid value.
    pub fn usage_type(mut self, val: &'a str) -> Result<Self, DataError> {
        let usage_type = val.trim();
        if usage_type.is_empty() {
            emit_error!(DataError::Validation(ValidationError::Empty(
                "usage_type".into()
            )))
        } else {
            let usage_type = IriStr::new(usage_type)?;
            self._usage_type = Some(usage_type);
            Ok(self)
        }
    }

    /// Add `label` tagged by the language `tag` to the `display` dictionary.
    ///
    /// Raise [DataError] if the `tag` was empty or invalid.
    pub fn display(mut self, tag: &MyLanguageTag, label: &str) -> Result<Self, DataError> {
        add_language!(self._display, tag, label);
        Ok(self)
    }

    /// Set (as in replace) the `display` property for the instance being built
    /// w/ the one passed as argument.
    pub fn with_display(mut self, map: LanguageMap) -> Result<Self, DataError> {
        self._display = Some(map);
        Ok(self)
    }

    /// Add `label` tagged by the language `tag` to the `description` dictionary.
    ///
    /// Raise [DataError] if the `tag` was empty or invalid.
    pub fn description(mut self, tag: &MyLanguageTag, label: &str) -> Result<Self, DataError> {
        add_language!(self._description, tag, label);
        Ok(self)
    }

    /// Set (as in replace) the `description` property for the instance being built
    /// w/ the one passed as argument.
    pub fn with_description(mut self, map: LanguageMap) -> Result<Self, DataError> {
        self._description = Some(map);
        Ok(self)
    }

    /// Set the `content_type` field.
    ///
    /// Raise [DataError] if the input string is empty, or is not a valid MIME
    /// type string.
    pub fn content_type(mut self, val: &str) -> Result<Self, DataError> {
        let val = val.trim();
        if val.is_empty() {
            emit_error!(DataError::Validation(ValidationError::Empty(
                "content_type".into()
            )))
        } else {
            let content_type = Mime::from_str(val)?;
            self._content_type = Some(content_type);
            Ok(self)
        }
    }

    /// Set the `length` field.
    pub fn length(mut self, val: i64) -> Result<Self, DataError> {
        if val < 1 {
            emit_error!(DataError::Validation(ValidationError::ConstraintViolation(
                "'length' should be > 0".into()
            )))
        } else {
            self._length = Some(val);
            Ok(self)
        }
    }

    /// Set the `sha2` field.
    ///
    /// Raise [DataError] if the input string is empty, has the wrong number
    /// of characters, or contains non-hexadecimal characters.
    pub fn sha2(mut self, val: &'a str) -> Result<Self, DataError> {
        let val = val.trim();
        if val.is_empty() {
            emit_error!(DataError::Validation(ValidationError::Empty("sha2".into())))
        } else {
            validate_sha2(val)?;
            self._sha2 = val;
            Ok(self)
        }
    }

    /// Set the `file_url` field.
    ///
    /// Raise [DataError] if the input string is empty, an error occurs while
    /// parsing it as an IRI, or the resulting IRI is an invalid URL.
    pub fn file_url(mut self, val: &'a str) -> Result<Self, DataError> {
        let file_url = val.trim();
        if file_url.is_empty() {
            emit_error!(DataError::Validation(ValidationError::Empty(
                "file_url".into()
            )))
        } else {
            let x = IriStr::new(file_url)?;
            validate_irl(x)?;
            self._file_url = Some(x);
            Ok(self)
        }
    }

    /// Create an [Attachment] instance from set field values.
    ///
    /// Raise a [DataError] if any required field is missing.
    pub fn build(&self) -> Result<Attachment, DataError> {
        if self._usage_type.is_none() {
            emit_error!(DataError::Validation(ValidationError::MissingField(
                "usage_type".into()
            )))
        }
        if self._length.is_none() {
            emit_error!(DataError::Validation(ValidationError::MissingField(
                "length".into()
            )))
        }
        if self._content_type.is_none() {
            emit_error!(DataError::Validation(ValidationError::MissingField(
                "content_type".into()
            )))
        }
        if self._sha2.is_empty() {
            emit_error!(DataError::Validation(ValidationError::MissingField(
                "sha2".into()
            )))
        }
        Ok(Attachment {
            usage_type: self._usage_type.unwrap().into(),
            display: self._display.to_owned().unwrap_or_default(),
            description: self._description.to_owned(),
            content_type: self._content_type.clone().unwrap(),
            length: self._length.unwrap(),
            sha2: self._sha2.to_owned(),
            file_url: self._file_url.map(|x| x.to_owned()),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tracing_test::traced_test;

    #[traced_test]
    #[test]
    fn test_serde_rename() -> Result<(), DataError> {
        const JSON: &str = r#"
        {
            "usageType": "http://adlnet.gov/expapi/attachments/signature",
            "display": { "en-US": "Signature" },
            "description": { "en-US": "A test signature" },
            "contentType": "application/octet-stream",
            "length": 4235,
            "sha2": "672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634"
        }"#;

        let en = MyLanguageTag::from_str("en")?;
        let us = MyLanguageTag::from_str("en-US")?;
        let au = MyLanguageTag::from_str("en-AU")?;

        let de_result = serde_json::from_str::<Attachment>(JSON);
        assert!(de_result.is_ok());
        let att = de_result.unwrap();

        assert_eq!(
            att.usage_type(),
            "http://adlnet.gov/expapi/attachments/signature"
        );
        assert!(att.display(&en).is_none());
        assert!(att.display(&us).is_some());
        assert_eq!(att.display(&us).unwrap(), "Signature");
        assert!(att.description(&au).is_none());
        assert!(att.description(&us).is_some());
        assert_eq!(att.description(&us).unwrap(), "A test signature");
        assert_eq!(att.content_type().to_string(), "application/octet-stream");
        assert_eq!(att.length(), 4235);
        assert_eq!(
            att.sha2(),
            "672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634"
        );
        assert!(att.file_url().is_none());

        Ok(())
    }

    #[traced_test]
    #[test]
    fn test_builder() -> Result<(), DataError> {
        let en = MyLanguageTag::from_str("en")?;

        let mut display = LanguageMap::new();
        display.insert(&en, "zDisplay");

        let mut description = LanguageMap::new();
        description.insert(&en, "zDescription");

        let builder = Attachment::builder()
            .usage_type("http://somewhere.net/attachment-usage/test")?
            .with_display(display)?
            .with_description(description)?
            .content_type("text/plain")?
            .length(99)?
            .sha2("495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a")?;
        let att = builder
            .file_url("https://localhost/xapi/static/c44/sAZH2_GCudIGDdvf0xgHtLA/a1")?
            .build()?;

        assert_eq!(att.content_type, "text/plain");

        Ok(())
    }
}