mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
message_correct: Add a serialise function, and simplify the representation.
This commit is contained in:
parent
4128c4b9ed
commit
7e964b167a
2 changed files with 39 additions and 10 deletions
|
@ -75,7 +75,7 @@ pub enum MessagePayload {
|
|||
Receipt(receipts::Receipt),
|
||||
Delay(delay::Delay),
|
||||
Attention(attention::Attention),
|
||||
MessageCorrect(message_correct::MessageCorrect),
|
||||
MessageCorrect(message_correct::Replace),
|
||||
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
|
|||
Some(MessagePayload::Delay(delay))
|
||||
} else if let Ok(attention) = attention::parse_attention(elem) {
|
||||
Some(MessagePayload::Attention(attention))
|
||||
} else if let Ok(replace) = message_correct::parse_message_correct(elem) {
|
||||
} else if let Ok(replace) = message_correct::parse_replace(elem) {
|
||||
Some(MessagePayload::MessageCorrect(replace))
|
||||
} else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) {
|
||||
Some(MessagePayload::ExplicitMessageEncryption(eme))
|
||||
|
|
|
@ -5,19 +5,29 @@ use error::Error;
|
|||
use ns;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum MessageCorrect {
|
||||
Replace(String),
|
||||
pub struct Replace {
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
pub fn parse_message_correct(root: &Element) -> Result<MessageCorrect, Error> {
|
||||
pub fn parse_replace(root: &Element) -> Result<Replace, Error> {
|
||||
if !root.is("replace", ns::MESSAGE_CORRECT) {
|
||||
return Err(Error::ParseError("This is not a replace element."));
|
||||
}
|
||||
for _ in root.children() {
|
||||
return Err(Error::ParseError("Unknown child in replace element."));
|
||||
}
|
||||
let id = root.attr("id").unwrap_or("").to_owned();
|
||||
Ok(MessageCorrect::Replace(id))
|
||||
let id = match root.attr("id") {
|
||||
Some(id) => id.to_owned(),
|
||||
None => return Err(Error::ParseError("No 'id' attribute present in replace.")),
|
||||
};
|
||||
Ok(Replace { id: id })
|
||||
}
|
||||
|
||||
pub fn serialise(replace: &Replace) -> Element {
|
||||
Element::builder("replace")
|
||||
.ns(ns::MESSAGE_CORRECT)
|
||||
.attr("id", replace.id.clone())
|
||||
.build()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -28,18 +38,37 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_simple() {
|
||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
||||
message_correct::parse_message_correct(&elem).unwrap();
|
||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
|
||||
message_correct::parse_replace(&elem).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_child() {
|
||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().unwrap();
|
||||
let error = message_correct::parse_message_correct(&elem).unwrap_err();
|
||||
let error = message_correct::parse_replace(&elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Unknown child in replace element.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_id() {
|
||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
||||
let error = message_correct::parse_replace(&elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "No 'id' attribute present in replace.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialise() {
|
||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
|
||||
let replace = message_correct::Replace { id: String::from("coucou") };
|
||||
let elem2 = message_correct::serialise(&replace);
|
||||
assert_eq!(elem, elem2);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue