imag/src/module/bm/mod.rs

147 lines
3.9 KiB
Rust
Raw Normal View History

2015-10-25 18:56:04 +00:00
use runtime::Runtime;
use module::Module;
use module::ModuleResult;
use module::ModuleError;
use std::path::Path;
use std::result::Result;
use clap::ArgMatches;
use regex::Regex;
2015-10-30 17:26:04 +00:00
mod header;
2015-11-24 19:15:41 +00:00
mod commands;
2015-10-30 17:26:04 +00:00
2015-11-10 16:38:02 +00:00
use self::header::build_header;
use storage::json::parser::JsonHeaderParser;
use storage::parser::FileHeaderParser;
2015-11-24 19:15:41 +00:00
use self::commands::add::*;
use self::commands::list::*;
use self::commands::remove::*;
2015-11-10 16:38:02 +00:00
2015-10-25 18:56:04 +00:00
pub struct BMModule {
path: Option<String>,
}
const CALLNAMES : &'static [&'static str] = &[ "bm", "bookmark" ];
impl Module for BMModule {
fn new(rt : &Runtime) -> BMModule {
BMModule {
path: None
}
}
fn callnames() -> &'static [&'static str] {
CALLNAMES
}
fn name(&self) -> &'static str{
"Bookmark"
}
fn shutdown(&self, rt : &Runtime) -> ModuleResult {
Ok(())
}
2015-11-24 19:15:41 +00:00
fn get_commands<EC: ExecutableCommand>(&self, rt: &Runtime) -> Vec<EC> {
vec![
AddCommand::new(),
ListCommand::new(),
RemoveCommand::new(),
]
}
2015-10-25 18:56:04 +00:00
}
fn add<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult {
let url = sub.value_of("url").unwrap();
let tags = get_tags(rt, sub);
2015-11-10 16:21:36 +00:00
info!("Adding url '{}' with tags '{:?}'", url, tags);
2015-10-25 18:56:04 +00:00
2015-11-10 16:38:02 +00:00
let header = build_header(&String::from(url), &tags);
let jheader = JsonHeaderParser::new(None).write(&header);
println!("JSON: {:?}", jheader);
2015-10-25 18:56:04 +00:00
Ok(())
}
fn list<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> ModuleResult {
let tags = get_tags(rt, sub);
let matcher = get_matcher(rt, sub);
2015-10-25 20:51:13 +00:00
match matcher {
Some(reg) => {
info!("Listing urls with matcher '{}' and with tags {:?}",
reg.as_str(),
2015-11-10 16:21:36 +00:00
tags);
2015-10-25 20:51:13 +00:00
}
None => {
2015-11-10 16:21:36 +00:00
info!("Listing urls with tags {:?}", tags);
2015-10-25 20:51:13 +00:00
}
}
2015-10-25 18:56:04 +00:00
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);
2015-10-25 20:51:13 +00:00
match id {
Some(idstr) => {
info!("Removing urls with id '{}'", idstr);
}
None => {
match matcher {
Some(reg) => {
info!("Removing urls with matcher '{}' and with tags {:?}",
2015-11-10 16:21:36 +00:00
reg.as_str(), tags);
2015-10-25 20:51:13 +00:00
}
None => {
2015-11-10 16:21:36 +00:00
info!("Listing urls with tags {:?}", tags);
2015-10-25 20:51:13 +00:00
}
}
}
}
2015-10-25 18:56:04 +00:00
Ok(())
}
2015-11-10 16:21:36 +00:00
fn get_tags<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Vec<String> {
2015-10-26 20:30:26 +00:00
debug!("Fetching tags from commandline");
2015-10-25 18:56:04 +00:00
sub.value_of("tags").and_then(|tags|
Some(tags.split(",")
2015-11-10 16:21:36 +00:00
.into_iter()
.map(|s| s.to_string())
2015-11-07 23:09:07 +00:00
.filter(|e|
if e.contains(" ") {
warn!("Tag contains spaces: '{}'", e);
false
2015-11-10 16:26:51 +00:00
} else {
true
2015-11-10 16:21:36 +00:00
}).collect()
2015-10-25 18:56:04 +00:00
)
2015-11-10 16:21:36 +00:00
).or(Some(vec![])).unwrap()
2015-10-25 18:56:04 +00:00
}
fn get_matcher<'a>(rt: &Runtime, sub: &ArgMatches<'a, 'a>) -> Option<Regex> {
2015-10-26 20:30:26 +00:00
debug!("Fetching matcher from commandline");
2015-10-25 18:56:04 +00:00
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> {
2015-10-26 20:30:26 +00:00
debug!("Fetching id from commandline");
2015-10-25 18:56:04 +00:00
sub.value_of("id").and_then(|s| Some(String::from(s)))
}