From 6a0724d1335a3c80efd1f8f71a27cc908574d409 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 19 Apr 2017 23:21:53 +0100 Subject: [PATCH] Add a MessagePayload trait, and implement it for Body and ChatState. --- src/chatstates.rs | 3 +++ src/lib.rs | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/chatstates.rs b/src/chatstates.rs index 6ec2e22d..8941408d 100644 --- a/src/chatstates.rs +++ b/src/chatstates.rs @@ -1,6 +1,7 @@ use minidom::Element; use error::Error; +use super::MessagePayload; use ns::CHATSTATES_NS; @@ -13,6 +14,8 @@ pub enum ChatState { Paused, } +impl MessagePayload for ChatState {} + pub fn parse_chatstate(root: &Element) -> Result { for _ in root.children() { return Err(Error::ParseError("Unknown child in chatstate element.")); diff --git a/src/lib.rs b/src/lib.rs index e0798c12..8914a6c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,3 +12,18 @@ pub mod jingle; pub mod ping; pub mod chatstates; pub mod ibb; + +use std::fmt::Debug; +use minidom::Element; + +pub trait MessagePayload: Debug {} + +pub fn parse_message_payload(elem: &Element) -> Option> { + if let Ok(body) = body::parse_body(elem) { + Some(Box::new(body)) + } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) { + Some(Box::new(chatstate)) + } else { + None + } +}