stanza_id: Split the stupid enum into two different structs.

This commit is contained in:
Emmanuel Gil Peyrot 2017-07-29 03:51:41 +01:00
parent db35d28c9c
commit a0b1d93ff0
2 changed files with 54 additions and 49 deletions

View file

@ -23,7 +23,7 @@ use delay::Delay;
use attention::Attention; use attention::Attention;
use message_correct::Replace; use message_correct::Replace;
use eme::ExplicitMessageEncryption; use eme::ExplicitMessageEncryption;
use stanza_id::StanzaId; use stanza_id::{StanzaId, OriginId};
use mam::Result_ as MamResult; use mam::Result_ as MamResult;
/// Lists every known payload of a `<message/>`. /// Lists every known payload of a `<message/>`.
@ -37,6 +37,7 @@ pub enum MessagePayload {
MessageCorrect(Replace), MessageCorrect(Replace),
ExplicitMessageEncryption(ExplicitMessageEncryption), ExplicitMessageEncryption(ExplicitMessageEncryption),
StanzaId(StanzaId), StanzaId(StanzaId),
OriginId(OriginId),
MamResult(MamResult), MamResult(MamResult),
Unknown(Element), Unknown(Element),
@ -73,8 +74,8 @@ impl TryFrom<Element> for MessagePayload {
("result", ns::MAM) => MessagePayload::MamResult(MamResult::try_from(elem)?), ("result", ns::MAM) => MessagePayload::MamResult(MamResult::try_from(elem)?),
// XEP-0359 // XEP-0359
("stanza-id", ns::SID) ("stanza-id", ns::SID) => MessagePayload::StanzaId(StanzaId::try_from(elem)?),
| ("origin-id", ns::SID) => MessagePayload::StanzaId(StanzaId::try_from(elem)?), ("origin-id", ns::SID) => MessagePayload::OriginId(OriginId::try_from(elem)?),
// XEP-0380 // XEP-0380
("encryption", ns::EME) => MessagePayload::ExplicitMessageEncryption(ExplicitMessageEncryption::try_from(elem)?), ("encryption", ns::EME) => MessagePayload::ExplicitMessageEncryption(ExplicitMessageEncryption::try_from(elem)?),
@ -95,6 +96,7 @@ impl From<MessagePayload> for Element {
MessagePayload::MessageCorrect(replace) => replace.into(), MessagePayload::MessageCorrect(replace) => replace.into(),
MessagePayload::ExplicitMessageEncryption(eme) => eme.into(), MessagePayload::ExplicitMessageEncryption(eme) => eme.into(),
MessagePayload::StanzaId(stanza_id) => stanza_id.into(), MessagePayload::StanzaId(stanza_id) => stanza_id.into(),
MessagePayload::OriginId(origin_id) => origin_id.into(),
MessagePayload::MamResult(result) => result.into(), MessagePayload::MamResult(result) => result.into(),
MessagePayload::Unknown(elem) => elem, MessagePayload::Unknown(elem) => elem,

View file

@ -14,54 +14,65 @@ use error::Error;
use ns; use ns;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum StanzaId { pub struct StanzaId {
StanzaId { pub id: String,
id: String, pub by: Jid,
by: Jid,
},
OriginId {
id: String,
},
} }
impl TryFrom<Element> for StanzaId { impl TryFrom<Element> for StanzaId {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<StanzaId, Error> { fn try_from(elem: Element) -> Result<StanzaId, Error> {
let is_stanza_id = elem.is("stanza-id", ns::SID); if !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 element."));
return Err(Error::ParseError("This is not a stanza-id or origin-id element."));
} }
for _ in elem.children() { 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(StanzaId {
Ok(if is_stanza_id { id: get_attr!(elem, "id", required),
let by = get_attr!(elem, "by", required); by: get_attr!(elem, "by", required),
StanzaId::StanzaId { id, by }
} else {
StanzaId::OriginId { id }
}) })
} }
} }
impl From<StanzaId> for Element { impl From<StanzaId> for Element {
fn from(stanza_id: StanzaId) -> Element { fn from(stanza_id: StanzaId) -> Element {
match stanza_id { Element::builder("stanza-id")
StanzaId::StanzaId { id, by } => { .ns(ns::SID)
Element::builder("stanza-id") .attr("id", stanza_id.id)
.ns(ns::SID) .attr("by", String::from(stanza_id.by))
.attr("id", id) .build()
.attr("by", String::from(by)) }
.build() }
},
StanzaId::OriginId { id } => { #[derive(Debug, Clone)]
Element::builder("origin-id") pub struct OriginId {
.ns(ns::SID) pub id: String,
.attr("id", id) }
.build()
}, impl TryFrom<Element> for OriginId {
type Err = Error;
fn try_from(elem: Element) -> Result<OriginId, Error> {
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<OriginId> 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() { fn test_simple() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
let stanza_id = StanzaId::try_from(elem).unwrap(); let stanza_id = StanzaId::try_from(elem).unwrap();
if let StanzaId::StanzaId { id, by } = stanza_id { assert_eq!(stanza_id.id, String::from("coucou"));
assert_eq!(id, String::from("coucou")); assert_eq!(stanza_id.by, Jid::from_str("coucou@coucou").unwrap());
assert_eq!(by, Jid::from_str("coucou@coucou").unwrap());
} else {
panic!();
}
let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap(); let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
let stanza_id = StanzaId::try_from(elem).unwrap(); let origin_id = OriginId::try_from(elem).unwrap();
if let StanzaId::OriginId { id } = stanza_id { assert_eq!(origin_id.id, String::from("coucou"));
assert_eq!(id, String::from("coucou"));
} else {
panic!();
}
} }
#[test] #[test]
@ -98,7 +101,7 @@ mod tests {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Unknown child in stanza-id or origin-id element."); assert_eq!(message, "Unknown child in stanza-id element.");
} }
#[test] #[test]
@ -126,7 +129,7 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap(); let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".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(); let elem2 = stanza_id.into();
assert_eq!(elem, elem2); assert_eq!(elem, elem2);
} }