diff --git a/src/lib.rs b/src/lib.rs index b311682..8efa52a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { 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)) diff --git a/src/message_correct.rs b/src/message_correct.rs index 789febb..09596ec 100644 --- a/src/message_correct.rs +++ b/src/message_correct.rs @@ -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 { +pub fn parse_replace(root: &Element) -> Result { 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 = "".parse().unwrap(); - message_correct::parse_message_correct(&elem).unwrap(); + let elem: Element = "".parse().unwrap(); + message_correct::parse_replace(&elem).unwrap(); } #[test] fn test_invalid_child() { let elem: Element = "".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 = "".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 = "".parse().unwrap(); + let replace = message_correct::Replace { id: String::from("coucou") }; + let elem2 = message_correct::serialise(&replace); + assert_eq!(elem, elem2); + } }