mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
jingle_ft: Switch to Into/TryFrom.
This commit is contained in:
parent
4f11a067d8
commit
a3a90e4eda
1 changed files with 140 additions and 130 deletions
270
src/jingle_ft.rs
270
src/jingle_ft.rs
|
@ -4,6 +4,8 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use hashes;
|
use hashes;
|
||||||
use hashes::{Hash, parse_hash};
|
use hashes::{Hash, parse_hash};
|
||||||
|
|
||||||
|
@ -85,150 +87,158 @@ impl IntoElements for Received {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_jingle_ft(root: &Element) -> Result<Description, Error> {
|
impl<'a> TryFrom<&'a Element> for Description {
|
||||||
if !root.is("description", ns::JINGLE_FT) {
|
type Error = Error;
|
||||||
return Err(Error::ParseError("This is not a JingleFT description element."));
|
|
||||||
}
|
|
||||||
if root.children().collect::<Vec<_>>().len() != 1 {
|
|
||||||
return Err(Error::ParseError("JingleFT description element must have exactly one child."));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut date = None;
|
fn try_from(elem: &'a Element) -> Result<Description, Error> {
|
||||||
let mut media_type = None;
|
if !elem.is("description", ns::JINGLE_FT) {
|
||||||
let mut name = None;
|
return Err(Error::ParseError("This is not a JingleFT description element."));
|
||||||
let mut desc = None;
|
|
||||||
let mut size = None;
|
|
||||||
let mut range = None;
|
|
||||||
let mut hashes = vec!();
|
|
||||||
for description_payload in root.children() {
|
|
||||||
if !description_payload.is("file", ns::JINGLE_FT) {
|
|
||||||
return Err(Error::ParseError("Unknown element in JingleFT description."));
|
|
||||||
}
|
}
|
||||||
for file_payload in description_payload.children() {
|
if elem.children().collect::<Vec<_>>().len() != 1 {
|
||||||
if file_payload.is("date", ns::JINGLE_FT) {
|
return Err(Error::ParseError("JingleFT description element must have exactly one child."));
|
||||||
if date.is_some() {
|
}
|
||||||
return Err(Error::ParseError("File must not have more than one date."));
|
|
||||||
}
|
let mut date = None;
|
||||||
date = Some(file_payload.text());
|
let mut media_type = None;
|
||||||
} else if file_payload.is("media-type", ns::JINGLE_FT) {
|
let mut name = None;
|
||||||
if media_type.is_some() {
|
let mut desc = None;
|
||||||
return Err(Error::ParseError("File must not have more than one media-type."));
|
let mut size = None;
|
||||||
}
|
let mut range = None;
|
||||||
media_type = Some(file_payload.text());
|
let mut hashes = vec!();
|
||||||
} else if file_payload.is("name", ns::JINGLE_FT) {
|
for description_payload in elem.children() {
|
||||||
if name.is_some() {
|
if !description_payload.is("file", ns::JINGLE_FT) {
|
||||||
return Err(Error::ParseError("File must not have more than one name."));
|
return Err(Error::ParseError("Unknown element in JingleFT description."));
|
||||||
}
|
}
|
||||||
name = Some(file_payload.text());
|
for file_payload in description_payload.children() {
|
||||||
} else if file_payload.is("desc", ns::JINGLE_FT) {
|
if file_payload.is("date", ns::JINGLE_FT) {
|
||||||
if desc.is_some() {
|
if date.is_some() {
|
||||||
return Err(Error::ParseError("File must not have more than one desc."));
|
return Err(Error::ParseError("File must not have more than one date."));
|
||||||
}
|
|
||||||
desc = Some(file_payload.text());
|
|
||||||
} else if file_payload.is("size", ns::JINGLE_FT) {
|
|
||||||
if size.is_some() {
|
|
||||||
return Err(Error::ParseError("File must not have more than one size."));
|
|
||||||
}
|
|
||||||
size = Some(file_payload.text().parse()?);
|
|
||||||
} else if file_payload.is("range", ns::JINGLE_FT) {
|
|
||||||
if range.is_some() {
|
|
||||||
return Err(Error::ParseError("File must not have more than one range."));
|
|
||||||
}
|
|
||||||
let offset = file_payload.attr("offset").unwrap_or("0").parse()?;
|
|
||||||
let length = match file_payload.attr("length") {
|
|
||||||
Some(length) => Some(length.parse()?),
|
|
||||||
None => None,
|
|
||||||
};
|
|
||||||
let mut range_hashes = vec!();
|
|
||||||
for hash_element in file_payload.children() {
|
|
||||||
if !hash_element.is("hash", ns::HASHES) {
|
|
||||||
return Err(Error::ParseError("Unknown element in JingleFT range."));
|
|
||||||
}
|
}
|
||||||
range_hashes.push(parse_hash(hash_element)?);
|
date = Some(file_payload.text());
|
||||||
|
} else if file_payload.is("media-type", ns::JINGLE_FT) {
|
||||||
|
if media_type.is_some() {
|
||||||
|
return Err(Error::ParseError("File must not have more than one media-type."));
|
||||||
|
}
|
||||||
|
media_type = Some(file_payload.text());
|
||||||
|
} else if file_payload.is("name", ns::JINGLE_FT) {
|
||||||
|
if name.is_some() {
|
||||||
|
return Err(Error::ParseError("File must not have more than one name."));
|
||||||
|
}
|
||||||
|
name = Some(file_payload.text());
|
||||||
|
} else if file_payload.is("desc", ns::JINGLE_FT) {
|
||||||
|
if desc.is_some() {
|
||||||
|
return Err(Error::ParseError("File must not have more than one desc."));
|
||||||
|
}
|
||||||
|
desc = Some(file_payload.text());
|
||||||
|
} else if file_payload.is("size", ns::JINGLE_FT) {
|
||||||
|
if size.is_some() {
|
||||||
|
return Err(Error::ParseError("File must not have more than one size."));
|
||||||
|
}
|
||||||
|
size = Some(file_payload.text().parse()?);
|
||||||
|
} else if file_payload.is("range", ns::JINGLE_FT) {
|
||||||
|
if range.is_some() {
|
||||||
|
return Err(Error::ParseError("File must not have more than one range."));
|
||||||
|
}
|
||||||
|
let offset = file_payload.attr("offset").unwrap_or("0").parse()?;
|
||||||
|
let length = match file_payload.attr("length") {
|
||||||
|
Some(length) => Some(length.parse()?),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
let mut range_hashes = vec!();
|
||||||
|
for hash_element in file_payload.children() {
|
||||||
|
if !hash_element.is("hash", ns::HASHES) {
|
||||||
|
return Err(Error::ParseError("Unknown element in JingleFT range."));
|
||||||
|
}
|
||||||
|
range_hashes.push(parse_hash(hash_element)?);
|
||||||
|
}
|
||||||
|
range = Some(Range {
|
||||||
|
offset: offset,
|
||||||
|
length: length,
|
||||||
|
hashes: range_hashes,
|
||||||
|
});
|
||||||
|
} else if file_payload.is("hash", ns::HASHES) {
|
||||||
|
hashes.push(parse_hash(file_payload)?);
|
||||||
|
} else {
|
||||||
|
return Err(Error::ParseError("Unknown element in JingleFT file."));
|
||||||
}
|
}
|
||||||
range = Some(Range {
|
|
||||||
offset: offset,
|
|
||||||
length: length,
|
|
||||||
hashes: range_hashes,
|
|
||||||
});
|
|
||||||
} else if file_payload.is("hash", ns::HASHES) {
|
|
||||||
hashes.push(parse_hash(file_payload)?);
|
|
||||||
} else {
|
|
||||||
return Err(Error::ParseError("Unknown element in JingleFT file."));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Description {
|
Ok(Description {
|
||||||
file: File {
|
file: File {
|
||||||
date: date,
|
date: date,
|
||||||
media_type: media_type,
|
media_type: media_type,
|
||||||
name: name,
|
name: name,
|
||||||
desc: desc,
|
desc: desc,
|
||||||
size: size,
|
size: size,
|
||||||
range: range,
|
range: range,
|
||||||
hashes: hashes,
|
hashes: hashes,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise_file(file: &File) -> Element {
|
impl<'a> Into<Element> for &'a File {
|
||||||
let mut root = Element::builder("file")
|
fn into(self) -> Element {
|
||||||
.ns(ns::JINGLE_FT)
|
let mut root = Element::builder("file")
|
||||||
.build();
|
.ns(ns::JINGLE_FT)
|
||||||
if let Some(ref date) = file.date {
|
.build();
|
||||||
root.append_child(Element::builder("date")
|
if let Some(ref date) = self.date {
|
||||||
.ns(ns::JINGLE_FT)
|
root.append_child(Element::builder("date")
|
||||||
.append(date.clone())
|
.ns(ns::JINGLE_FT)
|
||||||
.build());
|
.append(date.clone())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
if let Some(ref media_type) = self.media_type {
|
||||||
|
root.append_child(Element::builder("media-type")
|
||||||
|
.ns(ns::JINGLE_FT)
|
||||||
|
.append(media_type.clone())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
if let Some(ref name) = self.name {
|
||||||
|
root.append_child(Element::builder("name")
|
||||||
|
.ns(ns::JINGLE_FT)
|
||||||
|
.append(name.clone())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
if let Some(ref desc) = self.desc {
|
||||||
|
root.append_child(Element::builder("desc")
|
||||||
|
.ns(ns::JINGLE_FT)
|
||||||
|
.append(desc.clone())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
if let Some(ref size) = self.size {
|
||||||
|
root.append_child(Element::builder("size")
|
||||||
|
.ns(ns::JINGLE_FT)
|
||||||
|
.append(format!("{}", size))
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
if let Some(ref range) = self.range {
|
||||||
|
root.append_child(Element::builder("range")
|
||||||
|
.ns(ns::JINGLE_FT)
|
||||||
|
.append(range.clone())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
for hash in self.hashes.clone() {
|
||||||
|
root.append_child(hashes::serialise(&hash));
|
||||||
|
}
|
||||||
|
root
|
||||||
}
|
}
|
||||||
if let Some(ref media_type) = file.media_type {
|
|
||||||
root.append_child(Element::builder("media-type")
|
|
||||||
.ns(ns::JINGLE_FT)
|
|
||||||
.append(media_type.clone())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
if let Some(ref name) = file.name {
|
|
||||||
root.append_child(Element::builder("name")
|
|
||||||
.ns(ns::JINGLE_FT)
|
|
||||||
.append(name.clone())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
if let Some(ref desc) = file.desc {
|
|
||||||
root.append_child(Element::builder("desc")
|
|
||||||
.ns(ns::JINGLE_FT)
|
|
||||||
.append(desc.clone())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
if let Some(ref size) = file.size {
|
|
||||||
root.append_child(Element::builder("size")
|
|
||||||
.ns(ns::JINGLE_FT)
|
|
||||||
.append(format!("{}", size))
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
if let Some(ref range) = file.range {
|
|
||||||
root.append_child(Element::builder("range")
|
|
||||||
.ns(ns::JINGLE_FT)
|
|
||||||
.append(range.clone())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
for hash in file.hashes.clone() {
|
|
||||||
root.append_child(hashes::serialise(&hash));
|
|
||||||
}
|
|
||||||
root
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise(desc: &Description) -> Element {
|
impl<'a> Into<Element> for &'a Description {
|
||||||
Element::builder("description")
|
fn into(self) -> Element {
|
||||||
.ns(ns::JINGLE_FT)
|
let file: Element = (&self.file).into();
|
||||||
.append(serialise_file(&desc.file))
|
Element::builder("description")
|
||||||
.build()
|
.ns(ns::JINGLE_FT)
|
||||||
|
.append(file)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use jingle_ft;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_description() {
|
fn test_description() {
|
||||||
|
@ -245,7 +255,7 @@ mod tests {
|
||||||
</description>
|
</description>
|
||||||
"#.parse().unwrap();
|
"#.parse().unwrap();
|
||||||
|
|
||||||
let desc = jingle_ft::parse_jingle_ft(&elem).unwrap();
|
let desc = Description::try_from(&elem).unwrap();
|
||||||
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.desc, None);
|
assert_eq!(desc.file.desc, None);
|
||||||
|
@ -267,7 +277,7 @@ mod tests {
|
||||||
</description>
|
</description>
|
||||||
"#.parse().unwrap();
|
"#.parse().unwrap();
|
||||||
|
|
||||||
let desc = jingle_ft::parse_jingle_ft(&elem).unwrap();
|
let desc = Description::try_from(&elem).unwrap();
|
||||||
assert_eq!(desc.file.media_type, None);
|
assert_eq!(desc.file.media_type, None);
|
||||||
assert_eq!(desc.file.name, None);
|
assert_eq!(desc.file.name, None);
|
||||||
assert_eq!(desc.file.desc, None);
|
assert_eq!(desc.file.desc, None);
|
||||||
|
|
Loading…
Reference in a new issue