Notes: Make parser module member

This commit is contained in:
Matthias Beyer 2016-01-05 21:40:17 +01:00
parent d25ee7ec8a
commit cdbf6d2d2f

View file

@ -21,6 +21,7 @@ use module::helpers::cli::CliFileFilter;
pub struct Notes<'a> { pub struct Notes<'a> {
rt: &'a Runtime<'a>, rt: &'a Runtime<'a>,
parser: Parser<JsonHeaderParser>,
} }
impl<'a> Notes<'a> { impl<'a> Notes<'a> {
@ -28,6 +29,7 @@ impl<'a> Notes<'a> {
pub fn new(rt: &'a Runtime<'a>) -> Notes<'a> { pub fn new(rt: &'a Runtime<'a>) -> Notes<'a> {
Notes { Notes {
rt: rt, rt: rt,
parser: Parser::new(JsonHeaderParser::new(None)),
} }
} }
@ -36,7 +38,6 @@ impl<'a> Notes<'a> {
use self::header::build_header; use self::header::build_header;
use ui::external::editor::let_user_provide_content; use ui::external::editor::let_user_provide_content;
let parser = Parser::new(JsonHeaderParser::new(None));
let name = matches.value_of("name") let name = matches.value_of("name")
.map(String::from) .map(String::from)
.unwrap_or(String::from("")); .unwrap_or(String::from(""));
@ -54,10 +55,10 @@ impl<'a> Notes<'a> {
let fileid = self.rt.store().new_file_with_content(self, header, content); let fileid = self.rt.store().new_file_with_content(self, header, content);
self.rt self.rt
.store() .store()
.load(self, &parser, &fileid) .load(self, &self.parser, &fileid)
.and_then(|file| { .and_then(|file| {
info!("{}", Yellow.paint(format!("Created file in memory: {}", fileid))); info!("{}", Yellow.paint(format!("Created file in memory: {}", fileid)));
Some(self.rt.store().persist(&parser, file)) Some(self.rt.store().persist(&self.parser, file))
}) })
.unwrap_or(false) .unwrap_or(false)
} }
@ -66,8 +67,6 @@ impl<'a> Notes<'a> {
use ansi_term::Colour::{Red, Green}; use ansi_term::Colour::{Red, Green};
use ui::external::editor::edit_content; use ui::external::editor::edit_content;
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
let hash_filter = create_hash_filter(matches, "id", false); let hash_filter = create_hash_filter(matches, "id", false);
let head_filter = create_text_header_field_grep_filter(matches, "namematch", "NAME", false); let head_filter = create_text_header_field_grep_filter(matches, "namematch", "NAME", false);
@ -78,7 +77,7 @@ impl<'a> Notes<'a> {
let result = self.rt let result = self.rt
.store() .store()
.load_for_module(self, &parser) .load_for_module(self, &self.parser)
.into_iter() .into_iter()
.filter(|f| filter.filter_file(f)) .filter(|f| filter.filter_file(f))
.map(|file| { .map(|file| {
@ -99,7 +98,7 @@ impl<'a> Notes<'a> {
let mut f = file.deref().borrow_mut(); let mut f = file.deref().borrow_mut();
f.set_data(new_content); f.set_data(new_content);
} }
self.runtime().store().persist(&parser, file) self.runtime().store().persist(&self.parser, file)
} else { } else {
debug!("Editing didn't work"); debug!("Editing didn't work");
false false
@ -123,8 +122,6 @@ impl<'a> Notes<'a> {
} }
fn command_open(&self, matches: &ArgMatches) -> bool { fn command_open(&self, matches: &ArgMatches) -> bool {
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
let hash_filter = create_hash_filter(matches, "id", true); let hash_filter = create_hash_filter(matches, "id", true);
let head_filter = create_text_header_field_grep_filter(matches, "match", "NAME", true); let head_filter = create_text_header_field_grep_filter(matches, "match", "NAME", true);
@ -135,7 +132,7 @@ impl<'a> Notes<'a> {
let files = self.rt let files = self.rt
.store() .store()
.load_for_module(self, &parser) .load_for_module(self, &self.parser)
.into_iter() .into_iter()
.filter(|file| { .filter(|file| {
let res = filter.filter_file(file); let res = filter.filter_file(file);
@ -228,8 +225,6 @@ impl<'a> Notes<'a> {
use self::header::get_tags_from_header; use self::header::get_tags_from_header;
use module::helpers::cli::CliFileFilter; use module::helpers::cli::CliFileFilter;
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
let hash_filter = create_hash_filter(matches, "id", true); let hash_filter = create_hash_filter(matches, "id", true);
let head_filter = create_text_header_field_grep_filter(matches, "match", "NAME", true); let head_filter = create_text_header_field_grep_filter(matches, "match", "NAME", true);
@ -242,7 +237,7 @@ impl<'a> Notes<'a> {
printer.print_files_custom( printer.print_files_custom(
self.rt.store() self.rt.store()
.load_for_module(self, &parser) .load_for_module(self, &self.parser)
.into_iter() .into_iter()
.filter(|f| filter.filter_file(f)), .filter(|f| filter.filter_file(f)),
&|file| { &|file| {
@ -282,7 +277,6 @@ impl<'a> Notes<'a> {
table.set_titles(titles); table.set_titles(titles);
debug!("Table setup finished"); debug!("Table setup finished");
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
let hash_filter = create_hash_filter(matches, "id", false); let hash_filter = create_hash_filter(matches, "id", false);
let text_filter = create_text_header_field_grep_filter(matches, "match", "URL", false); let text_filter = create_text_header_field_grep_filter(matches, "match", "URL", false);
@ -292,7 +286,7 @@ impl<'a> Notes<'a> {
let result = self.rt let result = self.rt
.store() .store()
.load_for_module(self, &parser) .load_for_module(self, &self.parser)
.iter() .iter()
.filter(|file| { .filter(|file| {
let res = filter.filter_file(file); let res = filter.filter_file(file);
@ -367,8 +361,6 @@ impl<'a> Notes<'a> {
fn command_remove(&self, matches: &ArgMatches) -> bool { fn command_remove(&self, matches: &ArgMatches) -> bool {
use ansi_term::Colour::{Red, Green}; use ansi_term::Colour::{Red, Green};
let parser = Parser::new(JsonHeaderParser::new(None));
let filter = { let filter = {
let hash_filter = create_hash_filter(matches, "id", false); let hash_filter = create_hash_filter(matches, "id", false);
let text_filter = create_text_header_field_grep_filter(matches, "match", "URL", false); let text_filter = create_text_header_field_grep_filter(matches, "match", "URL", false);
@ -378,7 +370,7 @@ impl<'a> Notes<'a> {
let result = self.rt let result = self.rt
.store() .store()
.load_for_module(self, &parser) .load_for_module(self, &self.parser)
.iter() .iter()
.filter(|file| filter.filter_file(file)) .filter(|file| filter.filter_file(file))
.map(|file| { .map(|file| {
@ -408,8 +400,7 @@ impl<'a> Notes<'a> {
use module::helpers::header::tags::data::alter_tags_in_files; use module::helpers::header::tags::data::alter_tags_in_files;
use self::header::rebuild_header_with_tags; use self::header::rebuild_header_with_tags;
let parser = Parser::new(JsonHeaderParser::new(None)); alter_tags_in_files(self, matches, &self.parser, |old_tags, cli_tags| {
alter_tags_in_files(self, matches, &parser, |old_tags, cli_tags| {
let mut new_tags = old_tags.clone(); let mut new_tags = old_tags.clone();
new_tags.append(&mut cli_tags.clone()); new_tags.append(&mut cli_tags.clone());
new_tags new_tags
@ -420,8 +411,7 @@ impl<'a> Notes<'a> {
use module::helpers::header::tags::data::alter_tags_in_files; use module::helpers::header::tags::data::alter_tags_in_files;
use self::header::rebuild_header_with_tags; use self::header::rebuild_header_with_tags;
let parser = Parser::new(JsonHeaderParser::new(None)); alter_tags_in_files(self, matches, &self.parser, |old_tags, cli_tags| {
alter_tags_in_files(self, matches, &parser, |old_tags, cli_tags| {
old_tags.clone() old_tags.clone()
.into_iter() .into_iter()
.filter(|tag| !cli_tags.contains(tag)) .filter(|tag| !cli_tags.contains(tag))
@ -433,8 +423,7 @@ impl<'a> Notes<'a> {
use module::helpers::header::tags::data::alter_tags_in_files; use module::helpers::header::tags::data::alter_tags_in_files;
use self::header::rebuild_header_with_tags; use self::header::rebuild_header_with_tags;
let parser = Parser::new(JsonHeaderParser::new(None)); alter_tags_in_files(self, matches, &self.parser, |_, cli_tags| {
alter_tags_in_files(self, matches, &parser, |_, cli_tags| {
cli_tags.clone() cli_tags.clone()
}, rebuild_header_with_tags) }, rebuild_header_with_tags)
} }