receipts.rs

 1use minidom::Element;
 2
 3use error::Error;
 4
 5use ns;
 6
 7#[derive(Debug, Clone)]
 8pub enum Receipt {
 9    Request,
10    Received(String),
11}
12
13pub fn parse_receipt(root: &Element) -> Result<Receipt, Error> {
14    for _ in root.children() {
15        return Err(Error::ParseError("Unknown child in receipt element."));
16    }
17    if root.is("request", ns::RECEIPTS) {
18        Ok(Receipt::Request)
19    } else if root.is("received", ns::RECEIPTS) {
20        let id = root.attr("id").unwrap_or("").to_owned();
21        Ok(Receipt::Received(id))
22    } else {
23        Err(Error::ParseError("This is not a receipt element."))
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use minidom::Element;
30    //use error::Error;
31    use receipts;
32
33    #[test]
34    fn test_simple() {
35        let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
36        receipts::parse_receipt(&elem).unwrap();
37
38        let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
39        receipts::parse_receipt(&elem).unwrap();
40
41        let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap();
42        receipts::parse_receipt(&elem).unwrap();
43    }
44}