use crate::data_forms::DataForm;
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns;
use crate::pubsub::{
AffiliationAttribute, Item as PubSubItem, NodeName, Subscription, SubscriptionId,
};
use crate::util::error::Error;
use crate::Element;
use jid::Jid;
generate_element!(
Affiliations, "affiliations", PUBSUB,
attributes: [
node: Option<NodeName> = "node",
],
children: [
affiliations: Vec<Affiliation> = ("affiliation", PUBSUB) => Affiliation
]
);
generate_element!(
Affiliation, "affiliation", PUBSUB,
attributes: [
node: Required<NodeName> = "node",
affiliation: Required<AffiliationAttribute> = "affiliation",
]
);
generate_element!(
Configure, "configure", PUBSUB,
children: [
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm
]
);
generate_element!(
Create, "create", PUBSUB,
attributes: [
node: Option<NodeName> = "node",
]
);
generate_element!(
Default, "default", PUBSUB,
attributes: [
node: Option<NodeName> = "node",
]
);
generate_element!(
Items, "items", PUBSUB,
attributes: [
max_items: Option<u32> = "max_items",
node: Required<NodeName> = "node",
subid: Option<SubscriptionId> = "subid",
],
children: [
items: Vec<Item> = ("item", PUBSUB) => Item
]
);
impl Items {
pub fn new(node: &str) -> Items {
Items {
node: NodeName(String::from(node)),
max_items: None,
subid: None,
items: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Item(pub PubSubItem);
impl_pubsub_item!(Item, PUBSUB);
generate_element!(
Options, "options", PUBSUB,
attributes: [
jid: Required<Jid> = "jid",
node: Option<NodeName> = "node",
subid: Option<SubscriptionId> = "subid",
],
children: [
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm
]
);
generate_element!(
Publish, "publish", PUBSUB,
attributes: [
node: Required<NodeName> = "node",
],
children: [
items: Vec<Item> = ("item", PUBSUB) => Item
]
);
generate_element!(
PublishOptions, "publish-options", PUBSUB,
children: [
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm
]
);
generate_attribute!(
Notify,
"notify",
bool
);
generate_element!(
Retract, "retract", PUBSUB,
attributes: [
node: Required<NodeName> = "node",
notify: Default<Notify> = "notify",
],
children: [
items: Vec<Item> = ("item", PUBSUB) => Item
]
);
#[derive(Debug, Clone, PartialEq)]
pub struct SubscribeOptions {
required: bool,
}
impl TryFrom<Element> for SubscribeOptions {
type Error = 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::PUBSUB)
.append_all(if subscribe_options.required {
Some(Element::builder("required", ns::PUBSUB))
} else {
None
})
.build()
}
}
generate_element!(
Subscribe, "subscribe", PUBSUB,
attributes: [
jid: Required<Jid> = "jid",
node: Option<NodeName> = "node",
]
);
generate_element!(
Subscriptions, "subscriptions", PUBSUB,
attributes: [
node: Option<NodeName> = "node",
],
children: [
subscription: Vec<SubscriptionElem> = ("subscription", PUBSUB) => SubscriptionElem
]
);
generate_element!(
SubscriptionElem, "subscription", PUBSUB,
attributes: [
jid: Required<Jid> = "jid",
node: Option<NodeName> = "node",
subid: Option<SubscriptionId> = "subid",
subscription: Option<Subscription> = "subscription",
],
children: [
subscribe_options: Option<SubscribeOptions> = ("subscribe-options", PUBSUB) => SubscribeOptions
]
);
generate_element!(
Unsubscribe, "unsubscribe", PUBSUB,
attributes: [
jid: Required<Jid> = "jid",
node: Option<NodeName> = "node",
subid: Option<SubscriptionId> = "subid",
]
);
#[derive(Debug, Clone, PartialEq)]
pub enum PubSub {
Create {
create: Create,
configure: Option<Configure>,
},
Subscribe {
subscribe: Option<Subscribe>,
options: Option<Options>,
},
Publish {
publish: Publish,
publish_options: Option<PublishOptions>,
},
Affiliations(Affiliations),
Default(Default),
Items(Items),
Retract(Retract),
Subscription(SubscriptionElem),
Subscriptions(Subscriptions),
Unsubscribe(Unsubscribe),
}
impl IqGetPayload for PubSub {}
impl IqSetPayload for PubSub {}
impl IqResultPayload for PubSub {}
impl TryFrom<Element> for PubSub {
type Error = 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() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
}
let create = Create::try_from(child.clone())?;
payload = Some(PubSub::Create {
create,
configure: None,
});
} else if child.is("subscribe", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
}
let subscribe = Subscribe::try_from(child.clone())?;
payload = Some(PubSub::Subscribe {
subscribe: Some(subscribe),
options: None,
});
} else if child.is("options", ns::PUBSUB) {
if let Some(PubSub::Subscribe { subscribe, options }) = payload {
if options.is_some() {
return Err(Error::ParseError(
"Options is already defined in pubsub element.",
));
}
let options = Some(Options::try_from(child.clone())?);
payload = Some(PubSub::Subscribe { subscribe, options });
} else if payload.is_none() {
let options = Options::try_from(child.clone())?;
payload = Some(PubSub::Subscribe {
subscribe: None,
options: Some(options),
});
} else {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
}
} 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));
} else {
return Err(Error::ParseError("Unknown child in pubsub element."));
}
}
payload.ok_or(Error::ParseError("No payload in pubsub element."))
}
}
impl From<PubSub> for Element {
fn from(pubsub: PubSub) -> Element {
Element::builder("pubsub", ns::PUBSUB)
.append_all(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::Subscribe { subscribe, options } => {
let mut elems = vec![];
if let Some(subscribe) = subscribe {
elems.push(Element::from(subscribe));
}
if let Some(options) = options {
elems.push(Element::from(options));
}
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 crate::data_forms::{DataFormType, Field, FieldType};
#[test]
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());
}
_ => panic!(),
}
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
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());
}
_ => panic!(),
}
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
}
#[test]
fn create_and_configure_empty() {
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());
}
_ => panic!(),
}
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
}
#[test]
fn create_and_configure_simple() {
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>"
.parse()
.unwrap();
let elem1 = elem.clone();
let pubsub = PubSub::Create {
create: Create {
node: Some(NodeName(String::from("foo"))),
},
configure: Some(Configure {
form: Some(DataForm {
type_: DataFormType::Submit,
form_type: Some(String::from(ns::PUBSUB_CONFIGURE)),
title: None,
instructions: None,
fields: vec![Field {
var: String::from("pubsub#access_model"),
type_: FieldType::ListSingle,
label: None,
required: false,
options: vec![],
values: vec![String::from("whitelist")],
media: vec![],
}],
}),
}),
};
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
}
#[test]
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());
}
_ => panic!(),
}
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
}
#[test]
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());
}
_ => panic!(),
}
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
}
#[test]
fn invalid_empty_pubsub() {
let elem: Element = "<pubsub xmlns='http://jabber.org/protocol/pubsub'/>"
.parse()
.unwrap();
let error = PubSub::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "No payload in pubsub element.");
}
#[test]
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"
);
}
#[test]
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);
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);
}
#[test]
fn test_options_without_subscribe() {
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();
let elem1 = elem.clone();
let pubsub = PubSub::try_from(elem).unwrap();
match pubsub.clone() {
PubSub::Subscribe { subscribe, options } => {
assert!(subscribe.is_none());
assert!(options.is_some());
}
_ => panic!(),
}
let elem2 = Element::from(pubsub);
assert_eq!(elem1, elem2);
}
#[test]
fn test_serialize_options() {
let reference: Element = "<options xmlns='http://jabber.org/protocol/pubsub' jid='juliet@capulet.lit/balcony'><x xmlns='jabber:x:data' type='submit'/></options>"
.parse()
.unwrap();
let elem: Element = "<x xmlns='jabber:x:data' type='submit'/>".parse().unwrap();
let form = DataForm::try_from(elem).unwrap();
let options = Options {
jid: Jid::new("juliet@capulet.lit/balcony").unwrap(),
node: None,
subid: None,
form: Some(form),
};
let serialized: Element = options.into();
assert_eq!(serialized, reference);
}
#[test]
fn test_serialize_publish_options() {
let reference: Element = "<publish-options xmlns='http://jabber.org/protocol/pubsub'><x xmlns='jabber:x:data' type='submit'/></publish-options>"
.parse()
.unwrap();
let elem: Element = "<x xmlns='jabber:x:data' type='submit'/>".parse().unwrap();
let form = DataForm::try_from(elem).unwrap();
let options = PublishOptions { form: Some(form) };
let serialized: Element = options.into();
assert_eq!(serialized, reference);
}
}