From 67a7f1ab78e8252fb9608cdfd5a541a0e269e0e6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 31 Jan 2016 19:01:40 +0100 Subject: [PATCH] Add dedicated function for creating an entry with content --- imag-store/src/create.rs | 64 ++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/imag-store/src/create.rs b/imag-store/src/create.rs index 576ea10f..086b4698 100644 --- a/imag-store/src/create.rs +++ b/imag-store/src/create.rs @@ -28,13 +28,17 @@ pub fn create(rt: &Runtime) { let path = build_entry_path(rt, scmd.value_of("path").unwrap()); debug!("path = {:?}", path); - scmd.subcommand_matches("entry") - .map(|entry| create_from_cli_spec(rt, scmd, &path)) - .ok_or(()) // hackythehackhack - .map_err(|_| { - create_from_source(rt, scmd, &path) - .unwrap_or_else(|e| debug!("Error building Entry: {:?}", e)) - }); + if scmd.subcommand_matches("entry").is_some() { + create_from_cli_spec(rt, scmd, &path) + .or_else(|_| create_from_source(rt, scmd, &path)) + .or_else(|_| create_with_content_and_header(rt, + &path, + String::new(), + EntryHeader::new())) + } else { + create_with_content_and_header(rt, &path, String::new(), EntryHeader::new()) + } + .unwrap_or_else(|e| debug!("Error building Entry: {:?}", e)) }); } @@ -61,24 +65,11 @@ fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> R debug!("Got content with len = {}", content.len()); - rt.store() - .create(PathBuf::from(path)) - .map(|mut element| { - { - let mut e_content = element.get_content_mut(); - *e_content = content; - debug!("New content set"); - } - { - let mut e_header = element.get_header_mut(); - matches.subcommand_matches("entry") - .map(|entry_matches| { - *e_header = build_toml_header(entry_matches, EntryHeader::new()); - debug!("New header set"); - }); - } - }) - .map_err(|e| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(e)))) + let header = matches.subcommand_matches("entry") + .map(|entry_matches| build_toml_header(entry_matches, EntryHeader::new())) + .unwrap_or(EntryHeader::new()); + + create_with_content_and_header(rt, path, content, header) } fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Result<()> { @@ -106,6 +97,29 @@ fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Res .map_err(|serr| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(serr)))) } +fn create_with_content_and_header(rt: &Runtime, + path: &PathBuf, + content: String, + header: EntryHeader) -> Result<()> +{ + debug!("Creating entry with content"); + rt.store() + .create(PathBuf::from(path)) + .map(|mut element| { + { + let mut e_content = element.get_content_mut(); + *e_content = content; + debug!("New content set"); + } + { + let mut e_header = element.get_header_mut(); + *e_header = header; + debug!("New header set"); + } + }) + .map_err(|e| StoreError::new(StoreErrorKind::BackendError, Some(Box::new(e)))) +} + fn string_from_raw_src(raw_src: &str) -> String { let mut content = String::new(); if raw_src == "-" {