From dd67745e4d2ed74a7e5fdba09c173c824ef742d5 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 11 Nov 2022 09:34:37 +0100 Subject: [PATCH] events: Add convenience constructors for MessageType --- crates/ruma-common/src/events/room/message.rs | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/crates/ruma-common/src/events/room/message.rs b/crates/ruma-common/src/events/room/message.rs index b393a8bd..fe3fb852 100644 --- a/crates/ruma-common/src/events/room/message.rs +++ b/crates/ruma-common/src/events/room/message.rs @@ -73,34 +73,34 @@ impl RoomMessageEventContent { /// A constructor to create a plain text message. pub fn text_plain(body: impl Into) -> Self { - Self::new(MessageType::Text(TextMessageEventContent::plain(body))) + Self::new(MessageType::text_plain(body)) } /// A constructor to create an html message. pub fn text_html(body: impl Into, html_body: impl Into) -> Self { - Self::new(MessageType::Text(TextMessageEventContent::html(body, html_body))) + Self::new(MessageType::text_html(body, html_body)) } /// A constructor to create a markdown message. #[cfg(feature = "markdown")] pub fn text_markdown(body: impl AsRef + Into) -> Self { - Self::new(MessageType::Text(TextMessageEventContent::markdown(body))) + Self::new(MessageType::text_markdown(body)) } /// A constructor to create a plain text notice. pub fn notice_plain(body: impl Into) -> Self { - Self::new(MessageType::Notice(NoticeMessageEventContent::plain(body))) + Self::new(MessageType::notice_plain(body)) } /// A constructor to create an html notice. pub fn notice_html(body: impl Into, html_body: impl Into) -> Self { - Self::new(MessageType::Notice(NoticeMessageEventContent::html(body, html_body))) + Self::new(MessageType::notice_html(body, html_body)) } /// A constructor to create a markdown notice. #[cfg(feature = "markdown")] pub fn notice_markdown(body: impl AsRef + Into) -> Self { - Self::new(MessageType::Notice(NoticeMessageEventContent::markdown(body))) + Self::new(MessageType::notice_markdown(body)) } /// Turns `self` into a reply to the given message. @@ -458,6 +458,38 @@ impl MessageType { }) } + /// A constructor to create a plain text message. + pub fn text_plain(body: impl Into) -> Self { + Self::Text(TextMessageEventContent::plain(body)) + } + + /// A constructor to create an html message. + pub fn text_html(body: impl Into, html_body: impl Into) -> Self { + Self::Text(TextMessageEventContent::html(body, html_body)) + } + + /// A constructor to create a markdown message. + #[cfg(feature = "markdown")] + pub fn text_markdown(body: impl AsRef + Into) -> Self { + Self::Text(TextMessageEventContent::markdown(body)) + } + + /// A constructor to create a plain text notice. + pub fn notice_plain(body: impl Into) -> Self { + Self::Notice(NoticeMessageEventContent::plain(body)) + } + + /// A constructor to create an html notice. + pub fn notice_html(body: impl Into, html_body: impl Into) -> Self { + Self::Notice(NoticeMessageEventContent::html(body, html_body)) + } + + /// A constructor to create a markdown notice. + #[cfg(feature = "markdown")] + pub fn notice_markdown(body: impl AsRef + Into) -> Self { + Self::Notice(NoticeMessageEventContent::markdown(body)) + } + /// Returns a reference to the `msgtype` string. pub fn msgtype(&self) -> &str { match self {