summaryrefslogtreecommitdiffstats
path: root/imag-store
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-01-31 19:01:40 +0100
committerMatthias Beyer <mail@beyermatthias.de>2016-02-09 13:48:14 +0100
commit67a7f1ab78e8252fb9608cdfd5a541a0e269e0e6 (patch)
tree846425a508274ee8f5f8812f23856c5b4d68f79c /imag-store
parent756bd09a831480f753367a2dcf4985a8750a1e80 (diff)
Add dedicated function for creating an entry with content
Diffstat (limited to 'imag-store')
-rw-r--r--imag-store/src/create.rs64
1 files 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 == "-" {