ibb: Add a parser for the <data/> element.
This commit is contained in:
parent
6ba22a43e5
commit
fb373c2b66
1 changed files with 18 additions and 2 deletions
20
src/ibb.rs
20
src/ibb.rs
|
@ -1,6 +1,7 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
|
use base64;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ impl FromStr for Stanza {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum IBB {
|
pub enum IBB {
|
||||||
Open { block_size: u16, sid: String, stanza: Stanza },
|
Open { block_size: u16, sid: String, stanza: Stanza },
|
||||||
Data(u16, String, Vec<u8>),
|
Data { seq: u16, sid: String, data: Vec<u8> },
|
||||||
Close(String),
|
Close(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +64,18 @@ pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
|
||||||
sid: sid,
|
sid: sid,
|
||||||
stanza: stanza
|
stanza: stanza
|
||||||
})
|
})
|
||||||
|
} else if root.is("data", ns::IBB) {
|
||||||
|
let seq = required_attr(root, "seq", Error::ParseError("Required attribute 'seq' missing in data element."))?;
|
||||||
|
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
|
||||||
|
let data = base64::decode(&root.text())?;
|
||||||
|
for _ in root.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in data element."));
|
||||||
|
}
|
||||||
|
Ok(IBB::Data {
|
||||||
|
seq: seq,
|
||||||
|
sid: sid,
|
||||||
|
data: data
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ParseError("This is not an ibb element."))
|
Err(Error::ParseError("This is not an ibb element."))
|
||||||
}
|
}
|
||||||
|
@ -76,7 +89,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou'/>".parse().unwrap();
|
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap();
|
||||||
|
ibb::parse_ibb(&elem).unwrap();
|
||||||
|
|
||||||
|
let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap();
|
||||||
ibb::parse_ibb(&elem).unwrap();
|
ibb::parse_ibb(&elem).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue