events: Update types according to changes in MSC3554

This commit is contained in:
Kévin Commaille 2023-01-05 13:37:50 +01:00 committed by Kévin Commaille
parent 08aa2ca04c
commit fbf99fcc53
2 changed files with 29 additions and 15 deletions

View File

@ -265,10 +265,16 @@ pub struct TextRepresentation {
/// ///
/// This must be a valid language code according to [BCP 47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt). /// This must be a valid language code according to [BCP 47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt).
/// ///
/// This is optional and defaults to `en`.
///
/// [MSC3554]: https://github.com/matrix-org/matrix-spec-proposals/pull/3554 /// [MSC3554]: https://github.com/matrix-org/matrix-spec-proposals/pull/3554
#[cfg(feature = "unstable-msc3554")] #[cfg(feature = "unstable-msc3554")]
#[serde(skip_serializing_if = "Option::is_none")] #[serde(
pub lang: Option<String>, rename = "org.matrix.msc3554.lang",
default = "Text::default_lang",
skip_serializing_if = "Text::is_default_lang"
)]
pub lang: String,
} }
impl TextRepresentation { impl TextRepresentation {
@ -278,7 +284,7 @@ impl TextRepresentation {
mimetype: mimetype.into(), mimetype: mimetype.into(),
body: body.into(), body: body.into(),
#[cfg(feature = "unstable-msc3554")] #[cfg(feature = "unstable-msc3554")]
lang: None, lang: Self::default_lang(),
} }
} }
@ -309,4 +315,12 @@ impl TextRepresentation {
fn is_default_mimetype(mime: &str) -> bool { fn is_default_mimetype(mime: &str) -> bool {
mime == "text/plain" mime == "text/plain"
} }
fn default_lang() -> String {
"en".to_owned()
}
fn is_default_lang(lang: &str) -> bool {
lang == "en"
}
} }

View File

@ -315,18 +315,18 @@ fn emote_event_deserialization() {
#[cfg(feature = "unstable-msc3554")] #[cfg(feature = "unstable-msc3554")]
fn lang_serialization() { fn lang_serialization() {
let content = TextContentBlock::try_from(vec![ let content = TextContentBlock::try_from(vec![
assign!(TextRepresentation::plain("Bonjour le monde !"), { lang: Some("fr".into()) }), assign!(TextRepresentation::plain("Bonjour le monde !"), { lang: "fr".into() }),
assign!(TextRepresentation::plain("Hallo Welt!"), { lang: Some("de".into()) }), assign!(TextRepresentation::plain("Hallo Welt!"), { lang: "de".into() }),
assign!(TextRepresentation::plain("Hello World!"), { lang: Some("en".into()) }), assign!(TextRepresentation::plain("Hello World!"), { lang: "en".into() }),
]) ])
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
to_json_value(content).unwrap(), to_json_value(content).unwrap(),
json!([ json!([
{ "body": "Bonjour le monde !", "lang": "fr"}, { "body": "Bonjour le monde !", "org.matrix.msc3554.lang": "fr"},
{ "body": "Hallo Welt!", "lang": "de"}, { "body": "Hallo Welt!", "org.matrix.msc3554.lang": "de"},
{ "body": "Hello World!", "lang": "en"}, { "body": "Hello World!"},
]) ])
); );
} }
@ -335,15 +335,15 @@ fn lang_serialization() {
#[cfg(feature = "unstable-msc3554")] #[cfg(feature = "unstable-msc3554")]
fn lang_deserialization() { fn lang_deserialization() {
let json_data = json!([ let json_data = json!([
{ "body": "Bonjour le monde !", "lang": "fr"}, { "body": "Bonjour le monde !", "org.matrix.msc3554.lang": "fr"},
{ "body": "Hallo Welt!", "lang": "de"}, { "body": "Hallo Welt!", "org.matrix.msc3554.lang": "de"},
{ "body": "Hello World!", "lang": "en"}, { "body": "Hello World!"},
]); ]);
let content = from_json_value::<TextContentBlock>(json_data).unwrap(); let content = from_json_value::<TextContentBlock>(json_data).unwrap();
assert_eq!(content[0].lang.as_deref(), Some("fr")); assert_eq!(content[0].lang, "fr");
assert_eq!(content[1].lang.as_deref(), Some("de")); assert_eq!(content[1].lang, "de");
assert_eq!(content[2].lang.as_deref(), Some("en")); assert_eq!(content[2].lang, "en");
} }
#[test] #[test]