From d15d50d8a27e66958aca933ff97e1311f95bf52e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 30 Dec 2015 11:37:17 +0100 Subject: [PATCH] Refactor: Header helper code can use generic function --- src/module/helpers/header/mod.rs | 46 ++++++++++---------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/src/module/helpers/header/mod.rs b/src/module/helpers/header/mod.rs index 548dc167..ffbbd6c0 100644 --- a/src/module/helpers/header/mod.rs +++ b/src/module/helpers/header/mod.rs @@ -19,37 +19,7 @@ pub mod data { * Does no spec verification. */ pub fn get_url_from_header(header: &FHD) -> Option { - match header { - &FHD::Map{keys: ref ks} => { - let mut keys : Vec = ks.clone(); - keys.iter().find(|k| { - match k.deref() { - &FHD::Key{name: ref n, value: ref v} => n == "URL", - _ => false - } - }).and_then(|urlkey| { - match urlkey.deref().clone() { - FHD::Key{name: _, value: ref v} => { - match v.deref().clone() { - FHD::Text(s) => Some(s), - _ => { - warn!("Malformed Header Data: Expected Text, found non-Text"); - None - }, - } - } - _ => { - warn!("Malformed Header Data: Expected Text, found non-Text"); - None - }, - } - }) - }, - _ => { - warn!("Malformed Header Data: Expected Map, found non-Map"); - None - } - } + get_named_text_from_header("URL", header) } /** @@ -60,12 +30,24 @@ pub mod data { * Does no spec verification. */ pub fn get_name_from_header(header: &FHD) -> Option { + get_named_text_from_header("NAME", header) + } + + + /** + * Get a named field from the header, which has to be of this format + * + * { ..., "": "", ... } + * + * Does no spec verification. + */ + pub fn get_named_text_from_header(name: &'static str, header: &FHD) -> Option { match header { &FHD::Map{keys: ref ks} => { let mut keys : Vec = ks.clone(); keys.iter().find(|k| { match k.deref() { - &FHD::Key{name: ref n, value: ref v} => n == "NAME", + &FHD::Key{name: ref n, value: ref v} => n == name, _ => false } }).and_then(|urlkey| {