hashes: Use the new helpers.

This commit is contained in:
Emmanuel Gil Peyrot 2017-11-23 15:52:06 +00:00
parent ad17c877f5
commit 3f57edfc27

View file

@ -12,8 +12,7 @@ use minidom::{Element, IntoAttributeValue};
use error::Error; use error::Error;
use ns; use ns;
use helpers::Base64;
use base64;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -68,47 +67,19 @@ impl IntoAttributeValue for Algo {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] generate_element_with_text!(
pub struct Hash { #[derive(PartialEq)]
pub algo: Algo, Hash, "hash", ns::HASHES,
pub hash: Vec<u8>, [
} algo: Algo = "algo" => required
],
impl TryFrom<Element> for Hash { hash: Base64<Vec<u8>>
type Err = Error; );
fn try_from(elem: 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 = get_attr!(elem, "algo", required);
let hash = match elem.text().as_ref() {
"" => return Err(Error::ParseError("Hash element shouldnt be empty.")),
text => base64::decode(text)?,
};
Ok(Hash {
algo: algo,
hash: hash,
})
}
}
impl From<Hash> for Element {
fn from(hash: Hash) -> Element {
Element::builder("hash")
.ns(ns::HASHES)
.attr("algo", hash.algo)
.append(base64::encode(&hash.hash))
.build()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use base64;
#[test] #[test]
fn test_simple() { fn test_simple() {