// 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/. #![deny(missing_docs)] use try_from::TryFrom; use minidom::Element; use jid::Jid; use error::Error; use ns; use data_forms::{DataForm, DataFormType}; /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only represent /// the request, and not a result. #[derive(Debug, Clone)] pub struct DiscoInfoQuery { /// Node on which we are doing the discovery. pub node: Option, } impl TryFrom for DiscoInfoQuery { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "query", ns::DISCO_INFO); check_no_children!(elem, "query"); check_no_unknown_attributes!(elem, "query", ["node"]); Ok(DiscoInfoQuery { node: get_attr!(elem, "node", optional), }) } } impl From for Element { fn from(disco: DiscoInfoQuery) -> Element { Element::builder("query") .ns(ns::DISCO_INFO) .attr("node", disco.node) .build() } } /// Structure representing a `` element. #[derive(Debug, Clone, PartialEq)] pub struct Feature { /// Namespace of the feature we want to represent. pub var: String, } impl TryFrom for Feature { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "feature", ns::DISCO_INFO, "disco#info feature"); check_no_children!(elem, "disco#info feature"); check_no_unknown_attributes!(elem, "disco#info feature", ["var"]); Ok(Feature { var: get_attr!(elem, "var", required) }) } } impl From for Element { fn from(feature: Feature) -> Element { Element::builder("feature") .ns(ns::DISCO_INFO) .attr("var", feature.var) .build() } } /// Structure representing an `` element. #[derive(Debug, Clone)] pub struct Identity { /// Category of this identity. pub category: String, // TODO: use an enum here. /// Type of this identity. pub type_: String, // TODO: use an enum here. /// Lang of the name of this identity. pub lang: Option, /// Name of this identity. pub name: Option, } impl TryFrom for Identity { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "identity", ns::DISCO_INFO, "disco#info identity"); check_no_children!(elem, "disco#info identity"); check_no_unknown_attributes!(elem, "disco#info identity", ["category", "type", "xml:lang", "name"]); let category = get_attr!(elem, "category", required); if category == "" { return Err(Error::ParseError("Identity must have a non-empty 'category' attribute.")) } let type_ = get_attr!(elem, "type", required); if type_ == "" { return Err(Error::ParseError("Identity must have a non-empty 'type' attribute.")) } Ok(Identity { category: category, type_: type_, lang: get_attr!(elem, "xml:lang", optional), name: get_attr!(elem, "name", optional), }) } } impl From for Element { fn from(identity: Identity) -> Element { Element::builder("identity") .ns(ns::DISCO_INFO) .attr("category", identity.category) .attr("type", identity.type_) .attr("xml:lang", identity.lang) .attr("name", identity.name) .build() } } /// 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 TryFrom for DiscoInfoResult { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "query", ns::DISCO_INFO, "disco#info result"); check_no_unknown_attributes!(elem, "disco#info result", ["node"]); let mut result = DiscoInfoResult { node: get_attr!(elem, "node", optional), identities: vec!(), features: vec!(), extensions: vec!(), }; for child in elem.children() { if child.is("feature", ns::DISCO_INFO) { let feature = Feature::try_from(child.clone())?; result.features.push(feature); } else if child.is("identity", ns::DISCO_INFO) { let identity = Identity::try_from(child.clone())?; result.identities.push(identity); } 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::ParseError("Data form must have a 'result' type in disco#info.")); } if data_form.form_type.is_none() { return Err(Error::ParseError("Data form found without a FORM_TYPE.")); } result.extensions.push(data_form); } else { return Err(Error::ParseError("Unknown element in disco#info.")); } } if result.identities.is_empty() { return Err(Error::ParseError("There must be at least one identity in disco#info.")); } if result.features.is_empty() { return Err(Error::ParseError("There must be at least one feature in disco#info.")); } if !result.features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) { return Err(Error::ParseError("disco#info feature not present in disco#info.")); } Ok(result) } } impl From for Element { fn from(disco: DiscoInfoResult) -> Element { Element::builder("query") .ns(ns::DISCO_INFO) .attr("node", disco.node) .append(disco.identities) .append(disco.features) .append(disco.extensions) .build() } } /// Structure representing a `` element. /// /// It should only be used in an ``, as it can only represent /// the request, and not a result. #[derive(Debug, Clone)] pub struct DiscoItemsQuery { /// Node on which we are doing the discovery. pub node: Option, } impl TryFrom for DiscoItemsQuery { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query"); check_no_children!(elem, "disco#items query"); check_no_unknown_attributes!(elem, "disco#items query", ["node"]); Ok(DiscoItemsQuery { node: get_attr!(elem, "node", optional), }) } } impl From for Element { fn from(disco: DiscoItemsQuery) -> Element { Element::builder("query") .ns(ns::DISCO_ITEMS) .attr("node", disco.node) .build() } } /// Structure representing an `` element. #[derive(Debug, Clone)] pub struct Item { /// JID of the entity pointed by this item. pub jid: Jid, /// Node of the entity pointed by this item. pub node: Option, /// Name of the entity pointed by this item. pub name: Option, } impl TryFrom for Item { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "item", ns::DISCO_ITEMS); check_no_children!(elem, "item"); check_no_unknown_attributes!(elem, "item", ["jid", "node", "name"]); Ok(Item { jid: get_attr!(elem, "jid", required), node: get_attr!(elem, "node", optional), name: get_attr!(elem, "name", optional), }) } } impl From for Element { fn from(item: Item) -> Element { Element::builder("item") .ns(ns::DISCO_ITEMS) .attr("jid", item.jid) .attr("node", item.node) .attr("name", item.name) .build() } } /// 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 DiscoItemsResult { /// Node on which we have done this discovery. pub node: Option, /// List of items pointed by this entity. pub items: Vec, } impl TryFrom for DiscoItemsResult { type Err = Error; fn try_from(elem: Element) -> Result { check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query"); check_no_unknown_attributes!(elem, "disco#items query", ["node"]); let mut items: Vec = vec!(); for child in elem.children() { if child.is("item", ns::DISCO_ITEMS) { items.push(Item::try_from(child.clone())?); } else { return Err(Error::ParseError("Unknown element in disco#items.")); } } Ok(DiscoItemsResult { node: get_attr!(elem, "node", optional), items: items, }) } } impl From for Element { fn from(disco: DiscoItemsResult) -> Element { Element::builder("query") .ns(ns::DISCO_ITEMS) .attr("node", disco.node) .append(disco.items) .build() } } #[cfg(test)] mod tests { use super::*; use compare_elements::NamespaceAwareCompare; use std::str::FromStr; #[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_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!(elem1.compare_to(&elem2)); } #[test] fn test_invalid() { let elem: Element = "".parse().unwrap(); let error = DiscoInfoResult::try_from(elem).unwrap_err(); let message = match error { Error::ParseError(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 { Error::ParseError(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 { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "Identity must have a non-empty 'category' attribute."); let elem: Element = "".parse().unwrap(); let error = DiscoInfoResult::try_from(elem).unwrap_err(); let message = match error { Error::ParseError(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 { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "Identity must have a non-empty 'type' attribute."); } #[test] fn test_invalid_feature() { let elem: Element = "".parse().unwrap(); let error = DiscoInfoResult::try_from(elem).unwrap_err(); let message = match error { Error::ParseError(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 { Error::ParseError(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 { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "There must be at least one feature in disco#info."); let elem: Element = "".parse().unwrap(); let error = DiscoInfoResult::try_from(elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "disco#info feature not present 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(); assert_eq!(query.items.len(), 2); assert_eq!(query.items[0].jid, Jid::from_str("component").unwrap()); assert_eq!(query.items[0].node, None); assert_eq!(query.items[0].name, None); assert_eq!(query.items[1].jid, Jid::from_str("component2").unwrap()); assert_eq!(query.items[1].node, Some(String::from("test"))); assert_eq!(query.items[1].name, Some(String::from("A component"))); } }