Outsource header utilities into header helper

This commit is contained in:
Matthias Beyer 2015-12-05 16:01:30 +01:00
parent 566a2c2cce
commit c6a2d39981
4 changed files with 85 additions and 57 deletions

View file

@ -1,25 +1,14 @@
use std::ops::Deref; use module::header as headerhelpers;
use storage::file::FileHeaderData as FHD; use storage::file::FileHeaderData as FHD;
use storage::file::FileHeaderSpec as FHS; use storage::file::FileHeaderSpec as FHS;
pub fn get_spec() -> 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<String>) -> FHD { pub fn build_header(url: &String, tags: &Vec<String>) -> FHD {
FHD::Map { FHD::Map {
keys: vec![ keys: vec![
@ -29,52 +18,13 @@ pub fn build_header(url: &String, tags: &Vec<String>) -> FHD {
}, },
FHD::Key { FHD::Key {
name: String::from("TAGS"), 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<String>) -> 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<String> { pub fn get_tags_from_header(header: &FHD) -> Vec<String> {
let mut tags : Vec<String> = vec![]; headerhelpers::tags::data::get_tags_from_header(header)
fn match_array(a: &Box<FHD>) -> Vec<String> {
let mut tags : Vec<String> = vec![];
match a.deref() {
&FHD::Array{values: ref vs} => {
let values : Vec<FHD> = 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<FHD> = 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
} }

6
src/module/header/mod.rs Normal file
View file

@ -0,0 +1,6 @@
/*
* Helpers for headers
*/
pub mod tags;

71
src/module/header/tags.rs Normal file
View file

@ -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<String>) -> 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<String> {
let mut tags : Vec<String> = vec![];
fn match_array(a: &Box<FHD>) -> Vec<String> {
let mut tags : Vec<String> = vec![];
match a.deref() {
&FHD::Array{values: ref vs} => {
let values : Vec<FHD> = 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<FHD> = 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
}
}

View file

@ -10,6 +10,7 @@ use runtime::Runtime;
use storage::backend::StorageBackend; use storage::backend::StorageBackend;
pub mod bm; pub mod bm;
pub mod header;
#[derive(Debug)] #[derive(Debug)]
pub struct ModuleError { pub struct ModuleError {