Refactor: Header helper code can use generic function

This commit is contained in:
Matthias Beyer 2015-12-30 11:37:17 +01:00
parent eed57af0bb
commit d15d50d8a2

View file

@ -19,37 +19,7 @@ pub mod data {
* Does no spec verification.
*/
pub fn get_url_from_header(header: &FHD) -> Option<String> {
match header {
&FHD::Map{keys: ref ks} => {
let mut keys : Vec<FHD> = 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<String> {
get_named_text_from_header("NAME", header)
}
/**
* Get a named field from the header, which has to be of this format
*
* { ..., "<name of field>": "<content as string>", ... }
*
* Does no spec verification.
*/
pub fn get_named_text_from_header(name: &'static str, header: &FHD) -> Option<String> {
match header {
&FHD::Map{keys: ref ks} => {
let mut keys : Vec<FHD> = 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| {