diff --git a/src/module/bm/header.rs b/src/module/bm/header.rs index e2479048..c25199aa 100644 --- a/src/module/bm/header.rs +++ b/src/module/bm/header.rs @@ -1,25 +1,14 @@ -use std::ops::Deref; - +use module::helpers::header as headerhelpers; use storage::file::FileHeaderData as FHD; use storage::file::FileHeaderSpec as FHS; pub fn get_spec() -> FHS { - FHS::Map { keys: vec![ url_key(), tags_key() ] } + FHS::Map { + keys: vec![ headerhelpers::tags::spec::url_key(), + headerhelpers::tags::spec::tags_key() ] + } } -fn url_key() -> FHS { - FHS::Key { name: String::from("URL"), value_type: Box::new(FHS::Text) } -} - -fn tags_key() -> FHS { - FHS::Key { name: String::from("TAGS"), value_type: Box::new(text_array()) } -} - -fn text_array() -> FHS { - FHS::Array { allowed_types: vec![FHS::Text] } -} - - pub fn build_header(url: &String, tags: &Vec) -> FHD { FHD::Map { keys: vec![ @@ -29,52 +18,13 @@ pub fn build_header(url: &String, tags: &Vec) -> FHD { }, FHD::Key { name: String::from("TAGS"), - value: Box::new(build_tag_array(tags)) + value: Box::new(headerhelpers::tags::data::build_tag_array(tags)) } ] } } -fn build_tag_array(tags: &Vec) -> FHD { - let texttags = tags.into_iter().map(|t| FHD::Text(t.clone())).collect(); - FHD::Array { values: Box::new(texttags) } -} - pub fn get_tags_from_header(header: &FHD) -> Vec { - let mut tags : Vec = vec![]; - - fn match_array(a: &Box) -> Vec { - let mut tags : Vec = vec![]; - - match a.deref() { - &FHD::Array{values: ref vs} => { - let values : Vec = vs.deref().clone(); - for value in values { - match value { - FHD::Text(t) => tags.push(t), - _ => warn!("Malformed Header Data: Expected Text, found non-Text"), - } - } - } - _ => warn!("Malformed Header Data: Expected Array, found non-Array"), - } - - tags - } - - match header { - &FHD::Map{keys: ref ks} => { - let keys : Vec = ks.clone(); - for key in keys { - match key { - FHD::Key{name: _, value: ref v} => return match_array(v), - _ => warn!("Malformed Header Data: Expected Key, found non-Key"), - } - } - }, - _ => warn!("Malformed Header Data: Expected Map, found non-Map"), - } - - tags + headerhelpers::tags::data::get_tags_from_header(header) } diff --git a/src/module/helpers/header/mod.rs b/src/module/helpers/header/mod.rs new file mode 100644 index 00000000..febdd1c3 --- /dev/null +++ b/src/module/helpers/header/mod.rs @@ -0,0 +1,6 @@ +/* + * Helpers for headers + */ + +pub mod tags; + diff --git a/src/module/helpers/header/tags.rs b/src/module/helpers/header/tags.rs new file mode 100644 index 00000000..f9c22c33 --- /dev/null +++ b/src/module/helpers/header/tags.rs @@ -0,0 +1,71 @@ +/* + * Helpers for headers - Tags + */ + +pub mod spec { + use storage::file::FileHeaderSpec as FHS; + + pub fn url_key() -> FHS { + FHS::Key { name: String::from("URL"), value_type: Box::new(FHS::Text) } + } + + pub fn tags_key() -> FHS { + FHS::Key { name: String::from("TAGS"), value_type: Box::new(text_array()) } + } + + pub fn text_array() -> FHS { + FHS::Array { allowed_types: vec![FHS::Text] } + } + +} + +pub mod data { + use std::ops::Deref; + use storage::file::FileHeaderData as FHD; + + pub fn build_tag_array(tags: &Vec) -> FHD { + let texttags = tags.into_iter().map(|t| FHD::Text(t.clone())).collect(); + FHD::Array { values: Box::new(texttags) } + } + + pub fn get_tags_from_header(header: &FHD) -> Vec { + let mut tags : Vec = vec![]; + + fn match_array(a: &Box) -> Vec { + let mut tags : Vec = vec![]; + + match a.deref() { + &FHD::Array{values: ref vs} => { + let values : Vec = vs.deref().clone(); + for value in values { + match value { + FHD::Text(t) => tags.push(t), + _ => warn!("Malformed Header Data: Expected Text, found non-Text"), + } + } + } + _ => warn!("Malformed Header Data: Expected Array, found non-Array"), + } + + tags + } + + match header { + &FHD::Map{keys: ref ks} => { + let keys : Vec = ks.clone(); + for key in keys { + match key { + FHD::Key{name: _, value: ref v} => return match_array(v), + _ => warn!("Malformed Header Data: Expected Key, found non-Key"), + } + } + }, + _ => warn!("Malformed Header Data: Expected Map, found non-Map"), + } + + tags + } + + +} + diff --git a/src/module/helpers/mod.rs b/src/module/helpers/mod.rs new file mode 100644 index 00000000..f505d688 --- /dev/null +++ b/src/module/helpers/mod.rs @@ -0,0 +1 @@ +pub mod header; diff --git a/src/module/mod.rs b/src/module/mod.rs index 0d8de8e0..a3cd4eaf 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -10,6 +10,7 @@ use runtime::Runtime; use storage::backend::StorageBackend; pub mod bm; +pub mod helpers; #[derive(Debug)] pub struct ModuleError {