From af58063c7bea7572faa05269753322b678a5181b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 16 Mar 2022 11:34:39 +0100 Subject: [PATCH] common: Reorder events::message types --- crates/ruma-common/src/events/message.rs | 100 +++++++++++------------ 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/crates/ruma-common/src/events/message.rs b/crates/ruma-common/src/events/message.rs index 829b7e4f..36dbdd03 100644 --- a/crates/ruma-common/src/events/message.rs +++ b/crates/ruma-common/src/events/message.rs @@ -11,44 +11,40 @@ use content_serde::MessageContentSerDeHelper; use super::room::message::Relation; -/// Text message content. -#[derive(Clone, Debug, Serialize, Deserialize)] +/// The payload for an extensible text message. +#[derive(Clone, Debug, Serialize, Deserialize, EventContent)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct Text { - /// The mime type of the `body`. - #[serde(default = "Text::default_mimetype")] - pub mimetype: String, +#[ruma_event(type = "m.message", kind = MessageLike)] +pub struct MessageEventContent { + /// The message's text content. + #[serde(flatten)] + pub message: MessageContent, - /// The text content. - pub body: String, + /// Information about related messages for [rich replies]. + /// + /// [rich replies]: https://spec.matrix.org/v1.2/client-server-api/#rich-replies + #[serde(flatten, skip_serializing_if = "Option::is_none")] + pub relates_to: Option, } -impl Text { - /// Creates a new plain text message body. +impl MessageEventContent { + /// A convenience constructor to create a plain text message. pub fn plain(body: impl Into) -> Self { - Self { mimetype: "text/plain".to_owned(), body: body.into() } + Self { message: MessageContent::plain(body), relates_to: None } } - /// Creates a new HTML-formatted message body. - pub fn html(body: impl Into) -> Self { - Self { mimetype: "text/html".to_owned(), body: body.into() } + /// A convenience constructor to create an HTML message. + pub fn html(body: impl Into, html_body: impl Into) -> Self { + Self { message: MessageContent::html(body, html_body), relates_to: None } } - /// Creates a new HTML-formatted message body by parsing the Markdown in `body`. + /// A convenience constructor to create a Markdown message. /// - /// Returns `None` if no Markdown formatting was found. + /// Returns an HTML message if some Markdown formatting was detected, otherwise returns a plain + /// text message. #[cfg(feature = "markdown")] - pub fn markdown(body: impl AsRef) -> Option { - let body = body.as_ref(); - let mut html_body = String::new(); - - pulldown_cmark::html::push_html(&mut html_body, pulldown_cmark::Parser::new(body)); - - (html_body != format!("

{}

\n", body)).then(|| Self::html(html_body)) - } - - fn default_mimetype() -> String { - "text/plain".to_owned() + pub fn markdown(body: impl AsRef + Into) -> Self { + Self { message: MessageContent::markdown(body), relates_to: None } } } @@ -104,39 +100,43 @@ impl MessageContent { } } -/// The payload for an extensible text message. -#[derive(Clone, Debug, Serialize, Deserialize, EventContent)] +/// Text message content. +#[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[ruma_event(type = "m.message", kind = MessageLike)] -pub struct MessageEventContent { - /// The message's text content. - #[serde(flatten)] - pub message: MessageContent, +pub struct Text { + /// The mime type of the `body`. + #[serde(default = "Text::default_mimetype")] + pub mimetype: String, - /// Information about related messages for [rich replies]. - /// - /// [rich replies]: https://spec.matrix.org/v1.2/client-server-api/#rich-replies - #[serde(flatten, skip_serializing_if = "Option::is_none")] - pub relates_to: Option, + /// The text content. + pub body: String, } -impl MessageEventContent { - /// A convenience constructor to create a plain text message. +impl Text { + /// Creates a new plain text message body. pub fn plain(body: impl Into) -> Self { - Self { message: MessageContent::plain(body), relates_to: None } + Self { mimetype: "text/plain".to_owned(), body: body.into() } } - /// A convenience constructor to create an HTML message. - pub fn html(body: impl Into, html_body: impl Into) -> Self { - Self { message: MessageContent::html(body, html_body), relates_to: None } + /// Creates a new HTML-formatted message body. + pub fn html(body: impl Into) -> Self { + Self { mimetype: "text/html".to_owned(), body: body.into() } } - /// A convenience constructor to create a Markdown message. + /// Creates a new HTML-formatted message body by parsing the Markdown in `body`. /// - /// Returns an HTML message if some Markdown formatting was detected, otherwise returns a plain - /// text message. + /// Returns `None` if no Markdown formatting was found. #[cfg(feature = "markdown")] - pub fn markdown(body: impl AsRef + Into) -> Self { - Self { message: MessageContent::markdown(body), relates_to: None } + pub fn markdown(body: impl AsRef) -> Option { + let body = body.as_ref(); + let mut html_body = String::new(); + + pulldown_cmark::html::push_html(&mut html_body, pulldown_cmark::Parser::new(body)); + + (html_body != format!("

{}

\n", body)).then(|| Self::html(html_body)) + } + + fn default_mimetype() -> String { + "text/plain".to_owned() } }