// Copyright (c) 2017 Emmanuel Gil Peyrot // // 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/. use crate::data_forms::{DataForm, DataFormType}; use crate::iq::{IqGetPayload, IqResultPayload}; use crate::ns; use crate::rsm::{SetQuery, SetResult}; use crate::Element; use jid::Jid; use xso::error::{Error, FromElementError}; generate_element!( /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only represent /// the request, and not a result. DiscoInfoQuery, "query", DISCO_INFO, attributes: [ /// Node on which we are doing the discovery. node: Option = "node", ]); impl IqGetPayload for DiscoInfoQuery {} generate_element!( #[derive(Eq, Hash)] /// Structure representing a `` element. Feature, "feature", DISCO_INFO, attributes: [ /// Namespace of the feature we want to represent. var: Required = "var", ]); impl Feature { /// Create a new `` with the according `@var`. pub fn new>(var: S) -> Feature { Feature { var: var.into() } } } generate_element!( /// Structure representing an `` element. Identity, "identity", DISCO_INFO, attributes: [ /// Category of this identity. // TODO: use an enum here. category: RequiredNonEmpty = "category", /// Type of this identity. // TODO: use an enum here. type_: RequiredNonEmpty = "type", /// Lang of the name of this identity. lang: Option = "xml:lang", /// Name of this identity. name: Option = "name", ] ); impl Identity { /// Create a new ``. pub fn new(category: C, type_: T, lang: L, name: N) -> Identity where C: Into, T: Into, L: Into, N: Into, { Identity { category: category.into(), type_: type_.into(), lang: Some(lang.into()), name: Some(name.into()), } } /// Create a new `` without a name. pub fn new_anonymous(category: C, type_: T) -> Identity where C: Into, T: Into, { Identity { category: category.into(), type_: type_.into(), lang: None, name: None, } } } /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only /// represent the result, and not a request. #[derive(Debug, Clone)] pub struct DiscoInfoResult { /// Node on which we have done this discovery. pub node: Option, /// List of identities exposed by this entity. pub identities: Vec, /// List of features supported by this entity. pub features: Vec, /// List of extensions reported by this entity. pub extensions: Vec, } impl IqResultPayload for DiscoInfoResult {} impl TryFrom for DiscoInfoResult { type Error = FromElementError; fn try_from(elem: Element) -> Result { check_self!(elem, "query", DISCO_INFO, "disco#info result"); check_no_unknown_attributes!(elem, "disco#info result", ["node"]); let mut result = DiscoInfoResult { node: get_attr!(elem, "node", Option), identities: vec![], features: vec![], extensions: vec![], }; for child in elem.children() { if child.is("identity", ns::DISCO_INFO) { let identity = Identity::try_from(child.clone())?; result.identities.push(identity); } else if child.is("feature", ns::DISCO_INFO) { let feature = Feature::try_from(child.clone())?; result.features.push(feature); } else if child.is("x", ns::DATA_FORMS) { let data_form = DataForm::try_from(child.clone())?; if data_form.type_ != DataFormType::Result_ { return Err( Error::Other("Data form must have a 'result' type in disco#info.").into(), ); } if data_form.form_type.is_none() { return Err(Error::Other("Data form found without a FORM_TYPE.").into()); } result.extensions.push(data_form); } else { return Err(Error::Other("Unknown element in disco#info.").into()); } } #[cfg(not(feature = "disable-validation"))] { if result.identities.is_empty() { return Err( Error::Other("There must be at least one identity in disco#info.").into(), ); } if result.features.is_empty() { return Err( Error::Other("There must be at least one feature in disco#info.").into(), ); } } Ok(result) } } impl From for Element { fn from(disco: DiscoInfoResult) -> Element { Element::builder("query", ns::DISCO_INFO) .attr("node", disco.node) .append_all(disco.identities) .append_all(disco.features) .append_all(disco.extensions.iter().cloned().map(Element::from)) .build() } } generate_element!( /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only represent /// the request, and not a result. DiscoItemsQuery, "query", DISCO_ITEMS, attributes: [ /// Node on which we are doing the discovery. node: Option = "node", ], children: [ /// Optional paging via Result Set Management rsm: Option = ("set", RSM) => SetQuery, ]); impl IqGetPayload for DiscoItemsQuery {} generate_element!( /// Structure representing an `` element. Item, "item", DISCO_ITEMS, attributes: [ /// JID of the entity pointed by this item. jid: Required = "jid", /// Node of the entity pointed by this item. node: Option = "node", /// Name of the entity pointed by this item. name: Option = "name", ]); generate_element!( /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only /// represent the result, and not a request. DiscoItemsResult, "query", DISCO_ITEMS, attributes: [ /// Node on which we have done this discovery. node: Option = "node" ], children: [ /// List of items pointed by this entity. items: Vec = ("item", DISCO_ITEMS) => Item, /// Optional paging via Result Set Management rsm: Option = ("set", RSM) => SetResult, ] ); impl IqResultPayload for DiscoItemsResult {} #[cfg(test)] mod tests { use super::*; use jid::BareJid; #[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 = "".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 = "".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 = "coucou".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 = "example".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 = "" .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 element in disco#info."); } #[test] fn test_invalid_identity() { let elem: Element = "" .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 'category' missing."); let elem: Element = "" .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 'category' must not be empty."); let elem: Element = "".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 'type' missing."); let elem: Element = "".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 'type' must not be empty."); } #[test] fn test_invalid_feature() { let elem: Element = "" .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 'var' missing."); } #[test] fn test_invalid_result() { let elem: Element = "" .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 = "".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 = "" .parse() .unwrap(); let query = DiscoItemsQuery::try_from(elem).unwrap(); assert!(query.node.is_none()); let elem: Element = "" .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 = "" .parse() .unwrap(); let query = DiscoItemsResult::try_from(elem).unwrap(); assert!(query.node.is_none()); assert!(query.items.is_empty()); let elem: Element = "" .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 = "".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"))); } }