From ab841dc37563ec4ec7f386dc26dd074feb03984f Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 23 Apr 2017 03:21:21 +0100 Subject: [PATCH] receipts: Add a serialise function. --- src/receipts.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/receipts.rs b/src/receipts.rs index 052982ca..8eb07ff9 100644 --- a/src/receipts.rs +++ b/src/receipts.rs @@ -24,11 +24,24 @@ pub fn parse_receipt(root: &Element) -> Result { } } +pub fn serialise(receipt: &Receipt) -> Element { + match *receipt { + Receipt::Request => Element::builder("request") + .ns(ns::RECEIPTS) + .build(), + Receipt::Received(ref id) => Element::builder("received") + .ns(ns::RECEIPTS) + .attr("id", id.clone()) + .build(), + } +} + #[cfg(test)] mod tests { use minidom::Element; //use error::Error; use receipts; + use ns; #[test] fn test_simple() { @@ -41,4 +54,16 @@ mod tests { let elem: Element = "".parse().unwrap(); receipts::parse_receipt(&elem).unwrap(); } + + #[test] + fn test_serialise() { + let receipt = receipts::Receipt::Request; + let elem = receipts::serialise(&receipt); + assert!(elem.is("request", ns::RECEIPTS)); + + let receipt = receipts::Receipt::Received("coucou".to_owned()); + let elem = receipts::serialise(&receipt); + assert!(elem.is("received", ns::RECEIPTS)); + assert_eq!(elem.attr("id"), Some("coucou")); + } }