2018-05-14 19:04:16 +00:00
|
|
|
|
// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2018-05-14 22:18:15 +00:00
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
|
2018-05-14 19:04:16 +00:00
|
|
|
|
use try_from::TryFrom;
|
|
|
|
|
|
|
|
|
|
use minidom::Element;
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
|
|
|
|
|
use data_forms::DataForm;
|
|
|
|
|
|
|
|
|
|
use pubsub::{NodeName, ItemId, Subscription, SubscriptionId};
|
|
|
|
|
|
|
|
|
|
// TODO: a better solution would be to split this into a query and a result elements, like for
|
|
|
|
|
// XEP-0030.
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A list of affiliations you have on a service, or on a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Affiliations, "affiliations", PUBSUB,
|
|
|
|
|
attributes: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The optional node name this request pertains to.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The actual list of affiliation elements.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
affiliations: Vec<Affiliation> = ("affiliation", PUBSUB) => Affiliation
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_attribute!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A list of possible affiliations to a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
AffiliationAttribute, "affiliation", {
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// You are a member of this node, you can subscribe and retrieve items.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Member => "member",
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// You don’t have a specific affiliation with this node, you can only subscribe to it.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
None => "none",
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// You are banned from this node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Outcast => "outcast",
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// You are an owner of this node, and can do anything with it.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Owner => "owner",
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// You are a publisher on this node, you can publish and retract items to it.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Publisher => "publisher",
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// You can publish and retract items on this node, but not subscribe or retrive items.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
PublishOnly => "publish-only",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_only_attributes!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// An affiliation element.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Affiliation, "affiliation", PUBSUB, [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The node this affiliation pertains to.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: NodeName = "node" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The affiliation you currently have on this node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
affiliation: AffiliationAttribute = "affiliation" => required,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Request to configure a new node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Configure, "configure", PUBSUB,
|
|
|
|
|
child: (
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The form to configure it.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_only_attributes!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Request to create a new node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Create, "create", PUBSUB, [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The node name to create, if `None` the service will generate one.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_only_attributes!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Request for a default node configuration.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Default, "default", PUBSUB, [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The node targetted by this request, otherwise the entire service.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
2018-05-14 19:04:16 +00:00
|
|
|
|
// TODO: do we really want to support collection nodes?
|
|
|
|
|
// type: String = "type" => optional,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A request for a list of items.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Items, "items", PUBSUB,
|
|
|
|
|
attributes: [
|
|
|
|
|
// TODO: should be an xs:positiveInteger, that is, an unbounded int ≥ 1.
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Maximum number of items returned.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
max_items: Option<u32> = "max_items" => optional,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The node queried by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: NodeName = "node" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The subscription identifier related to this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subid: Option<SubscriptionId> = "subid" => optional,
|
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The actual list of items returned.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
items: Vec<Item> = ("item", PUBSUB) => Item
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// An item from a PubSub node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Item {
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The payload of this item, in an arbitrary namespace.
|
|
|
|
|
pub payload: Option<Element>,
|
|
|
|
|
|
|
|
|
|
/// The 'id' attribute of this item.
|
|
|
|
|
pub id: Option<ItemId>,
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Item {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Item, Error> {
|
|
|
|
|
check_self!(elem, "item", PUBSUB);
|
|
|
|
|
check_no_unknown_attributes!(elem, "item", ["id"]);
|
|
|
|
|
let mut payloads = elem.children().cloned().collect::<Vec<_>>();
|
|
|
|
|
let payload = payloads.pop();
|
|
|
|
|
if !payloads.is_empty() {
|
|
|
|
|
return Err(Error::ParseError("More than a single payload in item element."));
|
|
|
|
|
}
|
|
|
|
|
Ok(Item {
|
|
|
|
|
payload,
|
|
|
|
|
id: get_attr!(elem, "id", optional),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Item> for Element {
|
|
|
|
|
fn from(item: Item) -> Element {
|
|
|
|
|
Element::builder("item")
|
|
|
|
|
.ns(ns::PUBSUB)
|
|
|
|
|
.attr("id", item.id)
|
|
|
|
|
.append(item.payload)
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The options associated to a subscription request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Options, "options", PUBSUB,
|
|
|
|
|
attributes: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The JID affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
jid: Jid = "jid" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The node affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The subscription identifier affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subid: Option<SubscriptionId> = "subid" => optional,
|
|
|
|
|
],
|
|
|
|
|
child: (
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The form describing the subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Request to publish items to a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Publish, "publish", PUBSUB,
|
|
|
|
|
attributes: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The target node for this operation.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: NodeName = "node" => required,
|
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The items you want to publish.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
items: Vec<Item> = ("item", PUBSUB) => Item
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The options associated to a publish request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
PublishOptions, "publish-options", PUBSUB,
|
|
|
|
|
child: (
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The form describing these options.
|
2018-05-14 19:32:58 +00:00
|
|
|
|
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm
|
2018-05-14 19:04:16 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-14 22:18:15 +00:00
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// Whether a retract request should notify subscribers or not.
|
|
|
|
|
Notify, "notify", bool
|
|
|
|
|
);
|
2018-05-14 19:04:16 +00:00
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A request to retract some items from a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Retract, "retract", PUBSUB,
|
|
|
|
|
attributes: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The node affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: NodeName = "node" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// Whether a retract request should notify subscribers or not.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
notify: Notify = "notify" => default,
|
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The items affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
items: Vec<Item> = ("item", PUBSUB) => Item
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Indicate that the subscription can be configured.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct SubscribeOptions {
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// If `true`, the configuration is actually required.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
required: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for SubscribeOptions {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Self, Error> {
|
|
|
|
|
check_self!(elem, "subscribe-options", PUBSUB);
|
|
|
|
|
check_no_attributes!(elem, "subscribe-options");
|
|
|
|
|
let mut required = false;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("required", ns::PUBSUB) {
|
|
|
|
|
if required {
|
|
|
|
|
return Err(Error::ParseError("More than one required element in subscribe-options."));
|
|
|
|
|
}
|
|
|
|
|
required = true;
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in subscribe-options element."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(SubscribeOptions { required })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<SubscribeOptions> for Element {
|
|
|
|
|
fn from(subscribe_options: SubscribeOptions) -> Element {
|
|
|
|
|
Element::builder("subscribe-options")
|
|
|
|
|
.ns(ns::PUBSUB)
|
|
|
|
|
.append(if subscribe_options.required {
|
|
|
|
|
vec!(Element::builder("required")
|
|
|
|
|
.ns(ns::PUBSUB)
|
|
|
|
|
.build())
|
|
|
|
|
} else {
|
|
|
|
|
vec!()
|
|
|
|
|
})
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generate_element_with_only_attributes!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A request to subscribe a JID to a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Subscribe, "subscribe", PUBSUB, [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The JID being subscribed.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
jid: Jid = "jid" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The node to subscribe to.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A request for current subscriptions.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Subscriptions, "subscriptions", PUBSUB,
|
|
|
|
|
attributes: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The node to query.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The list of subscription elements returned.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subscription: Vec<SubscriptionElem> = ("subscription", PUBSUB) => SubscriptionElem
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_children!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// A subscription element, describing the state of a subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
SubscriptionElem, "subscription", PUBSUB,
|
|
|
|
|
attributes: [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The JID affected by this subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
jid: Jid = "jid" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The node affected by this subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The subscription identifier for this subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subid: Option<SubscriptionId> = "subid" => optional,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The state of the subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subscription: Option<Subscription> = "subscription" => optional,
|
|
|
|
|
],
|
|
|
|
|
child: (
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The options related to this subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subscribe_options: Option<SubscribeOptions> = ("subscribe-options", PUBSUB) => SubscribeOptions
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element_with_only_attributes!(
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// An unsubscribe request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Unsubscribe, "unsubscribe", PUBSUB, [
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The JID affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
jid: Jid = "jid" => required,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The node affected by this request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The subscription identifier for this subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
subid: Option<SubscriptionId> = "subid" => optional,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Main payload used to communicate with a PubSub service.
|
|
|
|
|
///
|
|
|
|
|
/// `<pubsub xmlns="http://jabber.org/protocol/pubsub"/>`
|
2018-05-14 19:04:16 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum PubSub {
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// Request to create a new node, with optional suggested name and suggested configuration.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Create {
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The create request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
create: Create,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The configure request for the new node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
configure: Option<Configure>
|
|
|
|
|
},
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// Request to publish items to a node, with optional options.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Publish {
|
2018-05-14 22:18:15 +00:00
|
|
|
|
/// The publish request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
publish: Publish,
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// The options related to this publish request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
publish_options: Option<PublishOptions>
|
|
|
|
|
},
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// A list of affiliations you have on a service, or on a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Affiliations(Affiliations),
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// Request for a default node configuration.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Default(Default),
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// A request for a list of items.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Items(Items),
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// A request to retract some items from a node.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Retract(Retract),
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// A request about a subscription.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Subscription(SubscriptionElem),
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// A request for current subscriptions.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Subscriptions(Subscriptions),
|
2018-05-14 22:18:15 +00:00
|
|
|
|
|
|
|
|
|
/// An unsubscribe request.
|
2018-05-14 19:04:16 +00:00
|
|
|
|
Unsubscribe(Unsubscribe),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for PubSub {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<PubSub, Error> {
|
|
|
|
|
check_self!(elem, "pubsub", PUBSUB);
|
|
|
|
|
check_no_attributes!(elem, "pubsub");
|
|
|
|
|
|
|
|
|
|
let mut payload = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("create", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
2018-05-14 19:32:58 +00:00
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
let create = Create::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Create { create, configure: None });
|
2018-05-14 19:32:58 +00:00
|
|
|
|
} else if child.is("configure", ns::PUBSUB) {
|
|
|
|
|
if let Some(PubSub::Create { create, configure }) = payload {
|
|
|
|
|
if configure.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Configure is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let configure = Some(Configure::try_from(child.clone())?);
|
|
|
|
|
payload = Some(PubSub::Create { create, configure });
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
} else if child.is("publish", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let publish = Publish::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Publish { publish, publish_options: None });
|
|
|
|
|
} else if child.is("publish-options", ns::PUBSUB) {
|
|
|
|
|
if let Some(PubSub::Publish { publish, publish_options }) = payload {
|
|
|
|
|
if publish_options.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Publish-options are already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let publish_options = Some(PublishOptions::try_from(child.clone())?);
|
|
|
|
|
payload = Some(PubSub::Publish { publish, publish_options });
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
} else if child.is("affiliations", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let affiliations = Affiliations::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Affiliations(affiliations));
|
|
|
|
|
} else if child.is("default", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let default = Default::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Default(default));
|
|
|
|
|
} else if child.is("items", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let items = Items::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Items(items));
|
|
|
|
|
} else if child.is("retract", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let retract = Retract::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Retract(retract));
|
|
|
|
|
} else if child.is("subscription", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let subscription = SubscriptionElem::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Subscription(subscription));
|
|
|
|
|
} else if child.is("subscriptions", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let subscriptions = Subscriptions::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Subscriptions(subscriptions));
|
|
|
|
|
} else if child.is("unsubscribe", ns::PUBSUB) {
|
|
|
|
|
if payload.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Payload is already defined in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
let unsubscribe = Unsubscribe::try_from(child.clone())?;
|
|
|
|
|
payload = Some(PubSub::Unsubscribe(unsubscribe));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in pubsub element."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(payload.ok_or(Error::ParseError("No payload in pubsub element."))?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<PubSub> for Element {
|
|
|
|
|
fn from(pubsub: PubSub) -> Element {
|
|
|
|
|
Element::builder("pubsub")
|
|
|
|
|
.ns(ns::PUBSUB)
|
|
|
|
|
.append(match pubsub {
|
|
|
|
|
PubSub::Create { create, configure } => {
|
|
|
|
|
let mut elems = vec!(Element::from(create));
|
|
|
|
|
if let Some(configure) = configure {
|
|
|
|
|
elems.push(Element::from(configure));
|
|
|
|
|
}
|
|
|
|
|
elems
|
|
|
|
|
},
|
|
|
|
|
PubSub::Publish { publish, publish_options } => {
|
|
|
|
|
let mut elems = vec!(Element::from(publish));
|
|
|
|
|
if let Some(publish_options) = publish_options {
|
|
|
|
|
elems.push(Element::from(publish_options));
|
|
|
|
|
}
|
|
|
|
|
elems
|
|
|
|
|
},
|
|
|
|
|
PubSub::Affiliations(affiliations) => vec!(Element::from(affiliations)),
|
|
|
|
|
PubSub::Default(default) => vec!(Element::from(default)),
|
|
|
|
|
PubSub::Items(items) => vec!(Element::from(items)),
|
|
|
|
|
PubSub::Retract(retract) => vec!(Element::from(retract)),
|
|
|
|
|
PubSub::Subscription(subscription) => vec!(Element::from(subscription)),
|
|
|
|
|
PubSub::Subscriptions(subscriptions) => vec!(Element::from(subscriptions)),
|
|
|
|
|
PubSub::Unsubscribe(unsubscribe) => vec!(Element::from(unsubscribe)),
|
|
|
|
|
})
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use compare_elements::NamespaceAwareCompare;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn create() {
|
|
|
|
|
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create/></pubsub>".parse().unwrap();
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let pubsub = PubSub::try_from(elem).unwrap();
|
|
|
|
|
match pubsub.clone() {
|
|
|
|
|
PubSub::Create { create, configure } => {
|
|
|
|
|
assert!(create.node.is_none());
|
|
|
|
|
assert!(configure.is_none());
|
|
|
|
|
}
|
2018-05-14 19:04:16 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 19:32:58 +00:00
|
|
|
|
let elem2 = Element::from(pubsub);
|
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
|
2018-05-14 19:32:58 +00:00
|
|
|
|
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create node='coucou'/></pubsub>".parse().unwrap();
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let pubsub = PubSub::try_from(elem).unwrap();
|
|
|
|
|
match pubsub.clone() {
|
|
|
|
|
PubSub::Create { create, configure } => {
|
|
|
|
|
assert_eq!(&create.node.unwrap().0, "coucou");
|
|
|
|
|
assert!(configure.is_none());
|
|
|
|
|
}
|
2018-05-14 19:04:16 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 19:32:58 +00:00
|
|
|
|
let elem2 = Element::from(pubsub);
|
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn create_and_configure() {
|
|
|
|
|
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><create/><configure/></pubsub>".parse().unwrap();
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let pubsub = PubSub::try_from(elem).unwrap();
|
|
|
|
|
match pubsub.clone() {
|
|
|
|
|
PubSub::Create { create, configure } => {
|
|
|
|
|
assert!(create.node.is_none());
|
|
|
|
|
assert!(configure.unwrap().form.is_none());
|
|
|
|
|
}
|
2018-05-14 19:04:16 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 19:32:58 +00:00
|
|
|
|
let elem2 = Element::from(pubsub);
|
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn publish() {
|
|
|
|
|
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><publish node='coucou'/></pubsub>".parse().unwrap();
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let pubsub = PubSub::try_from(elem).unwrap();
|
|
|
|
|
match pubsub.clone() {
|
|
|
|
|
PubSub::Publish { publish, publish_options } => {
|
|
|
|
|
assert_eq!(&publish.node.0, "coucou");
|
|
|
|
|
assert!(publish_options.is_none());
|
|
|
|
|
}
|
2018-05-14 19:04:16 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
2018-05-14 19:32:58 +00:00
|
|
|
|
|
|
|
|
|
let elem2 = Element::from(pubsub);
|
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn publish_with_publish_options() {
|
|
|
|
|
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'><publish node='coucou'/><publish-options/></pubsub>".parse().unwrap();
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let pubsub = PubSub::try_from(elem).unwrap();
|
|
|
|
|
match pubsub.clone() {
|
|
|
|
|
PubSub::Publish { publish, publish_options } => {
|
|
|
|
|
assert_eq!(&publish.node.0, "coucou");
|
|
|
|
|
assert!(publish_options.unwrap().form.is_none());
|
|
|
|
|
}
|
2018-05-14 19:04:16 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
2018-05-14 19:32:58 +00:00
|
|
|
|
|
|
|
|
|
let elem2 = Element::from(pubsub);
|
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn invalid_empty_pubsub() {
|
|
|
|
|
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'/>".parse().unwrap();
|
2018-05-14 19:04:16 +00:00
|
|
|
|
let error = PubSub::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2018-05-14 19:32:58 +00:00
|
|
|
|
assert_eq!(message, "No payload in pubsub element.");
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn publish_option() {
|
|
|
|
|
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();
|
|
|
|
|
let publish_options = PublishOptions::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(&publish_options.form.unwrap().form_type.unwrap(), "http://jabber.org/protocol/pubsub#publish-options");
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2018-05-14 19:32:58 +00:00
|
|
|
|
fn subscribe_options() {
|
|
|
|
|
let elem1: Element = "<subscribe-options xmlns='http://jabber.org/protocol/pubsub'/>".parse().unwrap();
|
|
|
|
|
let subscribe_options1 = SubscribeOptions::try_from(elem1).unwrap();
|
|
|
|
|
assert_eq!(subscribe_options1.required, false);
|
2018-05-14 19:04:16 +00:00
|
|
|
|
|
2018-05-14 19:32:58 +00:00
|
|
|
|
let elem2: Element = "<subscribe-options xmlns='http://jabber.org/protocol/pubsub'><required/></subscribe-options>".parse().unwrap();
|
|
|
|
|
let subscribe_options2 = SubscribeOptions::try_from(elem2).unwrap();
|
|
|
|
|
assert_eq!(subscribe_options2.required, true);
|
2018-05-14 19:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|