Message now has constructors for each type, and a with_body builder method (#78)

This commit is contained in:
xmppftw 2023-06-03 11:23:32 +02:00
parent 512c1d1aae
commit 6fa6deddcb
2 changed files with 49 additions and 1 deletions

View file

@ -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"] }`.

View file

@ -94,7 +94,8 @@ pub struct Message {
}
impl Message {
/// Creates a new `<message/>` stanza for the given recipient.
/// Creates a new `<message/>` stanza of type Chat for the given recipient.
/// This is equivalent to the [`Message::chat`] method.
pub fn new<J: Into<Option<Jid>>>(to: J) -> Message {
Message {
from: None,
@ -108,6 +109,51 @@ impl Message {
}
}
/// Creates a new `<message/>` stanza of a certain type for the given recipient.
pub fn new_with_type<J: Into<Option<Jid>>>(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<J: Into<Option<Jid>>>(to: J) -> Message {
Self::new_with_type(MessageType::Chat, to)
}
/// Creates a Message of type Error
pub fn error<J: Into<Option<Jid>>>(to: J) -> Message {
Self::new_with_type(MessageType::Error, to)
}
/// Creates a Message of type Groupchat
pub fn groupchat<J: Into<Option<Jid>>>(to: J) -> Message {
Self::new_with_type(MessageType::Groupchat, to)
}
/// Creates a Message of type Headline
pub fn headline<J: Into<Option<Jid>>>(to: J) -> Message {
Self::new_with_type(MessageType::Headline, to)
}
/// Creates a Message of type Normal
pub fn normal<J: Into<Option<Jid>>>(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<Lang, T>,
preferred_langs: Vec<&str>,