disco: Use the new helper macros to simplify parsing.

This commit is contained in:
Emmanuel Gil Peyrot 2017-10-10 19:00:42 +01:00
parent 9f27f200ca
commit 5f6f6a5e91

View file

@ -30,17 +30,9 @@ impl TryFrom<Element> for DiscoInfoQuery {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<DiscoInfoQuery, Error> { fn try_from(elem: Element) -> Result<DiscoInfoQuery, Error> {
if !elem.is("query", ns::DISCO_INFO) { check_self!(elem, "query", ns::DISCO_INFO);
return Err(Error::ParseError("This is not a disco#info element.")); check_no_children!(elem, "query");
} check_no_unknown_attributes!(elem, "query", ["node"]);
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 { Ok(DiscoInfoQuery {
node: get_attr!(elem, "node", optional), node: get_attr!(elem, "node", optional),
}) })
@ -67,17 +59,9 @@ impl TryFrom<Element> for Feature {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<Feature, Error> { fn try_from(elem: Element) -> Result<Feature, Error> {
if !elem.is("feature", ns::DISCO_INFO) { check_self!(elem, "feature", ns::DISCO_INFO, "disco#info feature");
return Err(Error::ParseError("This is not a disco#info feature element.")); check_no_children!(elem, "disco#info feature");
} check_no_unknown_attributes!(elem, "disco#info feature", ["var"]);
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in disco#info feature element."));
}
for (attr, _) in elem.attrs() {
if attr != "var" {
return Err(Error::ParseError("Unknown attribute in disco#info feature element."));
}
}
Ok(Feature { Ok(Feature {
var: get_attr!(elem, "var", required) var: get_attr!(elem, "var", required)
}) })
@ -113,9 +97,9 @@ impl TryFrom<Element> for Identity {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<Identity, Error> { fn try_from(elem: Element) -> Result<Identity, Error> {
if !elem.is("identity", ns::DISCO_INFO) { check_self!(elem, "identity", ns::DISCO_INFO, "disco#info identity");
return Err(Error::ParseError("This is not a disco#info identity element.")); 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); let category = get_attr!(elem, "category", required);
if category == "" { if category == "" {
@ -171,9 +155,8 @@ impl TryFrom<Element> for DiscoInfoResult {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<DiscoInfoResult, Error> { fn try_from(elem: Element) -> Result<DiscoInfoResult, Error> {
if !elem.is("query", ns::DISCO_INFO) { check_self!(elem, "query", ns::DISCO_INFO, "disco#info result");
return Err(Error::ParseError("This is not a disco#info element.")); check_no_unknown_attributes!(elem, "disco#info result", ["node"]);
}
let mut result = DiscoInfoResult { let mut result = DiscoInfoResult {
node: get_attr!(elem, "node", optional), node: get_attr!(elem, "node", optional),
@ -243,17 +226,9 @@ impl TryFrom<Element> for DiscoItemsQuery {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<DiscoItemsQuery, Error> { fn try_from(elem: Element) -> Result<DiscoItemsQuery, Error> {
if !elem.is("query", ns::DISCO_ITEMS) { check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query");
return Err(Error::ParseError("This is not a disco#items element.")); check_no_children!(elem, "disco#items query");
} check_no_unknown_attributes!(elem, "disco#items query", ["node"]);
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in disco#items."));
}
for (attr, _) in elem.attrs() {
if attr != "node" {
return Err(Error::ParseError("Unknown attribute in disco#items."));
}
}
Ok(DiscoItemsQuery { Ok(DiscoItemsQuery {
node: get_attr!(elem, "node", optional), node: get_attr!(elem, "node", optional),
}) })
@ -284,17 +259,9 @@ impl TryFrom<Element> for Item {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<Item, Error> { fn try_from(elem: Element) -> Result<Item, Error> {
if !elem.is("item", ns::DISCO_ITEMS) { check_self!(elem, "item", ns::DISCO_ITEMS);
return Err(Error::ParseError("This is not an item element.")); check_no_children!(elem, "item");
} check_no_unknown_attributes!(elem, "item", ["jid", "node", "name"]);
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in item element."));
}
for (attr, _) in elem.attrs() {
if attr != "jid" && attr != "node" && attr != "name" {
return Err(Error::ParseError("Unknown attribute in item element."));
}
}
Ok(Item { Ok(Item {
jid: get_attr!(elem, "jid", required), jid: get_attr!(elem, "jid", required),
node: get_attr!(elem, "node", optional), node: get_attr!(elem, "node", optional),
@ -332,14 +299,8 @@ impl TryFrom<Element> for DiscoItemsResult {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<DiscoItemsResult, Error> { fn try_from(elem: Element) -> Result<DiscoItemsResult, Error> {
if !elem.is("query", ns::DISCO_ITEMS) { check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query");
return Err(Error::ParseError("This is not a disco#items element.")); check_no_unknown_attributes!(elem, "disco#items query", ["node"]);
}
for (attr, _) in elem.attrs() {
if attr != "node" {
return Err(Error::ParseError("Unknown attribute in disco#items."));
}
}
let mut items: Vec<Item> = vec!(); let mut items: Vec<Item> = vec!();
for child in elem.children() { for child in elem.children() {