// 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 try_from::TryFrom; use minidom::Element; use error::Error; use ns; use data_forms::{DataForm, DataFormType}; #[derive(Debug, Clone)] pub struct DiscoInfoQuery { pub node: Option, } impl TryFrom for DiscoInfoQuery { type Err = Error; fn try_from(elem: Element) -> Result { if !elem.is("query", ns::DISCO_INFO) { return Err(Error::ParseError("This is not a disco#info element.")); } for _ in elem.children() { return Err(Error::ParseError("Unknown child in disco#info.")); } for (attr, _) in elem.attrs() { if attr != "node" { return Err(Error::ParseError("Unknown attribute in disco#info.")); } } 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() } } #[derive(Debug, Clone, PartialEq)] pub struct Feature { pub var: String, } impl From for Element { fn from(feature: Feature) -> Element { Element::builder("feature") .ns(ns::DISCO_INFO) .attr("var", feature.var) .build() } } #[derive(Debug, Clone)] pub struct Identity { pub category: String, // TODO: use an enum here. pub type_: String, // TODO: use an enum here. pub lang: Option, pub name: Option, } 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() } } #[derive(Debug, Clone)] pub struct DiscoInfoResult { pub node: Option, pub identities: Vec, pub features: Vec, pub extensions: Vec, } impl TryFrom for DiscoInfoResult { type Err = Error; fn try_from(elem: Element) -> Result { if !elem.is("query", ns::DISCO_INFO) { return Err(Error::ParseError("This is not a disco#info element.")); } let mut identities: Vec = vec!(); let mut features: Vec = vec!(); let mut extensions: Vec = vec!(); let node = get_attr!(elem, "node", optional); for child in elem.children() { if child.is("feature", ns::DISCO_INFO) { let feature = get_attr!(child, "var", required); features.push(Feature { var: feature, }); } else if child.is("identity", ns::DISCO_INFO) { let category = get_attr!(child, "category", required); if category == "" { return Err(Error::ParseError("Identity must have a non-empty 'category' attribute.")) } let type_ = get_attr!(child, "type", required); if type_ == "" { return Err(Error::ParseError("Identity must have a non-empty 'type' attribute.")) } let lang = get_attr!(child, "xml:lang", optional); let name = get_attr!(child, "name", optional); identities.push(Identity { category: category, type_: type_, lang: lang, name: name, }); } 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.")); } match data_form.form_type { Some(_) => extensions.push(data_form), None => return Err(Error::ParseError("Data form found without a FORM_TYPE.")), } } else { return Err(Error::ParseError("Unknown element in disco#info.")); } } if identities.is_empty() { return Err(Error::ParseError("There must be at least one identity in disco#info.")); } if features.is_empty() { return Err(Error::ParseError("There must be at least one feature in disco#info.")); } if !features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) { return Err(Error::ParseError("disco#info feature not present in disco#info.")); } Ok(DiscoInfoResult { node: node, identities: identities, features: features, extensions: extensions }) } } impl From for Element { fn from(disco: DiscoInfoResult) -> Element { for _ in disco.extensions { panic!("Not yet implemented!"); } Element::builder("query") .ns(ns::DISCO_INFO) .attr("node", disco.node) .append(disco.identities) .append(disco.features) .build() } } #[cfg(test)] mod tests { use super::*; #[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_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."); } }