jingle_ft: Implement IntoElements on Range, and change size to be an u64.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-24 19:52:41 +01:00
parent 5abf820fad
commit 049ef23595

View file

@ -1,8 +1,10 @@
extern crate minidom; extern crate minidom;
use hashes;
use hashes::{Hash, parse_hash}; use hashes::{Hash, parse_hash};
use minidom::Element; use minidom::{Element, IntoElements};
use minidom::convert::ElementEmitter;
use error::Error; use error::Error;
use ns; use ns;
@ -14,12 +16,29 @@ pub struct Range {
pub hashes: Vec<Hash>, pub hashes: Vec<Hash>,
} }
impl IntoElements for Range {
fn into_elements(self, emitter: &mut ElementEmitter) {
let mut elem = Element::builder("range")
.ns(ns::JINGLE_FT)
.attr("offset", format!("{}", self.offset))
.attr("length", match self.length {
Some(length) => Some(format!("{}", length)),
None => None
})
.build();
for hash in self.hashes {
elem.append_child(hashes::serialise(&hash));
}
emitter.append_child(elem);
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct File { pub struct File {
pub date: Option<String>, pub date: Option<String>,
pub media_type: Option<String>, pub media_type: Option<String>,
pub name: Option<String>, pub name: Option<String>,
pub size: Option<String>, pub size: Option<u64>,
pub range: Option<Range>, pub range: Option<Range>,
pub hashes: Vec<Hash>, pub hashes: Vec<Hash>,
} }
@ -79,7 +98,7 @@ pub fn parse_jingle_ft(root: &Element) -> Result<Description, Error> {
if size.is_some() { if size.is_some() {
return Err(Error::ParseError("File must not have more than one size.")); return Err(Error::ParseError("File must not have more than one size."));
} }
size = Some(file_payload.text()); size = Some(file_payload.text().parse()?);
} else if file_payload.is("range", ns::JINGLE_FT) { } else if file_payload.is("range", ns::JINGLE_FT) {
if range.is_some() { if range.is_some() {
return Err(Error::ParseError("File must not have more than one range.")); return Err(Error::ParseError("File must not have more than one range."));
@ -193,7 +212,7 @@ mod tests {
assert_eq!(desc.file.media_type, Some(String::from("text/plain"))); assert_eq!(desc.file.media_type, Some(String::from("text/plain")));
assert_eq!(desc.file.name, Some(String::from("test.txt"))); assert_eq!(desc.file.name, Some(String::from("test.txt")));
assert_eq!(desc.file.date, Some(String::from("2015-07-26T21:46:00"))); assert_eq!(desc.file.date, Some(String::from("2015-07-26T21:46:00")));
assert_eq!(desc.file.size, Some(String::from("6144"))); assert_eq!(desc.file.size, Some(6144u64));
assert_eq!(desc.file.range, None); assert_eq!(desc.file.range, None);
assert_eq!(desc.file.hashes[0].algo, "sha-1"); assert_eq!(desc.file.hashes[0].algo, "sha-1");
assert_eq!(desc.file.hashes[0].hash, "w0mcJylzCn+AfvuGdqkty2+KP48="); assert_eq!(desc.file.hashes[0].hash, "w0mcJylzCn+AfvuGdqkty2+KP48=");