From 820fbbf08ebda5208623ac55904d18ce16df8c3a Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 27 Mar 2020 16:45:59 +0100 Subject: [PATCH] xmpp-parsers: Add helper constructors for MIX. --- xmpp-parsers/src/mix.rs | 109 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/xmpp-parsers/src/mix.rs b/xmpp-parsers/src/mix.rs index bf2cd35..419af35 100644 --- a/xmpp-parsers/src/mix.rs +++ b/xmpp-parsers/src/mix.rs @@ -17,6 +17,13 @@ generate_id!( ParticipantId ); +impl ParticipantId { + /// Create a new ParticipantId. + pub fn new>(participant: P) -> ParticipantId { + ParticipantId(participant.into()) + } +} + generate_id!( /// A MIX channel identifier. ChannelId @@ -38,6 +45,16 @@ generate_element!( impl PubSubPayload for Participant {} +impl Participant { + /// Create a new MIX participant. + pub fn new, N: Into>(jid: J, nick: N) -> Participant { + Participant { + nick: nick.into(), + jid: jid.into(), + } + } +} + generate_element!( /// A node to subscribe to. Subscribe, "subscribe", MIX_CORE, @@ -47,6 +64,15 @@ generate_element!( ] ); +impl Subscribe { + /// Create a new Subscribe element. + pub fn new>(node: N) -> Subscribe { + Subscribe { + node: NodeName(node.into()), + } + } +} + generate_element!( /// A request from a user’s server to join a MIX channel. Join, "join", MIX_CORE, @@ -66,6 +92,28 @@ generate_element!( impl IqSetPayload for Join {} impl IqResultPayload for Join {} +impl Join { + /// Create a new Join element. + pub fn from_nick_and_nodes>(nick: N, nodes: &[&str]) -> Join { + let subscribes = nodes + .into_iter() + .cloned() + .map(|n| Subscribe::new(n)) + .collect(); + Join { + id: None, + nick: nick.into(), + subscribes, + } + } + + /// Sets the JID on this update-subscription. + pub fn with_id>(mut self, id: I) -> Self { + self.id = Some(ParticipantId(id.into())); + self + } +} + generate_element!( /// Update a given subscription. UpdateSubscription, "update-subscription", MIX_CORE, @@ -85,6 +133,27 @@ generate_element!( impl IqSetPayload for UpdateSubscription {} impl IqResultPayload for UpdateSubscription {} +impl UpdateSubscription { + /// Create a new UpdateSubscription element. + pub fn from_nodes(nodes: &[&str]) -> UpdateSubscription { + let subscribes = nodes + .into_iter() + .cloned() + .map(|n| Subscribe::new(n)) + .collect(); + UpdateSubscription { + jid: None, + subscribes, + } + } + + /// Sets the JID on this update-subscription. + pub fn with_jid(mut self, jid: BareJid) -> Self { + self.jid = Some(jid); + self + } +} + generate_empty_element!( /// Request to leave a given MIX channel. It will automatically unsubscribe the user from all /// nodes on this channel. @@ -108,6 +177,13 @@ generate_element!( impl IqSetPayload for SetNick {} impl IqResultPayload for SetNick {} +impl SetNick { + /// Create a new SetNick element. + pub fn new>(nick: N) -> SetNick { + SetNick { nick: nick.into() } + } +} + generate_element!( /// Message payload describing who actually sent the message, since unlike in MUC, all messages /// are sent from the channel’s JID. @@ -124,6 +200,16 @@ generate_element!( impl MessagePayload for Mix {} +impl Mix { + /// Create a new Mix element. + pub fn new, J: Into>(nick: N, jid: J) -> Mix { + Mix { + nick: nick.into(), + jid: jid.into(), + } + } +} + generate_element!( /// Create a new MIX channel. Create, "create", MIX_CORE, @@ -136,6 +222,20 @@ generate_element!( impl IqSetPayload for Create {} impl IqResultPayload for Create {} +impl Create { + /// Create a new ad-hoc Create element. + pub fn new() -> Create { + Create { channel: None } + } + + /// Create a new Create element with a channel identifier. + pub fn from_channel_id>(channel: C) -> Create { + Create { + channel: Some(ChannelId(channel.into())), + } + } +} + generate_element!( /// Destroy a given MIX channel. Destroy, "destroy", MIX_CORE, @@ -149,6 +249,15 @@ generate_element!( // other section so far. impl IqSetPayload for Destroy {} +impl Destroy { + /// Create a new Destroy element. + pub fn new>(channel: C) -> Destroy { + Destroy { + channel: ChannelId(channel.into()), + } + } +} + #[cfg(test)] mod tests { use super::*;