imag-store: Use TomlValueExt in replace for EntryHeader

Also, do not build empty TOML but use Entry::build_default_header()
This commit is contained in:
Matthias Beyer 2016-11-17 12:41:37 +01:00
parent 36dfee812f
commit 7b3f28eb0a
3 changed files with 16 additions and 21 deletions

View file

@ -28,10 +28,10 @@ use std::io::stderr;
use std::process::exit; use std::process::exit;
use clap::ArgMatches; use clap::ArgMatches;
use toml::Value;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::store::EntryHeader;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagerror::trace::trace_error_exit; use libimagerror::trace::trace_error_exit;
use libimagutil::debug_result::*; use libimagutil::debug_result::*;
@ -70,10 +70,11 @@ pub fn create(rt: &Runtime) {
.or_else(|_| create_with_content_and_header(rt, .or_else(|_| create_with_content_and_header(rt,
&path, &path,
String::new(), String::new(),
EntryHeader::new())) Entry::default_header()))
} else { } else {
debug!("Creating entry"); debug!("Creating entry");
create_with_content_and_header(rt, &path, String::new(), EntryHeader::new()) create_with_content_and_header(rt, &path, String::new(),
Entry::default_header())
} }
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
error!("Error building Entry"); error!("Error building Entry");
@ -100,8 +101,8 @@ fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> R
debug!("Got content with len = {}", content.len()); debug!("Got content with len = {}", content.len());
let header = matches.subcommand_matches("entry") let header = matches.subcommand_matches("entry")
.map_or_else(EntryHeader::new, .map_or_else(|| Entry::default_header(),
|entry_matches| build_toml_header(entry_matches, EntryHeader::new())); |entry_matches| build_toml_header(entry_matches, Entry::default_header()));
create_with_content_and_header(rt, path, content, header) create_with_content_and_header(rt, path, content, header)
} }
@ -138,7 +139,7 @@ fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &StoreId) -> Res
fn create_with_content_and_header(rt: &Runtime, fn create_with_content_and_header(rt: &Runtime,
path: &StoreId, path: &StoreId,
content: String, content: String,
header: EntryHeader) -> Result<()> header: Value) -> Result<()>
{ {
debug!("Creating entry with content at {:?}", path); debug!("Creating entry with content at {:?}", path);
rt.store() rt.store()

View file

@ -20,7 +20,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use clap::ArgMatches; use clap::ArgMatches;
use toml::Value;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
@ -70,8 +69,7 @@ pub fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) {
unimplemented!() unimplemented!()
} else { } else {
debug!("Printing header as TOML..."); debug!("Printing header as TOML...");
// We have to Value::Table() for Display println!("{}", e.get_header())
println!("{}", Value::Table(e.get_header().clone().into()))
} }
} }

View file

@ -24,13 +24,11 @@ use std::str::Split;
use clap::ArgMatches; use clap::ArgMatches;
use toml::Value; use toml::Value;
use libimagstore::store::EntryHeader;
use libimagutil::key_value_split::IntoKeyValue; use libimagutil::key_value_split::IntoKeyValue;
pub fn build_toml_header(matches: &ArgMatches, header: EntryHeader) -> EntryHeader { pub fn build_toml_header(matches: &ArgMatches, mut header: Value) -> Value {
debug!("Building header from cli spec"); debug!("Building header from cli spec");
if let Some(headerspecs) = matches.values_of("header") { if let Some(headerspecs) = matches.values_of("header") {
let mut main = header.into();
let kvs = headerspecs.into_iter() let kvs = headerspecs.into_iter()
.filter_map(|hs| { .filter_map(|hs| {
debug!("- Processing: '{}'", hs); debug!("- Processing: '{}'", hs);
@ -42,18 +40,16 @@ pub fn build_toml_header(matches: &ArgMatches, header: EntryHeader) -> EntryHead
let (key, value) = tpl.into(); let (key, value) = tpl.into();
debug!("Splitting: {:?}", key); debug!("Splitting: {:?}", key);
let mut split = key.split('.'); let mut split = key.split('.');
let current = split.next(); match (split.next(), &mut header) {
if current.is_some() { (Some(cur), &mut Value::Table(ref mut hdr)) =>
insert_key_into(String::from(current.unwrap()), &mut split, Cow::Owned(value), &mut main); insert_key_into(String::from(cur), &mut split, Cow::Owned(value), hdr),
_ => { }
} }
} }
debug!("Header = {:?}", main);
EntryHeader::from(main)
} else {
debug!("Header = {:?}", header);
header
} }
debug!("Header = {:?}", header);
header
} }
fn insert_key_into<'a>(current: String, fn insert_key_into<'a>(current: String,