common: Add support for translatable text content.
According to MSC3554
This commit is contained in:
parent
af58063c7b
commit
1f53f38b4f
@ -39,6 +39,7 @@ unstable-msc3488 = ["unstable-msc1767"]
|
||||
unstable-msc3551 = ["unstable-msc1767"]
|
||||
unstable-msc3552 = ["unstable-msc1767", "unstable-msc3551"]
|
||||
unstable-msc3553 = ["unstable-msc3552"]
|
||||
unstable-msc3554 = ["unstable-msc1767"]
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.13.0"
|
||||
|
@ -100,6 +100,12 @@ impl MessageContent {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Text>> for MessageContent {
|
||||
fn from(variants: Vec<Text>) -> Self {
|
||||
Self(variants)
|
||||
}
|
||||
}
|
||||
|
||||
/// Text message content.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
@ -110,17 +116,34 @@ pub struct Text {
|
||||
|
||||
/// The text content.
|
||||
pub body: String,
|
||||
|
||||
/// The language of the text.
|
||||
///
|
||||
/// This must be a valid language code according to [BCP 47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt).
|
||||
#[cfg(feature = "unstable-msc3554")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub lang: Option<String>,
|
||||
}
|
||||
|
||||
impl Text {
|
||||
/// Creates a new plain text message body.
|
||||
pub fn plain(body: impl Into<String>) -> Self {
|
||||
Self { mimetype: "text/plain".to_owned(), body: body.into() }
|
||||
Self {
|
||||
mimetype: "text/plain".to_owned(),
|
||||
body: body.into(),
|
||||
#[cfg(feature = "unstable-msc3554")]
|
||||
lang: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new HTML-formatted message body.
|
||||
pub fn html(body: impl Into<String>) -> Self {
|
||||
Self { mimetype: "text/html".to_owned(), body: body.into() }
|
||||
Self {
|
||||
mimetype: "text/html".to_owned(),
|
||||
body: body.into(),
|
||||
#[cfg(feature = "unstable-msc3554")]
|
||||
lang: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new HTML-formatted message body by parsing the Markdown in `body`.
|
||||
|
@ -354,3 +354,45 @@ fn emote_event_deserialization() {
|
||||
&& unsigned.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "unstable-msc3554")]
|
||||
fn lang_serialization() {
|
||||
use ruma_common::events::message::{MessageContent, Text};
|
||||
|
||||
let content = MessageContent::from(vec![
|
||||
assign!(Text::plain("Bonjour le monde !"), { lang: Some("fr".into()) }),
|
||||
assign!(Text::plain("Hallo Welt!"), { lang: Some("de".into()) }),
|
||||
assign!(Text::plain("Hello World!"), { lang: Some("en".into()) }),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&content).unwrap(),
|
||||
json!({
|
||||
"org.matrix.msc1767.message": [
|
||||
{ "body": "Bonjour le monde !", "mimetype": "text/plain", "lang": "fr"},
|
||||
{ "body": "Hallo Welt!", "mimetype": "text/plain", "lang": "de"},
|
||||
{ "body": "Hello World!", "mimetype": "text/plain", "lang": "en"},
|
||||
]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "unstable-msc3554")]
|
||||
fn lang_deserialization() {
|
||||
use ruma_common::events::message::MessageContent;
|
||||
|
||||
let json_data = json!({
|
||||
"org.matrix.msc1767.message": [
|
||||
{ "body": "Bonjour le monde !", "mimetype": "text/plain", "lang": "fr"},
|
||||
{ "body": "Hallo Welt!", "mimetype": "text/plain", "lang": "de"},
|
||||
{ "body": "Hello World!", "mimetype": "text/plain", "lang": "en"},
|
||||
]
|
||||
});
|
||||
|
||||
let content = from_json_value::<MessageContent>(json_data).unwrap();
|
||||
assert_eq!(content.variants()[0].lang.as_deref(), Some("fr"));
|
||||
assert_eq!(content.variants()[1].lang.as_deref(), Some("de"));
|
||||
assert_eq!(content.variants()[2].lang.as_deref(), Some("en"));
|
||||
}
|
||||
|
@ -129,6 +129,7 @@ unstable-msc3488 = [
|
||||
unstable-msc3551 = ["ruma-common/unstable-msc3551"]
|
||||
unstable-msc3552 = ["ruma-common/unstable-msc3552"]
|
||||
unstable-msc3553 = ["ruma-common/unstable-msc3553"]
|
||||
unstable-msc3554 = ["ruma-common/unstable-msc3554"]
|
||||
unstable-msc3618 = ["ruma-federation-api/unstable-msc3618"]
|
||||
unstable-msc3723 = ["ruma-federation-api/unstable-msc3723"]
|
||||
|
||||
@ -147,6 +148,7 @@ __ci = [
|
||||
"unstable-msc3551",
|
||||
"unstable-msc3552",
|
||||
"unstable-msc3553",
|
||||
"unstable-msc3554",
|
||||
"unstable-msc3618",
|
||||
"unstable-msc3723",
|
||||
"ruma-state-res/__ci",
|
||||
|
Loading…
x
Reference in New Issue
Block a user