use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
mod params_serde;
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct LoginTermsParams {
pub policies: BTreeMap<String, PolicyDefinition>,
}
impl LoginTermsParams {
pub fn new(policies: BTreeMap<String, PolicyDefinition>) -> Self {
Self { policies }
}
}
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PolicyDefinition {
pub version: String,
#[serde(flatten)]
pub translations: BTreeMap<String, PolicyTranslation>,
}
impl PolicyDefinition {
pub fn new(version: String, translations: BTreeMap<String, PolicyTranslation>) -> Self {
Self { version, translations }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PolicyTranslation {
pub name: String,
pub url: String,
}
impl PolicyTranslation {
pub fn new(name: String, url: String) -> Self {
Self { name, url }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct OAuthParams {
pub url: String,
}
impl OAuthParams {
pub fn new(url: String) -> Self {
Self { url }
}
}
#[cfg(test)]
mod tests {
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{LoginTermsParams, PolicyDefinition, PolicyTranslation};
#[test]
fn serialize_login_terms_params() {
let privacy_definition = PolicyDefinition::new(
"1".to_owned(),
[
(
"en-US".to_owned(),
PolicyTranslation::new(
"Privacy Policy".to_owned(),
"http://matrix.local/en-US/privacy".to_owned(),
),
),
(
"fr-FR".to_owned(),
PolicyTranslation::new(
"Politique de confidentialité".to_owned(),
"http://matrix.local/fr-FR/privacy".to_owned(),
),
),
]
.into(),
);
let params = LoginTermsParams::new([("privacy".to_owned(), privacy_definition)].into());
assert_eq!(
to_json_value(¶ms).unwrap(),
json!({
"policies": {
"privacy": {
"en-US": {
"name": "Privacy Policy",
"url": "http://matrix.local/en-US/privacy",
},
"fr-FR": {
"name": "Politique de confidentialité",
"url": "http://matrix.local/fr-FR/privacy",
},
"version": "1",
},
},
})
);
}
#[test]
fn deserialize_login_terms_params() {
let json = json!({
"policies": {
"privacy": {
"en-US": {
"name": "Privacy Policy",
"url": "http://matrix.local/en-US/privacy",
},
"fr-FR": {
"name": "Politique de confidentialité",
"url": "http://matrix.local/fr-FR/privacy",
},
},
},
});
from_json_value::<LoginTermsParams>(json).unwrap_err();
let json = json!({
"policies": {
"privacy_policy": {
"en": {
"name": "Privacy Policy",
"url": "https://example.org/somewhere/privacy-1.2-en.html"
},
"fr": {
"name": "Politique de confidentialité",
"url": "https://example.org/somewhere/privacy-1.2-fr.html"
},
"foo": "bar",
"version": "1.2",
},
"terms_of_service": {
"version": "1.2",
}
}
});
let params = from_json_value::<LoginTermsParams>(json).unwrap();
assert_eq!(params.policies.len(), 2);
let policy = params.policies.get("privacy_policy").unwrap();
assert_eq!(policy.version, "1.2");
assert_eq!(policy.translations.len(), 2);
let translation = policy.translations.get("en").unwrap();
assert_eq!(translation.name, "Privacy Policy");
assert_eq!(translation.url, "https://example.org/somewhere/privacy-1.2-en.html");
let translation = policy.translations.get("fr").unwrap();
assert_eq!(translation.name, "Politique de confidentialité");
assert_eq!(translation.url, "https://example.org/somewhere/privacy-1.2-fr.html");
let policy = params.policies.get("terms_of_service").unwrap();
assert_eq!(policy.version, "1.2");
assert_eq!(policy.translations.len(), 0);
}
}