hashes: Switch to Into/TryFrom.
This commit is contained in:
parent
0f58e650b7
commit
1ec3806629
3 changed files with 43 additions and 37 deletions
|
|
@ -4,10 +4,11 @@
|
||||||
// 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 disco::{Feature, Identity, Disco};
|
use disco::{Feature, Identity, Disco};
|
||||||
use data_forms::DataForm;
|
use data_forms::DataForm;
|
||||||
use hashes;
|
use hashes::Hash;
|
||||||
use hashes::{Hash, parse_hash};
|
|
||||||
|
|
||||||
use minidom::Element;
|
use minidom::Element;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
|
@ -31,7 +32,7 @@ pub fn parse_ecaps2(root: &Element) -> Result<ECaps2, Error> {
|
||||||
let mut hashes = vec!();
|
let mut hashes = vec!();
|
||||||
for child in root.children() {
|
for child in root.children() {
|
||||||
if child.is("hash", ns::HASHES) {
|
if child.is("hash", ns::HASHES) {
|
||||||
let hash = parse_hash(child)?;
|
let hash = Hash::try_from(child)?;
|
||||||
hashes.push(hash);
|
hashes.push(hash);
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::ParseError("Unknown child in ecaps2 element."));
|
return Err(Error::ParseError("Unknown child in ecaps2 element."));
|
||||||
|
|
@ -47,7 +48,7 @@ pub fn serialise(ecaps2: &ECaps2) -> Element {
|
||||||
.ns(ns::ECAPS2)
|
.ns(ns::ECAPS2)
|
||||||
.build();
|
.build();
|
||||||
for hash in ecaps2.hashes.clone() {
|
for hash in ecaps2.hashes.clone() {
|
||||||
let hash_elem = hashes::serialise(&hash);
|
let hash_elem = (&hash).into();
|
||||||
c.append_child(hash_elem);
|
c.append_child(hash_elem);
|
||||||
}
|
}
|
||||||
c
|
c
|
||||||
|
|
|
||||||
|
|
@ -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 minidom::Element;
|
use minidom::Element;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
|
@ -16,42 +18,46 @@ pub struct Hash {
|
||||||
pub hash: String,
|
pub hash: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_hash(root: &Element) -> Result<Hash, Error> {
|
impl<'a> TryFrom<&'a Element> for Hash {
|
||||||
if !root.is("hash", ns::HASHES) {
|
type Error = Error;
|
||||||
return Err(Error::ParseError("This is not a hash element."));
|
|
||||||
|
fn try_from(elem: &'a Element) -> Result<Hash, Error> {
|
||||||
|
if !elem.is("hash", ns::HASHES) {
|
||||||
|
return Err(Error::ParseError("This is not a hash element."));
|
||||||
|
}
|
||||||
|
for _ in elem.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in hash element."));
|
||||||
|
}
|
||||||
|
let algo = elem.attr("algo").ok_or(Error::ParseError("Mandatory argument 'algo' not present in hash element."))?.to_owned();
|
||||||
|
let hash = match elem.text().as_ref() {
|
||||||
|
"" => return Err(Error::ParseError("Hash element shouldn’t be empty.")),
|
||||||
|
text => text.to_owned(),
|
||||||
|
};
|
||||||
|
Ok(Hash {
|
||||||
|
algo: algo,
|
||||||
|
hash: hash,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
for _ in root.children() {
|
|
||||||
return Err(Error::ParseError("Unknown child in hash element."));
|
|
||||||
}
|
|
||||||
let algo = root.attr("algo").ok_or(Error::ParseError("Mandatory argument 'algo' not present in hash element."))?.to_owned();
|
|
||||||
let hash = match root.text().as_ref() {
|
|
||||||
"" => return Err(Error::ParseError("Hash element shouldn’t be empty.")),
|
|
||||||
text => text.to_owned(),
|
|
||||||
};
|
|
||||||
Ok(Hash {
|
|
||||||
algo: algo,
|
|
||||||
hash: hash,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise(hash: &Hash) -> Element {
|
impl<'a> Into<Element> for &'a Hash {
|
||||||
Element::builder("hash")
|
fn into(self) -> Element {
|
||||||
.ns(ns::HASHES)
|
Element::builder("hash")
|
||||||
.attr("algo", hash.algo.clone())
|
.ns(ns::HASHES)
|
||||||
.append(hash.hash.clone())
|
.attr("algo", self.algo.clone())
|
||||||
.build()
|
.append(self.hash.clone())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use hashes;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=</hash>".parse().unwrap();
|
||||||
let hash = hashes::parse_hash(&elem).unwrap();
|
let hash = Hash::try_from(&elem).unwrap();
|
||||||
assert_eq!(hash.algo, "sha-256");
|
assert_eq!(hash.algo, "sha-256");
|
||||||
assert_eq!(hash.hash, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=");
|
assert_eq!(hash.hash, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=");
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +65,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unknown() {
|
fn test_unknown() {
|
||||||
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
|
||||||
let error = hashes::parse_hash(&elem).unwrap_err();
|
let error = Hash::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
@ -70,7 +76,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_child() {
|
fn test_invalid_child() {
|
||||||
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
|
let elem: Element = "<hash xmlns='urn:xmpp:hashes:2'><coucou/></hash>".parse().unwrap();
|
||||||
let error = hashes::parse_hash(&elem).unwrap_err();
|
let error = Hash::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,7 @@
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use hashes;
|
use hashes::Hash;
|
||||||
use hashes::{Hash, parse_hash};
|
|
||||||
|
|
||||||
use minidom::{Element, IntoElements, ElementEmitter};
|
use minidom::{Element, IntoElements, ElementEmitter};
|
||||||
|
|
||||||
|
|
@ -32,7 +31,7 @@ impl IntoElements for Range {
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
for hash in self.hashes {
|
for hash in self.hashes {
|
||||||
elem.append_child(hashes::serialise(&hash));
|
elem.append_child((&hash).into());
|
||||||
}
|
}
|
||||||
emitter.append_child(elem);
|
emitter.append_child(elem);
|
||||||
}
|
}
|
||||||
|
|
@ -149,7 +148,7 @@ impl<'a> TryFrom<&'a Element> for Description {
|
||||||
if !hash_element.is("hash", ns::HASHES) {
|
if !hash_element.is("hash", ns::HASHES) {
|
||||||
return Err(Error::ParseError("Unknown element in JingleFT range."));
|
return Err(Error::ParseError("Unknown element in JingleFT range."));
|
||||||
}
|
}
|
||||||
range_hashes.push(parse_hash(hash_element)?);
|
range_hashes.push(Hash::try_from(hash_element)?);
|
||||||
}
|
}
|
||||||
range = Some(Range {
|
range = Some(Range {
|
||||||
offset: offset,
|
offset: offset,
|
||||||
|
|
@ -157,7 +156,7 @@ impl<'a> TryFrom<&'a Element> for Description {
|
||||||
hashes: range_hashes,
|
hashes: range_hashes,
|
||||||
});
|
});
|
||||||
} else if file_payload.is("hash", ns::HASHES) {
|
} else if file_payload.is("hash", ns::HASHES) {
|
||||||
hashes.push(parse_hash(file_payload)?);
|
hashes.push(Hash::try_from(file_payload)?);
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::ParseError("Unknown element in JingleFT file."));
|
return Err(Error::ParseError("Unknown element in JingleFT file."));
|
||||||
}
|
}
|
||||||
|
|
@ -220,7 +219,7 @@ impl<'a> Into<Element> for &'a File {
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
for hash in self.hashes.clone() {
|
for hash in self.hashes.clone() {
|
||||||
root.append_child(hashes::serialise(&hash));
|
root.append_child((&hash).into());
|
||||||
}
|
}
|
||||||
root
|
root
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue