Transfer old code to new structure

This commit is contained in:
Matthias Beyer 2015-11-28 12:09:00 +01:00
parent 08d6e8da13
commit ff873c8fe3
2 changed files with 95 additions and 94 deletions

View file

@ -4,15 +4,107 @@ use storage::backend::StorageBackend;
use module::CommandResult; use module::CommandResult;
use module::CommandEnv; use module::CommandEnv;
use module::bm::header::build_header;
use storage::json::parser::JsonHeaderParser;
use storage::parser::FileHeaderParser;
use clap::ArgMatches;
use regex::Regex;
pub fn add_command(env: CommandEnv) -> CommandResult { pub fn add_command(env: CommandEnv) -> CommandResult {
unimplemented!() let url = env.matches.value_of("url").unwrap();
let tags = get_tags(env.rt, env.matches);
info!("Adding url '{}' with tags '{:?}'", url, tags);
let header = build_header(&String::from(url), &tags);
let jheader = JsonHeaderParser::new(None).write(&header);
println!("JSON: {:?}", jheader);
Ok(())
} }
pub fn list_command(env: CommandEnv) -> CommandResult { pub fn list_command(env: CommandEnv) -> CommandResult {
unimplemented!() let tags = get_tags(env.rt, env.matches);
let matcher = get_matcher(env.rt, env.matches);
match matcher {
Some(reg) => {
info!("Listing urls with matcher '{}' and with tags {:?}",
reg.as_str(),
tags);
}
None => {
info!("Listing urls with tags {:?}", tags);
}
}
Ok(())
} }
pub fn remove_command(env: CommandEnv) -> CommandResult { pub fn remove_command(env: CommandEnv) -> CommandResult {
unimplemented!() let tags = get_tags(env.rt, env.matches);
let matcher = get_matcher(env.rt, env.matches);
let id = get_id(env.rt, env.matches);
match id {
Some(idstr) => {
info!("Removing urls with id '{}'", idstr);
}
None => {
match matcher {
Some(reg) => {
info!("Removing urls with matcher '{}' and with tags {:?}",
reg.as_str(), tags);
}
None => {
info!("Listing urls with tags {:?}", tags);
}
}
}
}
Ok(())
}
/*
*
* Private helpers
*
*/
fn get_tags<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Vec<String> {
debug!("Fetching tags from commandline");
sub.value_of("tags").and_then(|tags|
Some(tags.split(",")
.into_iter()
.map(|s| s.to_string())
.filter(|e|
if e.contains(" ") {
warn!("Tag contains spaces: '{}'", e);
false
} else {
true
}).collect()
)
).or(Some(vec![])).unwrap()
}
fn get_matcher<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option<Regex> {
debug!("Fetching matcher from commandline");
if let Some(s) = sub.value_of("match") {
if let Ok(r) = Regex::new(s) {
return Some(r)
} else {
error!("Regex error, continuing without regex");
}
}
None
}
fn get_id<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option<String> {
debug!("Fetching id from commandline");
sub.value_of("id").and_then(|s| Some(String::from(s)))
} }

View file

@ -52,94 +52,3 @@ impl Module for BMModule {
} }
} }
fn add<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult {
let url = sub.value_of("url").unwrap();
let tags = get_tags(rt, sub);
info!("Adding url '{}' with tags '{:?}'", url, tags);
let header = build_header(&String::from(url), &tags);
let jheader = JsonHeaderParser::new(None).write(&header);
println!("JSON: {:?}", jheader);
Ok(())
}
fn list<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult {
let tags = get_tags(rt, sub);
let matcher = get_matcher(rt, sub);
match matcher {
Some(reg) => {
info!("Listing urls with matcher '{}' and with tags {:?}",
reg.as_str(),
tags);
}
None => {
info!("Listing urls with tags {:?}", tags);
}
}
Ok(())
}
fn remove<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult {
let tags = get_tags(rt, sub);
let matcher = get_matcher(rt, sub);
let id = get_id(rt, sub);
match id {
Some(idstr) => {
info!("Removing urls with id '{}'", idstr);
}
None => {
match matcher {
Some(reg) => {
info!("Removing urls with matcher '{}' and with tags {:?}",
reg.as_str(), tags);
}
None => {
info!("Listing urls with tags {:?}", tags);
}
}
}
}
Ok(())
}
fn get_tags<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Vec<String> {
debug!("Fetching tags from commandline");
sub.value_of("tags").and_then(|tags|
Some(tags.split(",")
.into_iter()
.map(|s| s.to_string())
.filter(|e|
if e.contains(" ") {
warn!("Tag contains spaces: '{}'", e);
false
} else {
true
}).collect()
)
).or(Some(vec![])).unwrap()
}
fn get_matcher<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option<Regex> {
debug!("Fetching matcher from commandline");
if let Some(s) = sub.value_of("match") {
if let Ok(r) = Regex::new(s) {
return Some(r)
} else {
error!("Regex error, continuing without regex");
}
}
None
}
fn get_id<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option<String> {
debug!("Fetching id from commandline");
sub.value_of("id").and_then(|s| Some(String::from(s)))
}