chatstates: Improve parsing.

This commit is contained in:
Emmanuel Gil Peyrot 2017-06-25 19:55:55 +01:00
parent eca588ad6f
commit 13e8507848

View file

@ -25,25 +25,23 @@ impl TryFrom<Element> for ChatState {
type Error = Error; type Error = Error;
fn try_from(elem: Element) -> Result<ChatState, Error> { fn try_from(elem: Element) -> Result<ChatState, Error> {
if elem.ns() != Some(ns::CHATSTATES) {
return Err(Error::ParseError("This is not a chatstate element."));
}
for _ in elem.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in chatstate element.")); return Err(Error::ParseError("Unknown child in chatstate element."));
} }
for _ in elem.attrs() { for _ in elem.attrs() {
return Err(Error::ParseError("Unknown attribute in chatstate element.")); return Err(Error::ParseError("Unknown attribute in chatstate element."));
} }
if elem.is("active", ns::CHATSTATES) { Ok(match elem.name() {
Ok(ChatState::Active) "active" => ChatState::Active,
} else if elem.is("composing", ns::CHATSTATES) { "composing" => ChatState::Composing,
Ok(ChatState::Composing) "gone" => ChatState::Gone,
} else if elem.is("gone", ns::CHATSTATES) { "inactive" => ChatState::Inactive,
Ok(ChatState::Gone) "paused" => ChatState::Paused,
} else if elem.is("inactive", ns::CHATSTATES) { _ => return Err(Error::ParseError("This is not a chatstate element.")),
Ok(ChatState::Inactive) })
} else if elem.is("paused", ns::CHATSTATES) {
Ok(ChatState::Paused)
} else {
Err(Error::ParseError("This is not a chatstate element."))
}
} }
} }