From 6fa6deddcb8e1c010723c5e8d0545adb909948b9 Mon Sep 17 00:00:00 2001 From: xmppftw Date: Sat, 3 Jun 2023 11:23:32 +0200 Subject: [PATCH] Message now has constructors for each type, and a with_body builder method (#78) --- parsers/ChangeLog | 2 ++ parsers/src/message.rs | 48 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/parsers/ChangeLog b/parsers/ChangeLog index c0e88c51..3e385c85 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -7,6 +7,8 @@ xxx - Correct cargo doc warnings - Presence now has constructors for each variant so you don't have to import presence::Type, where Presence::available represents type None (#79) - Presence::with_payload builds a payload into the presence (#79) + - Message now has constructors for each type ; Message::new still builds a Chat type (#78) + - Message::with_body builder method appends a body in a given language to the message (#78) * Breaking changes: - Removed the 'serde' feature. Add it directly by using 'jid'. `jid = { version = "*", features = ["serde"] }`. diff --git a/parsers/src/message.rs b/parsers/src/message.rs index 281543a2..e03578f3 100644 --- a/parsers/src/message.rs +++ b/parsers/src/message.rs @@ -94,7 +94,8 @@ pub struct Message { } impl Message { - /// Creates a new `` stanza for the given recipient. + /// Creates a new `` stanza of type Chat for the given recipient. + /// This is equivalent to the [`Message::chat`] method. pub fn new>>(to: J) -> Message { Message { from: None, @@ -108,6 +109,51 @@ impl Message { } } + /// Creates a new `` stanza of a certain type for the given recipient. + pub fn new_with_type>>(type_: MessageType, to: J) -> Message { + Message { + from: None, + to: to.into(), + id: None, + type_, + bodies: BTreeMap::new(), + subjects: BTreeMap::new(), + thread: None, + payloads: vec![], + } + } + + /// Creates a Message of type Chat + pub fn chat>>(to: J) -> Message { + Self::new_with_type(MessageType::Chat, to) + } + + /// Creates a Message of type Error + pub fn error>>(to: J) -> Message { + Self::new_with_type(MessageType::Error, to) + } + + /// Creates a Message of type Groupchat + pub fn groupchat>>(to: J) -> Message { + Self::new_with_type(MessageType::Groupchat, to) + } + + /// Creates a Message of type Headline + pub fn headline>>(to: J) -> Message { + Self::new_with_type(MessageType::Headline, to) + } + + /// Creates a Message of type Normal + pub fn normal>>(to: J) -> Message { + Self::new_with_type(MessageType::Normal, to) + } + + /// Appends a body in given lang to the Message + pub fn with_body(mut self, lang: Lang, body: String) -> Message { + self.bodies.insert(lang, Body(body)); + self + } + fn get_best<'a, T>( map: &'a BTreeMap, preferred_langs: Vec<&str>,