disco: Split query and result.
This commit is contained in:
parent
945ae350ac
commit
76a46559b8
4 changed files with 78 additions and 44 deletions
69
src/disco.rs
69
src/disco.rs
|
|
@ -13,6 +13,41 @@ use ns;
|
|||
|
||||
use data_forms::{DataForm, DataFormType};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DiscoInfoQuery {
|
||||
pub node: Option<String>,
|
||||
}
|
||||
|
||||
impl TryFrom<Element> for DiscoInfoQuery {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(elem: Element) -> Result<DiscoInfoQuery, Error> {
|
||||
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<DiscoInfoQuery> 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,
|
||||
|
|
@ -60,17 +95,17 @@ impl IntoElements for Identity {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Disco {
|
||||
pub struct DiscoInfoResult {
|
||||
pub node: Option<String>,
|
||||
pub identities: Vec<Identity>,
|
||||
pub features: Vec<Feature>,
|
||||
pub extensions: Vec<DataForm>,
|
||||
}
|
||||
|
||||
impl TryFrom<Element> for Disco {
|
||||
impl TryFrom<Element> for DiscoInfoResult {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(elem: Element) -> Result<Disco, Error> {
|
||||
fn try_from(elem: Element) -> Result<DiscoInfoResult, Error> {
|
||||
if !elem.is("query", ns::DISCO_INFO) {
|
||||
return Err(Error::ParseError("This is not a disco#info element."));
|
||||
}
|
||||
|
|
@ -120,8 +155,6 @@ impl TryFrom<Element> for Disco {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO: encode these restrictions only for result disco#info, not get ones.
|
||||
if identities.is_empty() {
|
||||
return Err(Error::ParseError("There must be at least one identity in disco#info."));
|
||||
}
|
||||
|
|
@ -131,9 +164,8 @@ impl TryFrom<Element> for Disco {
|
|||
if !features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) {
|
||||
return Err(Error::ParseError("disco#info feature not present in disco#info."));
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(Disco {
|
||||
Ok(DiscoInfoResult {
|
||||
node: node,
|
||||
identities: identities,
|
||||
features: features,
|
||||
|
|
@ -142,7 +174,7 @@ impl TryFrom<Element> for Disco {
|
|||
}
|
||||
}
|
||||
|
||||
impl Into<Element> for Disco {
|
||||
impl Into<Element> for DiscoInfoResult {
|
||||
fn into(self) -> Element {
|
||||
for _ in self.extensions {
|
||||
panic!("Not yet implemented!");
|
||||
|
|
@ -163,7 +195,7 @@ mod tests {
|
|||
#[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 = Disco::try_from(elem).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);
|
||||
|
|
@ -173,7 +205,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_invalid() {
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -184,7 +216,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_invalid_identity() {
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -192,7 +224,7 @@ mod tests {
|
|||
assert_eq!(message, "Required attribute 'category' missing.");
|
||||
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -200,7 +232,7 @@ mod tests {
|
|||
assert_eq!(message, "Identity must have a non-empty 'category' attribute.");
|
||||
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -208,7 +240,7 @@ mod tests {
|
|||
assert_eq!(message, "Required attribute 'type' missing.");
|
||||
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -219,7 +251,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_invalid_feature() {
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -228,10 +260,9 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_invalid_result() {
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -239,7 +270,7 @@ mod tests {
|
|||
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 = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -247,7 +278,7 @@ mod tests {
|
|||
assert_eq!(message, "There must be at least one feature in disco#info.");
|
||||
|
||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#items'/></query>".parse().unwrap();
|
||||
let error = Disco::try_from(elem).unwrap_err();
|
||||
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue