diff --git a/src/message.rs b/src/message.rs index 29234321..90bce3db 100644 --- a/src/message.rs +++ b/src/message.rs @@ -23,7 +23,7 @@ use delay::Delay; use attention::Attention; use message_correct::Replace; use eme::ExplicitMessageEncryption; -use stanza_id::StanzaId; +use stanza_id::{StanzaId, OriginId}; use mam::Result_ as MamResult; /// Lists every known payload of a ``. @@ -37,6 +37,7 @@ pub enum MessagePayload { MessageCorrect(Replace), ExplicitMessageEncryption(ExplicitMessageEncryption), StanzaId(StanzaId), + OriginId(OriginId), MamResult(MamResult), Unknown(Element), @@ -73,8 +74,8 @@ impl TryFrom for MessagePayload { ("result", ns::MAM) => MessagePayload::MamResult(MamResult::try_from(elem)?), // XEP-0359 - ("stanza-id", ns::SID) - | ("origin-id", ns::SID) => MessagePayload::StanzaId(StanzaId::try_from(elem)?), + ("stanza-id", ns::SID) => MessagePayload::StanzaId(StanzaId::try_from(elem)?), + ("origin-id", ns::SID) => MessagePayload::OriginId(OriginId::try_from(elem)?), // XEP-0380 ("encryption", ns::EME) => MessagePayload::ExplicitMessageEncryption(ExplicitMessageEncryption::try_from(elem)?), @@ -95,6 +96,7 @@ impl From for Element { MessagePayload::MessageCorrect(replace) => replace.into(), MessagePayload::ExplicitMessageEncryption(eme) => eme.into(), MessagePayload::StanzaId(stanza_id) => stanza_id.into(), + MessagePayload::OriginId(origin_id) => origin_id.into(), MessagePayload::MamResult(result) => result.into(), MessagePayload::Unknown(elem) => elem, diff --git a/src/stanza_id.rs b/src/stanza_id.rs index 72687d9b..43ce9645 100644 --- a/src/stanza_id.rs +++ b/src/stanza_id.rs @@ -14,54 +14,65 @@ use error::Error; use ns; #[derive(Debug, Clone)] -pub enum StanzaId { - StanzaId { - id: String, - by: Jid, - }, - OriginId { - id: String, - }, +pub struct StanzaId { + pub id: String, + pub by: Jid, } impl TryFrom for StanzaId { type Err = Error; fn try_from(elem: Element) -> Result { - let is_stanza_id = elem.is("stanza-id", ns::SID); - if !is_stanza_id && !elem.is("origin-id", ns::SID) { - return Err(Error::ParseError("This is not a stanza-id or origin-id element.")); + if !elem.is("stanza-id", ns::SID) { + return Err(Error::ParseError("This is not a stanza-id element.")); } for _ in elem.children() { - return Err(Error::ParseError("Unknown child in stanza-id or origin-id element.")); + return Err(Error::ParseError("Unknown child in stanza-id element.")); } - let id = get_attr!(elem, "id", required); - Ok(if is_stanza_id { - let by = get_attr!(elem, "by", required); - StanzaId::StanzaId { id, by } - } else { - StanzaId::OriginId { id } + Ok(StanzaId { + id: get_attr!(elem, "id", required), + by: get_attr!(elem, "by", required), }) } } impl From for Element { fn from(stanza_id: StanzaId) -> Element { - match stanza_id { - StanzaId::StanzaId { id, by } => { - Element::builder("stanza-id") - .ns(ns::SID) - .attr("id", id) - .attr("by", String::from(by)) - .build() - }, - StanzaId::OriginId { id } => { - Element::builder("origin-id") - .ns(ns::SID) - .attr("id", id) - .build() - }, + Element::builder("stanza-id") + .ns(ns::SID) + .attr("id", stanza_id.id) + .attr("by", String::from(stanza_id.by)) + .build() + } +} + +#[derive(Debug, Clone)] +pub struct OriginId { + pub id: String, +} + +impl TryFrom for OriginId { + type Err = Error; + + fn try_from(elem: Element) -> Result { + if !elem.is("origin-id", ns::SID) { + return Err(Error::ParseError("This is not an origin-id element.")); } + for _ in elem.children() { + return Err(Error::ParseError("Unknown child in origin-id element.")); + } + Ok(OriginId { + id: get_attr!(elem, "id", required), + }) + } +} + +impl From for Element { + fn from(origin_id: OriginId) -> Element { + Element::builder("origin-id") + .ns(ns::SID) + .attr("id", origin_id.id) + .build() } } @@ -74,20 +85,12 @@ mod tests { fn test_simple() { let elem: Element = "".parse().unwrap(); let stanza_id = StanzaId::try_from(elem).unwrap(); - if let StanzaId::StanzaId { id, by } = stanza_id { - assert_eq!(id, String::from("coucou")); - assert_eq!(by, Jid::from_str("coucou@coucou").unwrap()); - } else { - panic!(); - } + assert_eq!(stanza_id.id, String::from("coucou")); + assert_eq!(stanza_id.by, Jid::from_str("coucou@coucou").unwrap()); let elem: Element = "".parse().unwrap(); - let stanza_id = StanzaId::try_from(elem).unwrap(); - if let StanzaId::OriginId { id } = stanza_id { - assert_eq!(id, String::from("coucou")); - } else { - panic!(); - } + let origin_id = OriginId::try_from(elem).unwrap(); + assert_eq!(origin_id.id, String::from("coucou")); } #[test] @@ -98,7 +101,7 @@ mod tests { Error::ParseError(string) => string, _ => panic!(), }; - assert_eq!(message, "Unknown child in stanza-id or origin-id element."); + assert_eq!(message, "Unknown child in stanza-id element."); } #[test] @@ -126,7 +129,7 @@ mod tests { #[test] fn test_serialise() { let elem: Element = "".parse().unwrap(); - let stanza_id = StanzaId::StanzaId { id: String::from("coucou"), by: Jid::from_str("coucou@coucou").unwrap() }; + let stanza_id = StanzaId { id: String::from("coucou"), by: Jid::from_str("coucou@coucou").unwrap() }; let elem2 = stanza_id.into(); assert_eq!(elem, elem2); }