xapi_rs/data/
about.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3use crate::{MyError, MyVersion, data::Extensions};
4use core::fmt;
5use serde::{Deserialize, Serialize};
6use serde_with::skip_serializing_none;
7use std::str::FromStr;
8
9/// Structure containing information about an LRS, including supported
10/// extensions and xAPI version(s).
11#[derive(Debug, Deserialize, PartialEq, Serialize)]
12#[skip_serializing_none]
13pub struct About {
14    // IMPORTANT (rsn) 20240526 - this field is set as a Vector of String and
15    // not a Vector of MyVersion b/c it's used in higher level layers that do
16    // not need to concern themselves w/ how i work around the constraint re.
17    // serialization imposed by the semver::Version.
18    #[serde(rename = "version")]
19    versions: Vec<String>,
20    extensions: Option<Extensions>,
21}
22
23impl About {
24    /// Construct a new instance.
25    pub fn new(versions: Vec<MyVersion>, extensions: Extensions) -> Self {
26        let versions = versions.iter().map(|x| x.to_string()).collect();
27        let extensions = if extensions.is_empty() {
28            None
29        } else {
30            Some(extensions)
31        };
32        About {
33            versions,
34            extensions,
35        }
36    }
37
38    /// Return the list of supported xAPI versions.
39    pub fn versions(&self) -> Result<Vec<MyVersion>, MyError> {
40        let mut vec = vec![];
41        for x in self.versions.iter() {
42            vec.push(MyVersion::from_str(x)?);
43        }
44        Ok(vec)
45    }
46
47    /// Return supported [Extensions].
48    pub fn extensions(&self) -> Option<&Extensions> {
49        self.extensions.as_ref()
50    }
51}
52
53impl fmt::Display for About {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        let mut vec = vec![];
56        vec.push(format!(
57            "versions: [{}]",
58            &self
59                .versions
60                .iter()
61                .map(|x| x.to_string())
62                .collect::<Vec<_>>()
63                .join(", ")
64        ));
65        if self.extensions.is_some() {
66            vec.push(format!("extensions: {}", self.extensions.as_ref().unwrap()))
67        }
68        let res = vec
69            .iter()
70            .map(|x| x.to_string())
71            .collect::<Vec<_>>()
72            .join(", ");
73        write!(f, "About{{ {res} }}")
74    }
75}