hashes: Switch to Into/TryFrom.
This commit is contained in:
parent
0f58e650b7
commit
1ec3806629
3 changed files with 43 additions and 37 deletions
|
|
@ -4,6 +4,8 @@
|
|||
// 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/.
|
||||
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use minidom::Element;
|
||||
|
||||
use error::Error;
|
||||
|
|
@ -16,42 +18,46 @@ pub struct Hash {
|
|||
pub hash: String,
|
||||
}
|
||||
|
||||
pub fn parse_hash(root: &Element) -> Result<Hash, Error> {
|
||||
if !root.is("hash", ns::HASHES) {
|
||||
return Err(Error::ParseError("This is not a hash element."));
|
||||
impl<'a> TryFrom<&'a Element> for Hash {
|
||||
type Error = Error;
|
||||
|
||||
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 {
|
||||
Element::builder("hash")
|
||||
.ns(ns::HASHES)
|
||||
.attr("algo", hash.algo.clone())
|
||||
.append(hash.hash.clone())
|
||||
.build()
|
||||
impl<'a> Into<Element> for &'a Hash {
|
||||
fn into(self) -> Element {
|
||||
Element::builder("hash")
|
||||
.ns(ns::HASHES)
|
||||
.attr("algo", self.algo.clone())
|
||||
.append(self.hash.clone())
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use minidom::Element;
|
||||
use error::Error;
|
||||
use hashes;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_simple() {
|
||||
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.hash, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=");
|
||||
}
|
||||
|
|
@ -59,7 +65,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_unknown() {
|
||||
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 {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
@ -70,7 +76,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_invalid_child() {
|
||||
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 {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue