From 3f57edfc277c81d1d83b7f0447b33163dda95ef2 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 23 Nov 2017 15:52:06 +0000 Subject: [PATCH] hashes: Use the new helpers. --- src/hashes.rs | 49 ++++++++++--------------------------------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/src/hashes.rs b/src/hashes.rs index bcaf94ac..535f99c5 100644 --- a/src/hashes.rs +++ b/src/hashes.rs @@ -12,8 +12,7 @@ use minidom::{Element, IntoAttributeValue}; use error::Error; use ns; - -use base64; +use helpers::Base64; #[allow(non_camel_case_types)] #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -68,47 +67,19 @@ impl IntoAttributeValue for Algo { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Hash { - pub algo: Algo, - pub hash: Vec, -} - -impl TryFrom for Hash { - type Err = Error; - - fn try_from(elem: Element) -> Result { - 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 shouldn’t be empty.")), - text => base64::decode(text)?, - }; - Ok(Hash { - algo: algo, - hash: hash, - }) - } -} - -impl From for Element { - fn from(hash: Hash) -> Element { - Element::builder("hash") - .ns(ns::HASHES) - .attr("algo", hash.algo) - .append(base64::encode(&hash.hash)) - .build() - } -} +generate_element_with_text!( + #[derive(PartialEq)] + Hash, "hash", ns::HASHES, + [ + algo: Algo = "algo" => required + ], + hash: Base64> +); #[cfg(test)] mod tests { use super::*; + use base64; #[test] fn test_simple() {