Merge branch 'refactor-header_helpers'
This commit is contained in:
commit
307e13da3d
5 changed files with 86 additions and 57 deletions
|
@ -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<String>) -> FHD {
|
||||
FHD::Map {
|
||||
keys: vec![
|
||||
|
@ -29,52 +18,13 @@ pub fn build_header(url: &String, tags: &Vec<String>) -> 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<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
|
||||
headerhelpers::tags::data::get_tags_from_header(header)
|
||||
}
|
||||
|
||||
|
|
6
src/module/helpers/header/mod.rs
Normal file
6
src/module/helpers/header/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
/*
|
||||
* Helpers for headers
|
||||
*/
|
||||
|
||||
pub mod tags;
|
||||
|
71
src/module/helpers/header/tags.rs
Normal file
71
src/module/helpers/header/tags.rs
Normal 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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
1
src/module/helpers/mod.rs
Normal file
1
src/module/helpers/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod header;
|
|
@ -10,6 +10,7 @@ use runtime::Runtime;
|
|||
use storage::backend::StorageBackend;
|
||||
|
||||
pub mod bm;
|
||||
pub mod helpers;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModuleError {
|
||||
|
|
Loading…
Reference in a new issue