From e5c1be68d066f7bff37ad9a69de19e62224d0540 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 1 Nov 2018 17:25:24 +0100 Subject: [PATCH] muc: Add constructors. --- src/muc/muc.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/muc/muc.rs b/src/muc/muc.rs index c363107a..def14bae 100644 --- a/src/muc/muc.rs +++ b/src/muc/muc.rs @@ -10,6 +10,7 @@ use date::DateTime; generate_element!( /// Represents the query for messages before our join. + #[derive(PartialEq)] History, "history", MUC, attributes: [ /// How many characters of history to send, in XML characters. @@ -26,8 +27,40 @@ generate_element!( ] ); +impl History { + pub fn new() -> Self { + History { + maxchars: None, + maxstanzas: None, + seconds: None, + since: None, + } + } + + pub fn with_maxchars(mut self, maxchars: u32) -> Self { + self.maxchars = Some(maxchars); + self + } + + pub fn with_maxstanzas(mut self, maxstanzas: u32) -> Self { + self.maxstanzas = Some(maxstanzas); + self + } + + pub fn with_seconds(mut self, seconds: u32) -> Self { + self.seconds = Some(seconds); + self + } + + pub fn with_since(mut self, since: DateTime) -> Self { + self.since = Some(since); + self + } +} + generate_element!( /// Represents a room join request. + #[derive(PartialEq)] Muc, "x", MUC, children: [ /// Password to use when the room is protected by a password. password: Option = ("password", MUC) => String, @@ -39,6 +72,25 @@ generate_element!( impl PresencePayload for Muc {} +impl Muc { + pub fn new() -> Self { + Muc { + password: None, + history: None, + } + } + + pub fn with_password(mut self, password: String) -> Self { + self.password = Some(password); + self + } + + pub fn with_history(mut self, history: History) -> Self { + self.history = Some(history); + self + } +} + #[cfg(test)] mod tests { use super::*; @@ -110,6 +162,9 @@ mod tests { " .parse().unwrap(); let muc = Muc::try_from(elem).unwrap(); + let muc2 = Muc::new().with_history(History::new().with_maxstanzas(0)); + assert_eq!(muc, muc2); + let history = muc.history.unwrap(); assert_eq!(history.maxstanzas, Some(0)); assert_eq!(history.maxchars, None);