use xso::{AsXml, FromXml};
use crate::data_forms::DataForm;
use crate::iq::{IqGetPayload, IqResultPayload};
use crate::ns;
use crate::rsm::{SetQuery, SetResult};
use jid::Jid;
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::DISCO_INFO, name = "query")]
pub struct DiscoInfoQuery {
#[xml(attribute(default))]
pub node: Option<String>,
}
impl IqGetPayload for DiscoInfoQuery {}
#[derive(FromXml, AsXml, Debug, Clone, PartialEq, Eq, Hash)]
#[xml(namespace = ns::DISCO_INFO, name = "feature")]
pub struct Feature {
#[xml(attribute)]
pub var: String,
}
impl Feature {
pub fn new<S: Into<String>>(var: S) -> Feature {
Feature { var: var.into() }
}
}
#[derive(FromXml, AsXml, Debug, Clone, PartialEq, Eq, Hash)]
#[xml(namespace = ns::DISCO_INFO, name = "identity")]
pub struct Identity {
#[xml(attribute)]
pub category: String,
#[xml(attribute = "type")]
pub type_: String,
#[xml(attribute(default, name = "xml:lang"))]
pub lang: Option<String>,
#[xml(attribute(default))]
pub name: Option<String>,
}
impl Identity {
pub fn new<C, T, L, N>(category: C, type_: T, lang: L, name: N) -> Identity
where
C: Into<String>,
T: Into<String>,
L: Into<String>,
N: Into<String>,
{
Identity {
category: category.into(),
type_: type_.into(),
lang: Some(lang.into()),
name: Some(name.into()),
}
}
pub fn new_anonymous<C, T, L, N>(category: C, type_: T) -> Identity
where
C: Into<String>,
T: Into<String>,
{
Identity {
category: category.into(),
type_: type_.into(),
lang: None,
name: None,
}
}
}
#[derive(FromXml, AsXml, Debug, Clone)]
#[xml(namespace = ns::DISCO_INFO, name = "query")]
pub struct DiscoInfoResult {
#[xml(attribute(default))]
pub node: Option<String>,
#[xml(child(n = ..))]
pub identities: Vec<Identity>,
#[xml(child(n = ..))]
pub features: Vec<Feature>,
#[xml(child(n = ..))]
pub extensions: Vec<DataForm>,
}
impl IqResultPayload for DiscoInfoResult {}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::DISCO_ITEMS, name = "query")]
pub struct DiscoItemsQuery {
#[xml(attribute(default))]
pub node: Option<String>,
#[xml(child(default))]
pub rsm: Option<SetQuery>,
}
impl IqGetPayload for DiscoItemsQuery {}
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::DISCO_ITEMS, name = "item")]
pub struct Item {
#[xml(attribute)]
pub jid: Jid,
#[xml(attribute(default))]
pub node: Option<String>,
#[xml(attribute(default))]
pub name: Option<String>,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::DISCO_ITEMS, name = "query")]
pub struct DiscoItemsResult {
#[xml(attribute(default))]
pub node: Option<String>,
#[xml(child(n = ..))]
pub items: Vec<Item>,
#[xml(child(default))]
pub rsm: Option<SetResult>,
}
impl IqResultPayload for DiscoItemsResult {}
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
use minidom::Element;
use xso::error::{Error, FromElementError};
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Identity, 48);
assert_size!(Feature, 12);
assert_size!(DiscoInfoQuery, 12);
assert_size!(DiscoInfoResult, 48);
assert_size!(Item, 40);
assert_size!(DiscoItemsQuery, 52);
assert_size!(DiscoItemsResult, 64);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Identity, 96);
assert_size!(Feature, 24);
assert_size!(DiscoInfoQuery, 24);
assert_size!(DiscoInfoResult, 96);
assert_size!(Item, 80);
assert_size!(DiscoItemsQuery, 104);
assert_size!(DiscoItemsResult, 128);
}
#[test]
fn test_simple() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert!(query.node.is_none());
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert!(query.extensions.is_empty());
}
#[test]
fn test_identity_after_feature() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature var='http://jabber.org/protocol/disco#info'/><identity category='client' type='pc'/></query>".parse().unwrap();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert!(query.extensions.is_empty());
}
#[test]
fn test_feature_after_dataform() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>coucou</value></field></x><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert_eq!(query.extensions.len(), 1);
}
#[test]
fn test_extension() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>example</value></field></x></query>".parse().unwrap();
let elem1 = elem.clone();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert!(query.node.is_none());
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert_eq!(query.extensions.len(), 1);
assert_eq!(query.extensions[0].form_type, Some(String::from("example")));
let elem2 = query.into();
assert_eq!(elem1, elem2);
}
#[test]
fn test_invalid() {
let elem: Element =
"<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>"
.parse()
.unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in DiscoInfoResult element.");
}
#[test]
fn test_invalid_identity() {
let elem: Element =
"<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>"
.parse()
.unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(
message,
"Required attribute field 'category' on Identity element missing."
);
let elem: Element =
"<query xmlns='http://jabber.org/protocol/disco#info'><identity type='coucou'/></query>"
.parse()
.unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(
message,
"Required attribute field 'category' on Identity element missing."
);
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(
message,
"Required attribute field 'type_' on Identity element missing."
);
}
#[test]
fn test_invalid_feature() {
let elem: Element =
"<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>"
.parse()
.unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(
message,
"Required attribute field 'var' on Feature element missing."
);
}
#[test]
#[ignore]
fn test_invalid_result() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>"
.parse()
.unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(
message,
"There must be at least one identity in disco#info."
);
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
let error = DiscoInfoResult::try_from(elem).unwrap_err();
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "There must be at least one feature in disco#info.");
}
#[test]
fn test_simple_items() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
.parse()
.unwrap();
let query = DiscoItemsQuery::try_from(elem).unwrap();
assert!(query.node.is_none());
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>"
.parse()
.unwrap();
let query = DiscoItemsQuery::try_from(elem).unwrap();
assert_eq!(query.node, Some(String::from("coucou")));
}
#[test]
fn test_simple_items_result() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
.parse()
.unwrap();
let query = DiscoItemsResult::try_from(elem).unwrap();
assert!(query.node.is_none());
assert!(query.items.is_empty());
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>"
.parse()
.unwrap();
let query = DiscoItemsResult::try_from(elem).unwrap();
assert_eq!(query.node, Some(String::from("coucou")));
assert!(query.items.is_empty());
}
#[test]
fn test_answers_items_result() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'><item jid='component'/><item jid='component2' node='test' name='A component'/></query>".parse().unwrap();
let query = DiscoItemsResult::try_from(elem).unwrap();
let elem2 = Element::from(query);
let query = DiscoItemsResult::try_from(elem2).unwrap();
assert_eq!(query.items.len(), 2);
assert_eq!(query.items[0].jid, BareJid::new("component").unwrap());
assert_eq!(query.items[0].node, None);
assert_eq!(query.items[0].name, None);
assert_eq!(query.items[1].jid, BareJid::new("component2").unwrap());
assert_eq!(query.items[1].node, Some(String::from("test")));
assert_eq!(query.items[1].name, Some(String::from("A component")));
}
#[test]
fn test_missing_disco_info_feature_workaround() {
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/muc#user'/></query>".parse().unwrap();
let query = DiscoInfoResult::try_from(elem).unwrap();
assert_eq!(query.identities.len(), 1);
assert_eq!(query.features.len(), 1);
assert!(query.extensions.is_empty());
}
}