Rewrite adding/removing to allow adding and removing multiple tags

This commit is contained in:
Matthias Beyer 2016-05-15 00:04:13 +02:00
parent 072564f6b8
commit 332dba028c

View file

@ -42,8 +42,8 @@ fn main() {
.subcommand_name() .subcommand_name()
.map_or_else( .map_or_else(
|| { || {
let add = rt.cli().value_of("add"); let add = rt.cli().values_of("add").map(|o| o.map(String::from));
let rem = rt.cli().value_of("remove"); let rem = rt.cli().values_of("remove").map(|o| o.map(String::from));
alter(&rt, id, add, rem); alter(&rt, id, add, rem);
}, },
@ -59,7 +59,7 @@ fn main() {
}); });
} }
fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>) { fn alter<SI: Iterator<Item = String>>(rt: &Runtime, id: &str, add: Option<SI>, rem: Option<SI>) {
let path = { let path = {
match build_entry_path(rt.store(), id) { match build_entry_path(rt.store(), id) {
Err(e) => { Err(e) => {
@ -76,22 +76,22 @@ fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>) {
.retrieve(path) .retrieve(path)
.map(|mut e| { .map(|mut e| {
add.map(|tags| { add.map(|tags| {
for tag in tags.split(',') { for tag in tags {
info!("Adding tag '{}'", tag); debug!("Adding tag '{:?}'", tag);
if let Err(e) = e.add_tag(String::from(tag)) { if let Err(e) = e.add_tag(tag) {
trace_error(&e); trace_error(&e);
} }
} }
}); }); // it is okay to ignore a None here
rem.map(|tags| { rem.map(|tags| {
for tag in tags.split(',') { for tag in tags {
info!("Removing tag '{}'", tag); debug!("Removing tag '{:?}'", tag);
if let Err(e) = e.remove_tag(String::from(tag)) { if let Err(e) = e.remove_tag(tag) {
trace_error(&e); trace_error(&e);
} }
} }
}); }); // it is okay to ignore a None here
}) })
.map_err(|e| { .map_err(|e| {
info!("No entry."); info!("No entry.");