chatstates: Switch to Into/TryFrom.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-06 20:33:58 +01:00
parent a3a90e4eda
commit e451520187
2 changed files with 44 additions and 39 deletions

View file

@ -4,6 +4,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::convert::TryFrom;
use minidom::Element; use minidom::Element;
use error::Error; use error::Error;
@ -19,53 +21,56 @@ pub enum ChatState {
Paused, Paused,
} }
pub fn parse_chatstate(root: &Element) -> Result<ChatState, Error> { impl<'a> TryFrom<&'a Element> for ChatState {
for _ in root.children() { type Error = Error;
return Err(Error::ParseError("Unknown child in chatstate element."));
} fn try_from(elem: &'a Element) -> Result<ChatState, Error> {
if root.is("active", ns::CHATSTATES) { for _ in elem.children() {
Ok(ChatState::Active) return Err(Error::ParseError("Unknown child in chatstate element."));
} else if root.is("composing", ns::CHATSTATES) { }
Ok(ChatState::Composing) if elem.is("active", ns::CHATSTATES) {
} else if root.is("gone", ns::CHATSTATES) { Ok(ChatState::Active)
Ok(ChatState::Gone) } else if elem.is("composing", ns::CHATSTATES) {
} else if root.is("inactive", ns::CHATSTATES) { Ok(ChatState::Composing)
Ok(ChatState::Inactive) } else if elem.is("gone", ns::CHATSTATES) {
} else if root.is("paused", ns::CHATSTATES) { Ok(ChatState::Gone)
Ok(ChatState::Paused) } else if elem.is("inactive", ns::CHATSTATES) {
} else { Ok(ChatState::Inactive)
Err(Error::ParseError("This is not a chatstate element.")) } else if elem.is("paused", ns::CHATSTATES) {
Ok(ChatState::Paused)
} else {
Err(Error::ParseError("This is not a chatstate element."))
}
} }
} }
pub fn serialise(chatstate: &ChatState) -> Element { impl<'a> Into<Element> for &'a ChatState {
Element::builder(match *chatstate { fn into(self) -> Element {
ChatState::Active => "active", Element::builder(match *self {
ChatState::Composing => "composing", ChatState::Active => "active",
ChatState::Gone => "gone", ChatState::Composing => "composing",
ChatState::Inactive => "inactive", ChatState::Gone => "gone",
ChatState::Paused => "paused", ChatState::Inactive => "inactive",
}).ns(ns::CHATSTATES) ChatState::Paused => "paused",
.build() }).ns(ns::CHATSTATES)
.build()
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use minidom::Element; use super::*;
use error::Error;
use chatstates;
use ns;
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap(); let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
chatstates::parse_chatstate(&elem).unwrap(); ChatState::try_from(&elem).unwrap();
} }
#[test] #[test]
fn test_invalid() { fn test_invalid() {
let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap(); let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
let error = chatstates::parse_chatstate(&elem).unwrap_err(); let error = ChatState::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -76,7 +81,7 @@ mod tests {
#[test] #[test]
fn test_invalid_child() { fn test_invalid_child() {
let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap(); let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap();
let error = chatstates::parse_chatstate(&elem).unwrap_err(); let error = ChatState::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -88,7 +93,7 @@ mod tests {
#[ignore] #[ignore]
fn test_invalid_attribute() { fn test_invalid_attribute() {
let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap(); let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap();
let error = chatstates::parse_chatstate(&elem).unwrap_err(); let error = ChatState::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -98,8 +103,8 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let chatstate = chatstates::ChatState::Active; let chatstate = ChatState::Active;
let elem = chatstates::serialise(&chatstate); let elem: Element = (&chatstate).into();
assert!(elem.is("active", ns::CHATSTATES)); assert!(elem.is("active", ns::CHATSTATES));
} }
} }

View file

@ -17,7 +17,7 @@ use ns;
use body; use body;
use stanza_error; use stanza_error;
use chatstates; use chatstates::ChatState;
use receipts::Receipt; use receipts::Receipt;
use delay; use delay;
use attention::Attention; use attention::Attention;
@ -29,7 +29,7 @@ use eme;
pub enum MessagePayload { pub enum MessagePayload {
Body(body::Body), Body(body::Body),
StanzaError(stanza_error::StanzaError), StanzaError(stanza_error::StanzaError),
ChatState(chatstates::ChatState), ChatState(ChatState),
Receipt(Receipt), Receipt(Receipt),
Delay(delay::Delay), Delay(delay::Delay),
Attention(Attention), Attention(Attention),
@ -115,7 +115,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
Some(MessagePayload::Body(body)) Some(MessagePayload::Body(body))
} else if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) { } else if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) {
Some(MessagePayload::StanzaError(stanza_error)) Some(MessagePayload::StanzaError(stanza_error))
} else if let Ok(chatstate) = chatstates::parse_chatstate(elem) { } else if let Ok(chatstate) = ChatState::try_from(elem) {
Some(MessagePayload::ChatState(chatstate)) Some(MessagePayload::ChatState(chatstate))
} else if let Ok(receipt) = Receipt::try_from(elem) { } else if let Ok(receipt) = Receipt::try_from(elem) {
Some(MessagePayload::Receipt(receipt)) Some(MessagePayload::Receipt(receipt))
@ -149,7 +149,7 @@ pub fn serialise_payload(payload: &MessagePayload) -> Element {
MessagePayload::Body(ref body) => body::serialise(body), MessagePayload::Body(ref body) => body::serialise(body),
MessagePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error), MessagePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
MessagePayload::Attention(ref attention) => attention.into(), MessagePayload::Attention(ref attention) => attention.into(),
MessagePayload::ChatState(ref chatstate) => chatstates::serialise(chatstate), MessagePayload::ChatState(ref chatstate) => chatstate.into(),
MessagePayload::Receipt(ref receipt) => receipt.into(), MessagePayload::Receipt(ref receipt) => receipt.into(),
MessagePayload::Delay(ref delay) => delay::serialise(delay), MessagePayload::Delay(ref delay) => delay::serialise(delay),
MessagePayload::MessageCorrect(ref replace) => replace.into(), MessagePayload::MessageCorrect(ref replace) => replace.into(),