From 7e401b5881911b197fe540577a5bd3ed6b0f9dba Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 28 Dec 2015 20:46:19 +0100 Subject: [PATCH] Add generic alter_tags_in_files() function --- src/module/bm/header.rs | 8 ++--- src/module/bm/mod.rs | 68 ++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/module/bm/header.rs b/src/module/bm/header.rs index 4a823cde..3bbe2861 100644 --- a/src/module/bm/header.rs +++ b/src/module/bm/header.rs @@ -30,11 +30,11 @@ pub fn get_tags_from_header(header: &FHD) -> Vec { headerhelpers::tags::data::get_tags_from_header(header) } -pub fn set_tags_in_header(header: &FHD, tags: Vec) { - headerhelpers::tags::data::set_tags_in_header(header, tags) -} - pub fn get_url_from_header(header: &FHD) -> Option { headerhelpers::data::get_url_from_header(header) } +pub fn rebuild_header_with_tags(header: &FHD, tags: Vec) -> Option { + get_url_from_header(header).map(|url| build_header(url, tags)) +} + diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs index ba808229..ae8ba9bf 100644 --- a/src/module/bm/mod.rs +++ b/src/module/bm/mod.rs @@ -153,47 +153,65 @@ impl<'a> BM<'a> { } fn command_add_tags(&self, matches: &ArgMatches) -> bool { - use self::header::set_tags_in_header; + self.alter_tags_in_files(matches, |old_tags, cli_tags| { + let mut new_tags = old_tags.clone(); + new_tags.append(&mut cli_tags.clone()); + new_tags + }) + } - let tags = matches.value_of("tags") + fn alter_tags_in_files(&self, matches: &ArgMatches, generate_new_tags: F) -> bool + where F: Fn(Vec, &Vec) -> Vec + { + use self::header::rebuild_header_with_tags; + + let cli_tags = matches.value_of("tags") .map(|ts| { ts.split(",") .map(String::from) .collect::>() }) .unwrap_or(vec![]); - let (filter, files) = self.get_files(matches, "with_id", "with_match", "with_tags"); - if tags.len() == 0 { - error!("No tags to add, exiting."); - exit(1); - } + let (filter, files) = self.get_files(matches, "with_id", "with_match", "with_tags"); if !filter { warn!("There were no filter applied when loading the files"); } - let result = files - .iter() + let parser = Parser::new(JsonHeaderParser::new(None)); + files + .into_iter() .map(|file| { - debug!("Adding tags to file: {:?}", file); - let f = file.deref().borrow(); - let hdr = f.header(); - let mut ts = get_tags_from_header(hdr); - let mut append_tags = tags.clone(); - ts.append(&mut append_tags); - set_tags_in_header(hdr, ts); + debug!("Remove tags from file: {:?}", file); + + let hdr = { + let f = file.deref().borrow(); + f.header().clone() + }; + + debug!("Tags:..."); + let old_tags = get_tags_from_header(&hdr); + debug!(" old_tags = {:?}", &old_tags); + debug!(" cli_tags = {:?}", &cli_tags); + + let new_tags = generate_new_tags(old_tags, &cli_tags); + debug!(" new_tags = {:?}", &new_tags); + + let new_header = rebuild_header_with_tags(&hdr, new_tags) + .unwrap_or_else(|| { + error!("Could not rebuild header for file"); + exit(1); + }); + { + let mut f_mut = file.deref().borrow_mut(); + f_mut.set_header(new_header); + } + + self.rt.store().persist(&parser, file); true }) - .all(|x| x); - - if result { - info!("Adding tags to links succeeded"); - } else { - error!("Adding tags to links failed"); - } - - return result; + .all(|x| x) } fn get_files(&self,