receipts: Split the stupid enum into two different structs.
This commit is contained in:
parent
a0b1d93ff0
commit
37d1ae64ad
2 changed files with 65 additions and 41 deletions
|
@ -18,7 +18,7 @@ use ns;
|
||||||
|
|
||||||
use stanza_error::StanzaError;
|
use stanza_error::StanzaError;
|
||||||
use chatstates::ChatState;
|
use chatstates::ChatState;
|
||||||
use receipts::Receipt;
|
use receipts::{Request as ReceiptRequest, Received as ReceiptReceived};
|
||||||
use delay::Delay;
|
use delay::Delay;
|
||||||
use attention::Attention;
|
use attention::Attention;
|
||||||
use message_correct::Replace;
|
use message_correct::Replace;
|
||||||
|
@ -31,7 +31,8 @@ use mam::Result_ as MamResult;
|
||||||
pub enum MessagePayload {
|
pub enum MessagePayload {
|
||||||
StanzaError(StanzaError),
|
StanzaError(StanzaError),
|
||||||
ChatState(ChatState),
|
ChatState(ChatState),
|
||||||
Receipt(Receipt),
|
ReceiptRequest(ReceiptRequest),
|
||||||
|
ReceiptReceived(ReceiptReceived),
|
||||||
Delay(Delay),
|
Delay(Delay),
|
||||||
Attention(Attention),
|
Attention(Attention),
|
||||||
MessageCorrect(Replace),
|
MessageCorrect(Replace),
|
||||||
|
@ -58,8 +59,8 @@ impl TryFrom<Element> for MessagePayload {
|
||||||
| ("gone", ns::CHATSTATES) => MessagePayload::ChatState(ChatState::try_from(elem)?),
|
| ("gone", ns::CHATSTATES) => MessagePayload::ChatState(ChatState::try_from(elem)?),
|
||||||
|
|
||||||
// XEP-0184
|
// XEP-0184
|
||||||
("request", ns::RECEIPTS)
|
("request", ns::RECEIPTS) => MessagePayload::ReceiptRequest(ReceiptRequest::try_from(elem)?),
|
||||||
| ("received", ns::RECEIPTS) => MessagePayload::Receipt(Receipt::try_from(elem)?),
|
("received", ns::RECEIPTS) => MessagePayload::ReceiptReceived(ReceiptReceived::try_from(elem)?),
|
||||||
|
|
||||||
// XEP-0203
|
// XEP-0203
|
||||||
("delay", ns::DELAY) => MessagePayload::Delay(Delay::try_from(elem)?),
|
("delay", ns::DELAY) => MessagePayload::Delay(Delay::try_from(elem)?),
|
||||||
|
@ -91,7 +92,8 @@ impl From<MessagePayload> for Element {
|
||||||
MessagePayload::StanzaError(stanza_error) => stanza_error.into(),
|
MessagePayload::StanzaError(stanza_error) => stanza_error.into(),
|
||||||
MessagePayload::Attention(attention) => attention.into(),
|
MessagePayload::Attention(attention) => attention.into(),
|
||||||
MessagePayload::ChatState(chatstate) => chatstate.into(),
|
MessagePayload::ChatState(chatstate) => chatstate.into(),
|
||||||
MessagePayload::Receipt(receipt) => receipt.into(),
|
MessagePayload::ReceiptRequest(request) => request.into(),
|
||||||
|
MessagePayload::ReceiptReceived(received) => received.into(),
|
||||||
MessagePayload::Delay(delay) => delay.into(),
|
MessagePayload::Delay(delay) => delay.into(),
|
||||||
MessagePayload::MessageCorrect(replace) => replace.into(),
|
MessagePayload::MessageCorrect(replace) => replace.into(),
|
||||||
MessagePayload::ExplicitMessageEncryption(eme) => eme.into(),
|
MessagePayload::ExplicitMessageEncryption(eme) => eme.into(),
|
||||||
|
|
|
@ -13,46 +13,65 @@ use error::Error;
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Receipt {
|
pub struct Request;
|
||||||
Request,
|
|
||||||
Received(Option<String>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Element> for Receipt {
|
impl TryFrom<Element> for Request {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn try_from(elem: Element) -> Result<Receipt, Error> {
|
fn try_from(elem: Element) -> Result<Request, Error> {
|
||||||
|
if !elem.is("request", ns::RECEIPTS) {
|
||||||
|
return Err(Error::ParseError("This is not a request element."));
|
||||||
|
}
|
||||||
for _ in elem.children() {
|
for _ in elem.children() {
|
||||||
return Err(Error::ParseError("Unknown child in receipt element."));
|
return Err(Error::ParseError("Unknown child in request element."));
|
||||||
}
|
}
|
||||||
if elem.is("request", ns::RECEIPTS) {
|
for _ in elem.attrs() {
|
||||||
for _ in elem.attrs() {
|
return Err(Error::ParseError("Unknown attribute in request element."));
|
||||||
return Err(Error::ParseError("Unknown attribute in request element."));
|
|
||||||
}
|
|
||||||
Ok(Receipt::Request)
|
|
||||||
} else if elem.is("received", ns::RECEIPTS) {
|
|
||||||
for (attr, _) in elem.attrs() {
|
|
||||||
if attr != "id" {
|
|
||||||
return Err(Error::ParseError("Unknown attribute in received element."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let id = get_attr!(elem, "id", optional);
|
|
||||||
Ok(Receipt::Received(id))
|
|
||||||
} else {
|
|
||||||
Err(Error::ParseError("This is not a receipt element."))
|
|
||||||
}
|
}
|
||||||
|
Ok(Request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Receipt> for Element {
|
impl From<Request> for Element {
|
||||||
fn from(receipt: Receipt) -> Element {
|
fn from(_: Request) -> Element {
|
||||||
match receipt {
|
Element::builder("request")
|
||||||
Receipt::Request => Element::builder("request")
|
.ns(ns::RECEIPTS)
|
||||||
.ns(ns::RECEIPTS),
|
.build()
|
||||||
Receipt::Received(id) => Element::builder("received")
|
}
|
||||||
.ns(ns::RECEIPTS)
|
}
|
||||||
.attr("id", id),
|
|
||||||
}.build()
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Received {
|
||||||
|
pub id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<Element> for Received {
|
||||||
|
type Err = Error;
|
||||||
|
|
||||||
|
fn try_from(elem: Element) -> Result<Received, Error> {
|
||||||
|
if !elem.is("received", ns::RECEIPTS) {
|
||||||
|
return Err(Error::ParseError("This is not a received element."));
|
||||||
|
}
|
||||||
|
for _ in elem.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in received element."));
|
||||||
|
}
|
||||||
|
for (attr, _) in elem.attrs() {
|
||||||
|
if attr != "id" {
|
||||||
|
return Err(Error::ParseError("Unknown attribute in received element."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Received {
|
||||||
|
id: get_attr!(elem, "id", optional),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Received> for Element {
|
||||||
|
fn from(received: Received) -> Element {
|
||||||
|
Element::builder("received")
|
||||||
|
.ns(ns::RECEIPTS)
|
||||||
|
.attr("id", received.id)
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,22 +82,25 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
|
let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
|
||||||
Receipt::try_from(elem).unwrap();
|
Request::try_from(elem).unwrap();
|
||||||
|
|
||||||
let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
|
let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
|
||||||
Receipt::try_from(elem).unwrap();
|
Received::try_from(elem).unwrap();
|
||||||
|
|
||||||
let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap();
|
let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap();
|
||||||
Receipt::try_from(elem).unwrap();
|
Received::try_from(elem).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise() {
|
fn test_serialise() {
|
||||||
let receipt = Receipt::Request;
|
let receipt = Request;
|
||||||
let elem: Element = receipt.into();
|
let elem: Element = receipt.into();
|
||||||
assert!(elem.is("request", ns::RECEIPTS));
|
assert!(elem.is("request", ns::RECEIPTS));
|
||||||
|
assert_eq!(elem.attrs().count(), 0);
|
||||||
|
|
||||||
let receipt = Receipt::Received(Some(String::from("coucou")));
|
let receipt = Received {
|
||||||
|
id: Some(String::from("coucou")),
|
||||||
|
};
|
||||||
let elem: Element = receipt.into();
|
let elem: Element = receipt.into();
|
||||||
assert!(elem.is("received", ns::RECEIPTS));
|
assert!(elem.is("received", ns::RECEIPTS));
|
||||||
assert_eq!(elem.attr("id"), Some("coucou"));
|
assert_eq!(elem.attr("id"), Some("coucou"));
|
||||||
|
|
Loading…
Reference in a new issue