xmpp_parsers/pubsub/
pubsub.rs

1// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{
8    error::{Error, FromElementError},
9    AsXml, FromXml,
10};
11
12use crate::data_forms::DataForm;
13use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
14use crate::ns;
15use crate::pubsub::{
16    AffiliationAttribute, ItemId, NodeName, PubSubPayload, Subscription, SubscriptionId,
17};
18use jid::Jid;
19use minidom::Element;
20
21// TODO: a better solution would be to split this into a query and a result elements, like for
22// XEP-0030.
23/// A list of affiliations you have on a service, or on a node.
24#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
25#[xml(namespace = ns::PUBSUB, name = "affiliations")]
26pub struct Affiliations {
27    /// The optional node name this request pertains to.
28    #[xml(attribute(default))]
29    pub node: Option<NodeName>,
30
31    /// The actual list of affiliation elements.
32    #[xml(child(n = ..))]
33    pub affiliations: Vec<Affiliation>,
34}
35
36/// An affiliation element.
37#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
38#[xml(namespace = ns::PUBSUB, name = "affiliation")]
39pub struct Affiliation {
40    /// The node this affiliation pertains to.
41    #[xml(attribute)]
42    pub node: NodeName,
43
44    /// The affiliation you currently have on this node.
45    #[xml(attribute)]
46    pub affiliation: AffiliationAttribute,
47}
48
49/// Request to configure a new node.
50#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
51#[xml(namespace = ns::PUBSUB, name = "configure")]
52pub struct Configure {
53    /// The form to configure it.
54    #[xml(child(default))]
55    pub form: Option<DataForm>,
56}
57
58/// Request to create a new node.
59#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
60#[xml(namespace = ns::PUBSUB, name = "create")]
61pub struct Create {
62    /// The node name to create, if `None` the service will generate one.
63    #[xml(attribute(default))]
64    pub node: Option<NodeName>,
65}
66
67/// Request for a default node configuration.
68#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
69#[xml(namespace = ns::PUBSUB, name = "default")]
70pub struct Default {
71    /// The node targeted by this request, otherwise the entire service.
72    #[xml(attribute(default))]
73    pub node: Option<NodeName>,
74    // TODO: do we really want to support collection nodes?
75    // #[xml(attribute(default, name = "type"))]
76    // type_: Option<String>,
77}
78
79/// A request for a list of items.
80#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
81#[xml(namespace = ns::PUBSUB, name = "items")]
82pub struct Items {
83    // TODO: should be an xs:positiveInteger, that is, an unbounded int ≥ 1.
84    /// Maximum number of items returned.
85    #[xml(attribute(name = "max_items" /*sic!*/, default))]
86    pub max_items: Option<u32>,
87
88    /// The node queried by this request.
89    #[xml(attribute)]
90    pub node: NodeName,
91
92    /// The subscription identifier related to this request.
93    #[xml(attribute(default))]
94    pub subid: Option<SubscriptionId>,
95
96    /// The actual list of items returned.
97    #[xml(child(n = ..))]
98    pub items: Vec<Item>,
99}
100
101impl Items {
102    /// Create a new items request.
103    pub fn new(node: &str) -> Items {
104        Items {
105            node: NodeName(String::from(node)),
106            max_items: None,
107            subid: None,
108            items: Vec::new(),
109        }
110    }
111}
112
113/// A requested item from a PubSub node.
114#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
115#[xml(namespace = ns::PUBSUB, name = "item")]
116pub struct Item {
117    /// The identifier for this item, unique per node.
118    #[xml(attribute(default))]
119    pub id: Option<ItemId>,
120
121    /// The JID of the entity who published this item.
122    #[xml(attribute(default))]
123    pub publisher: Option<Jid>,
124
125    /// The payload of this item, in an arbitrary namespace.
126    #[xml(element(default))]
127    pub payload: Option<Element>,
128}
129
130impl Item {
131    /// Create a new item, accepting only payloads implementing `PubSubPayload`.
132    pub fn new<P: PubSubPayload>(
133        id: Option<ItemId>,
134        publisher: Option<Jid>,
135        payload: Option<P>,
136    ) -> Item {
137        Item {
138            id,
139            publisher,
140            payload: payload.map(Into::into),
141        }
142    }
143}
144
145/// The options associated to a subscription request.
146#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
147#[xml(namespace = ns::PUBSUB, name = "options")]
148pub struct Options {
149    /// The JID affected by this request.
150    #[xml(attribute)]
151    pub jid: Jid,
152
153    /// The node affected by this request.
154    #[xml(attribute(default))]
155    pub node: Option<NodeName>,
156
157    /// The subscription identifier affected by this request.
158    #[xml(attribute(default))]
159    pub subid: Option<SubscriptionId>,
160
161    /// The form describing the subscription.
162    #[xml(child(default))]
163    pub form: Option<DataForm>,
164}
165
166/// Request to publish items to a node.
167#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
168#[xml(namespace = ns::PUBSUB, name = "publish")]
169pub struct Publish {
170    /// The target node for this operation.
171    #[xml(attribute)]
172    pub node: NodeName,
173
174    /// The items you want to publish.
175    #[xml(child(n = ..))]
176    pub items: Vec<Item>,
177}
178
179/// The options associated to a publish request.
180#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
181#[xml(namespace = ns::PUBSUB, name = "publish-options")]
182pub struct PublishOptions {
183    /// The form describing these options.
184    #[xml(child(default))]
185    pub form: Option<DataForm>,
186}
187
188/// A request to retract some items from a node.
189#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
190#[xml(namespace = ns::PUBSUB, name = "retract")]
191pub struct Retract {
192    /// The node affected by this request.
193    #[xml(attribute)]
194    pub node: NodeName,
195
196    /// Whether a retract request should notify subscribers or not.
197    #[xml(attribute(default))]
198    pub notify: bool,
199
200    /// The items affected by this request.
201    #[xml(child(n = ..))]
202    pub items: Vec<Item>,
203}
204
205/// Indicate that the subscription can be configured.
206#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
207#[xml(namespace = ns::PUBSUB, name = "subscribe-options")]
208pub struct SubscribeOptions {
209    /// If `true`, the configuration is actually required.
210    #[xml(flag)]
211    required: bool,
212}
213
214/// A request to subscribe a JID to a node.
215#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
216#[xml(namespace = ns::PUBSUB, name = "subscribe")]
217pub struct Subscribe {
218    /// The JID being subscribed.
219    #[xml(attribute)]
220    pub jid: Jid,
221
222    /// The node to subscribe to.
223    #[xml(attribute)]
224    pub node: Option<NodeName>,
225}
226
227/// A request for current subscriptions.
228#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
229#[xml(namespace = ns::PUBSUB, name = "subscriptions")]
230pub struct Subscriptions {
231    /// The node to query.
232    #[xml(attribute(default))]
233    pub node: Option<NodeName>,
234
235    /// The list of subscription elements returned.
236    #[xml(child(n = ..))]
237    pub subscription: Vec<SubscriptionElem>,
238}
239
240/// A subscription element, describing the state of a subscription.
241#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
242#[xml(namespace = ns::PUBSUB, name = "subscription")]
243pub struct SubscriptionElem {
244    /// The JID affected by this subscription.
245    #[xml(attribute)]
246    jid: Jid,
247
248    /// The node affected by this subscription.
249    #[xml(attribute(default))]
250    node: Option<NodeName>,
251
252    /// The subscription identifier for this subscription.
253    #[xml(attribute(default))]
254    subid: Option<SubscriptionId>,
255
256    /// The state of the subscription.
257    #[xml(attribute(default))]
258    subscription: Option<Subscription>,
259
260    /// The options related to this subscription.
261    #[xml(child(default))]
262    subscribe_options: Option<SubscribeOptions>,
263}
264
265/// An unsubscribe request.
266#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
267#[xml(namespace = ns::PUBSUB, name = "unsubscribe")]
268pub struct Unsubscribe {
269    /// The JID affected by this request.
270    #[xml(attribute)]
271    jid: Jid,
272
273    /// The node affected by this request.
274    #[xml(attribute)]
275    node: Option<NodeName>,
276
277    /// The subscription identifier for this subscription.
278    #[xml(attribute)]
279    subid: Option<SubscriptionId>,
280}
281
282/// Main payload used to communicate with a PubSub service.
283///
284/// `<pubsub xmlns="http://jabber.org/protocol/pubsub"/>`
285#[derive(Debug, Clone, PartialEq)]
286pub enum PubSub {
287    /// Request to create a new node, with optional suggested name and suggested configuration.
288    Create {
289        /// The create request.
290        create: Create,
291
292        /// The configure request for the new node.
293        configure: Option<Configure>,
294    },
295
296    /// A subscribe request.
297    Subscribe {
298        /// The subscribe request.
299        subscribe: Option<Subscribe>,
300
301        /// The options related to this subscribe request.
302        options: Option<Options>,
303    },
304
305    /// Request to publish items to a node, with optional options.
306    Publish {
307        /// The publish request.
308        publish: Publish,
309
310        /// The options related to this publish request.
311        publish_options: Option<PublishOptions>,
312    },
313
314    /// A list of affiliations you have on a service, or on a node.
315    Affiliations(Affiliations),
316
317    /// Request for a default node configuration.
318    Default(Default),
319
320    /// A request for a list of items.
321    Items(Items),
322
323    /// A request to retract some items from a node.
324    Retract(Retract),
325
326    /// A request about a subscription.
327    Subscription(SubscriptionElem),
328
329    /// A request for current subscriptions.
330    Subscriptions(Subscriptions),
331
332    /// An unsubscribe request.
333    Unsubscribe(Unsubscribe),
334}
335
336impl IqGetPayload for PubSub {}
337impl IqSetPayload for PubSub {}
338impl IqResultPayload for PubSub {}
339
340impl TryFrom<Element> for PubSub {
341    type Error = FromElementError;
342
343    fn try_from(elem: Element) -> Result<PubSub, FromElementError> {
344        check_self!(elem, "pubsub", PUBSUB);
345        check_no_attributes!(elem, "pubsub");
346
347        let mut payload = None;
348        for child in elem.children() {
349            if child.is("create", ns::PUBSUB) {
350                if payload.is_some() {
351                    return Err(
352                        Error::Other("Payload is already defined in pubsub element.").into(),
353                    );
354                }
355                let create = Create::try_from(child.clone())?;
356                payload = Some(PubSub::Create {
357                    create,
358                    configure: None,
359                });
360            } else if child.is("subscribe", ns::PUBSUB) {
361                if payload.is_some() {
362                    return Err(
363                        Error::Other("Payload is already defined in pubsub element.").into(),
364                    );
365                }
366                let subscribe = Subscribe::try_from(child.clone())?;
367                payload = Some(PubSub::Subscribe {
368                    subscribe: Some(subscribe),
369                    options: None,
370                });
371            } else if child.is("options", ns::PUBSUB) {
372                if let Some(PubSub::Subscribe { subscribe, options }) = payload {
373                    if options.is_some() {
374                        return Err(
375                            Error::Other("Options is already defined in pubsub element.").into(),
376                        );
377                    }
378                    let options = Some(Options::try_from(child.clone())?);
379                    payload = Some(PubSub::Subscribe { subscribe, options });
380                } else if payload.is_none() {
381                    let options = Options::try_from(child.clone())?;
382                    payload = Some(PubSub::Subscribe {
383                        subscribe: None,
384                        options: Some(options),
385                    });
386                } else {
387                    return Err(
388                        Error::Other("Payload is already defined in pubsub element.").into(),
389                    );
390                }
391            } else if child.is("configure", ns::PUBSUB) {
392                if let Some(PubSub::Create { create, configure }) = payload {
393                    if configure.is_some() {
394                        return Err(Error::Other(
395                            "Configure is already defined in pubsub element.",
396                        )
397                        .into());
398                    }
399                    let configure = Some(Configure::try_from(child.clone())?);
400                    payload = Some(PubSub::Create { create, configure });
401                } else {
402                    return Err(
403                        Error::Other("Payload is already defined in pubsub element.").into(),
404                    );
405                }
406            } else if child.is("publish", ns::PUBSUB) {
407                if payload.is_some() {
408                    return Err(
409                        Error::Other("Payload is already defined in pubsub element.").into(),
410                    );
411                }
412                let publish = Publish::try_from(child.clone())?;
413                payload = Some(PubSub::Publish {
414                    publish,
415                    publish_options: None,
416                });
417            } else if child.is("publish-options", ns::PUBSUB) {
418                if let Some(PubSub::Publish {
419                    publish,
420                    publish_options,
421                }) = payload
422                {
423                    if publish_options.is_some() {
424                        return Err(Error::Other(
425                            "Publish-options are already defined in pubsub element.",
426                        )
427                        .into());
428                    }
429                    let publish_options = Some(PublishOptions::try_from(child.clone())?);
430                    payload = Some(PubSub::Publish {
431                        publish,
432                        publish_options,
433                    });
434                } else {
435                    return Err(
436                        Error::Other("Payload is already defined in pubsub element.").into(),
437                    );
438                }
439            } else if child.is("affiliations", ns::PUBSUB) {
440                if payload.is_some() {
441                    return Err(
442                        Error::Other("Payload is already defined in pubsub element.").into(),
443                    );
444                }
445                let affiliations = Affiliations::try_from(child.clone())?;
446                payload = Some(PubSub::Affiliations(affiliations));
447            } else if child.is("default", ns::PUBSUB) {
448                if payload.is_some() {
449                    return Err(
450                        Error::Other("Payload is already defined in pubsub element.").into(),
451                    );
452                }
453                let default = Default::try_from(child.clone())?;
454                payload = Some(PubSub::Default(default));
455            } else if child.is("items", ns::PUBSUB) {
456                if payload.is_some() {
457                    return Err(
458                        Error::Other("Payload is already defined in pubsub element.").into(),
459                    );
460                }
461                let items = Items::try_from(child.clone())?;
462                payload = Some(PubSub::Items(items));
463            } else if child.is("retract", ns::PUBSUB) {
464                if payload.is_some() {
465                    return Err(
466                        Error::Other("Payload is already defined in pubsub element.").into(),
467                    );
468                }
469                let retract = Retract::try_from(child.clone())?;
470                payload = Some(PubSub::Retract(retract));
471            } else if child.is("subscription", ns::PUBSUB) {
472                if payload.is_some() {
473                    return Err(
474                        Error::Other("Payload is already defined in pubsub element.").into(),
475                    );
476                }
477                let subscription = SubscriptionElem::try_from(child.clone())?;
478                payload = Some(PubSub::Subscription(subscription));
479            } else if child.is("subscriptions", ns::PUBSUB) {
480                if payload.is_some() {
481                    return Err(
482                        Error::Other("Payload is already defined in pubsub element.").into(),
483                    );
484                }
485                let subscriptions = Subscriptions::try_from(child.clone())?;
486                payload = Some(PubSub::Subscriptions(subscriptions));
487            } else if child.is("unsubscribe", ns::PUBSUB) {
488                if payload.is_some() {
489                    return Err(
490                        Error::Other("Payload is already defined in pubsub element.").into(),
491                    );
492                }
493                let unsubscribe = Unsubscribe::try_from(child.clone())?;
494                payload = Some(PubSub::Unsubscribe(unsubscribe));
495            } else {
496                return Err(Error::Other("Unknown child in pubsub element.").into());
497            }
498        }
499        payload.ok_or(Error::Other("No payload in pubsub element.").into())
500    }
501}
502
503impl From<PubSub> for Element {
504    fn from(pubsub: PubSub) -> Element {
505        Element::builder("pubsub", ns::PUBSUB)
506            .append_all(match pubsub {
507                PubSub::Create { create, configure } => {
508                    let mut elems = vec![Element::from(create)];
509                    if let Some(configure) = configure {
510                        elems.push(Element::from(configure));
511                    }
512                    elems
513                }
514                PubSub::Subscribe { subscribe, options } => {
515                    let mut elems = vec![];
516                    if let Some(subscribe) = subscribe {
517                        elems.push(Element::from(subscribe));
518                    }
519                    if let Some(options) = options {
520                        elems.push(Element::from(options));
521                    }
522                    elems
523                }
524                PubSub::Publish {
525                    publish,
526                    publish_options,
527                } => {
528                    let mut elems = vec![Element::from(publish)];
529                    if let Some(publish_options) = publish_options {
530                        elems.push(Element::from(publish_options));
531                    }
532                    elems
533                }
534                PubSub::Affiliations(affiliations) => vec![Element::from(affiliations)],
535                PubSub::Default(default) => vec![Element::from(default)],
536                PubSub::Items(items) => vec![Element::from(items)],
537                PubSub::Retract(retract) => vec![Element::from(retract)],
538                PubSub::Subscription(subscription) => vec![Element::from(subscription)],
539                PubSub::Subscriptions(subscriptions) => vec![Element::from(subscriptions)],
540                PubSub::Unsubscribe(unsubscribe) => vec![Element::from(unsubscribe)],
541            })
542            .build()
543    }
544}
545
546#[cfg(test)]
547mod tests {
548    use super::*;
549    use crate::data_forms::{DataFormType, Field, FieldType};
550
551    #[test]
552    fn create() {
553        let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create/></pubsub>"
554            .parse()
555            .unwrap();
556        let elem1 = elem.clone();
557        let pubsub = PubSub::try_from(elem).unwrap();
558        match pubsub.clone() {
559            PubSub::Create { create, configure } => {
560                assert!(create.node.is_none());
561                assert!(configure.is_none());
562            }
563            _ => panic!(),
564        }
565
566        let elem2 = Element::from(pubsub);
567        assert_eq!(elem1, elem2);
568
569        let elem: Element =
570            "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create node='coucou'/></pubsub>"
571                .parse()
572                .unwrap();
573        let elem1 = elem.clone();
574        let pubsub = PubSub::try_from(elem).unwrap();
575        match pubsub.clone() {
576            PubSub::Create { create, configure } => {
577                assert_eq!(&create.node.unwrap().0, "coucou");
578                assert!(configure.is_none());
579            }
580            _ => panic!(),
581        }
582
583        let elem2 = Element::from(pubsub);
584        assert_eq!(elem1, elem2);
585    }
586
587    #[test]
588    fn create_and_configure_empty() {
589        let elem: Element =
590            "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create/><configure/></pubsub>"
591                .parse()
592                .unwrap();
593        let elem1 = elem.clone();
594        let pubsub = PubSub::try_from(elem).unwrap();
595        match pubsub.clone() {
596            PubSub::Create { create, configure } => {
597                assert!(create.node.is_none());
598                assert!(configure.unwrap().form.is_none());
599            }
600            _ => panic!(),
601        }
602
603        let elem2 = Element::from(pubsub);
604        assert_eq!(elem1, elem2);
605    }
606
607    #[test]
608    fn create_and_configure_simple() {
609        // XXX: Do we want xmpp-parsers to always specify the field type in the output Element?
610        let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create node='foo'/><configure><x xmlns='jabber:x:data' type='submit'><field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/pubsub#node_config</value></field><field var='pubsub#access_model' type='list-single'><value>whitelist</value></field></x></configure></pubsub>"
611        .parse()
612        .unwrap();
613        let elem1 = elem.clone();
614
615        let pubsub = PubSub::Create {
616            create: Create {
617                node: Some(NodeName(String::from("foo"))),
618            },
619            configure: Some(Configure {
620                form: Some(DataForm::new(
621                    DataFormType::Submit,
622                    ns::PUBSUB_CONFIGURE,
623                    vec![Field::new("pubsub#access_model", FieldType::ListSingle)
624                        .with_value("whitelist")],
625                )),
626            }),
627        };
628
629        let elem2 = Element::from(pubsub);
630        assert_eq!(elem1, elem2);
631    }
632
633    #[test]
634    fn publish() {
635        let elem: Element =
636            "<pubsub xmlns='http://jabber.org/protocol/pubsub'><publish node='coucou'/></pubsub>"
637                .parse()
638                .unwrap();
639        let elem1 = elem.clone();
640        let pubsub = PubSub::try_from(elem).unwrap();
641        match pubsub.clone() {
642            PubSub::Publish {
643                publish,
644                publish_options,
645            } => {
646                assert_eq!(&publish.node.0, "coucou");
647                assert!(publish_options.is_none());
648            }
649            _ => panic!(),
650        }
651
652        let elem2 = Element::from(pubsub);
653        assert_eq!(elem1, elem2);
654    }
655
656    #[test]
657    fn publish_with_publish_options() {
658        let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><publish node='coucou'/><publish-options/></pubsub>".parse().unwrap();
659        let elem1 = elem.clone();
660        let pubsub = PubSub::try_from(elem).unwrap();
661        match pubsub.clone() {
662            PubSub::Publish {
663                publish,
664                publish_options,
665            } => {
666                assert_eq!(&publish.node.0, "coucou");
667                assert!(publish_options.unwrap().form.is_none());
668            }
669            _ => panic!(),
670        }
671
672        let elem2 = Element::from(pubsub);
673        assert_eq!(elem1, elem2);
674    }
675
676    #[test]
677    fn invalid_empty_pubsub() {
678        let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'/>"
679            .parse()
680            .unwrap();
681        let error = PubSub::try_from(elem).unwrap_err();
682        let message = match error {
683            FromElementError::Invalid(Error::Other(string)) => string,
684            _ => panic!(),
685        };
686        assert_eq!(message, "No payload in pubsub element.");
687    }
688
689    #[test]
690    fn publish_option() {
691        let elem: Element = "<publish-options xmlns='http://jabber.org/protocol/pubsub'><x xmlns='jabber:x:data' type='submit'><field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/pubsub#publish-options</value></field></x></publish-options>".parse().unwrap();
692        let publish_options = PublishOptions::try_from(elem).unwrap();
693        assert_eq!(
694            &publish_options.form.unwrap().form_type.unwrap(),
695            "http://jabber.org/protocol/pubsub#publish-options"
696        );
697    }
698
699    #[test]
700    fn subscribe_options() {
701        let elem1: Element = "<subscribe-options xmlns='http://jabber.org/protocol/pubsub'/>"
702            .parse()
703            .unwrap();
704        let subscribe_options1 = SubscribeOptions::try_from(elem1).unwrap();
705        assert_eq!(subscribe_options1.required, false);
706
707        let elem2: Element = "<subscribe-options xmlns='http://jabber.org/protocol/pubsub'><required/></subscribe-options>".parse().unwrap();
708        let subscribe_options2 = SubscribeOptions::try_from(elem2).unwrap();
709        assert_eq!(subscribe_options2.required, true);
710    }
711
712    #[test]
713    fn test_options_without_subscribe() {
714        let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><options xmlns='http://jabber.org/protocol/pubsub' jid='juliet@capulet.lit/balcony'><x xmlns='jabber:x:data' type='submit'/></options></pubsub>".parse().unwrap();
715        let elem1 = elem.clone();
716        let pubsub = PubSub::try_from(elem).unwrap();
717        match pubsub.clone() {
718            PubSub::Subscribe { subscribe, options } => {
719                assert!(subscribe.is_none());
720                assert!(options.is_some());
721            }
722            _ => panic!(),
723        }
724
725        let elem2 = Element::from(pubsub);
726        assert_eq!(elem1, elem2);
727    }
728
729    #[test]
730    fn test_serialize_options() {
731        let reference: Element = "<options xmlns='http://jabber.org/protocol/pubsub' jid='juliet@capulet.lit/balcony'><x xmlns='jabber:x:data' type='submit'/></options>"
732        .parse()
733        .unwrap();
734
735        let elem: Element = "<x xmlns='jabber:x:data' type='submit'/>".parse().unwrap();
736
737        let form = DataForm::try_from(elem).unwrap();
738
739        let options = Options {
740            jid: Jid::new("juliet@capulet.lit/balcony").unwrap(),
741            node: None,
742            subid: None,
743            form: Some(form),
744        };
745        let serialized: Element = options.into();
746        assert_eq!(serialized, reference);
747    }
748
749    #[test]
750    fn test_serialize_publish_options() {
751        let reference: Element = "<publish-options xmlns='http://jabber.org/protocol/pubsub'><x xmlns='jabber:x:data' type='submit'/></publish-options>"
752        .parse()
753        .unwrap();
754
755        let elem: Element = "<x xmlns='jabber:x:data' type='submit'/>".parse().unwrap();
756
757        let form = DataForm::try_from(elem).unwrap();
758
759        let options = PublishOptions { form: Some(form) };
760        let serialized: Element = options.into();
761        assert_eq!(serialized, reference);
762    }
763}