From 7ae2d926cc9d2e800877f841d4a51b3d69a91a6d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 22 Apr 2019 11:48:17 +0200 Subject: [PATCH 1/3] Use toml-query master branch during development Signed-off-by: Matthias Beyer --- lib/entry/libimagentryref/Cargo.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/entry/libimagentryref/Cargo.toml b/lib/entry/libimagentryref/Cargo.toml index ccec9afa..d2efadc7 100644 --- a/lib/entry/libimagentryref/Cargo.toml +++ b/lib/entry/libimagentryref/Cargo.toml @@ -34,9 +34,11 @@ libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagentryutil = { version = "0.10.0", path = "../../../lib/entry/libimagentryutil" } [dependencies.toml-query] -version = "0.8" +#version = "0.8" default-features = false -features = ["typed"] +features = ["typed"] +git = "https://github.com/matthiasbeyer/toml-query" +branch = "master" [dev-dependencies] env_logger = "0.5" From 2854e88dcc3de3d305f56d0c5c076818c20266bd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 22 Apr 2019 12:36:12 +0200 Subject: [PATCH 2/3] Rename: "ref.collection" to "ref.basepath" The setting in the header should reflect the name of the setting in the configuration file, for a better user experience. Signed-off-by: Matthias Beyer --- lib/entry/libimagentryref/src/reference.rs | 80 +++++++++++----------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index 68dd238b..64c77a41 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -37,13 +37,13 @@ use failure::ResultExt; use hasher::Hasher; -/// A configuration of "collection name" -> "collection path" mappings +/// A configuration of "basepath name" -> "basepath path" mappings /// /// Should be deserializeable from the configuration file right away, because we expect a /// configuration like this in the config file: /// /// ```toml -/// [ref.collections] +/// [ref.basepathes] /// music = "/home/alice/music" /// documents = "/home/alice/doc" /// ``` @@ -182,12 +182,12 @@ impl<'a, H: Hasher> Ref for RefWithHasher<'a, H> { .map(PathBuf::from) .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.relpath")))?; - let collection_name = self.0 + let basepath_name = self.0 .get_header() - .read_string("ref.collection")? - .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.collection")))?; + .read_string("ref.basepath")? + .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("ref.basepath")))?; - get_file_path(config, &collection_name, relpath) + get_file_path(config, &basepath_name, relpath) } /// Get the relative path, relative to the configured basepath @@ -211,10 +211,10 @@ impl<'a, H: Hasher> Ref for RefWithHasher<'a, H> { .read("ref")? .ok_or_else(|| err_msg("Header missing at 'ref'"))?; - let collection_name = ref_header - .read("collection") + let basepath_name = ref_header + .read("basepath") .map_err(Error::from)? - .ok_or_else(|| err_msg("Header missing at 'ref.collection'"))? + .ok_or_else(|| err_msg("Header missing at 'ref.basepath'"))? .as_str() .ok_or_else(|| Error::from(EM::EntryHeaderTypeError2("ref.hash.", "string")))?; @@ -227,7 +227,7 @@ impl<'a, H: Hasher> Ref for RefWithHasher<'a, H> { .ok_or_else(|| Error::from(EM::EntryHeaderTypeError2("ref.hash.", "string")))?; - let file_path = get_file_path(config, collection_name.as_ref(), &path)?; + let file_path = get_file_path(config, basepath_name.as_ref(), &path)?; ref_header .read(H::NAME) @@ -249,7 +249,7 @@ pub trait MutRef { /// Make a ref out of a normal (non-ref) entry. /// /// If the entry is already a ref, this fails if `force` is false - fn make_ref(&mut self, path: P, collection_name: Coll, config: &Config, force: bool) + fn make_ref(&mut self, path: P, basepath_name: Coll, config: &Config, force: bool) -> Result<()> where P: AsRef, Coll: AsRef; @@ -278,7 +278,7 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> } let _ = header.delete("ref.hash").context("Removing ref.hash")?; - let _ = header.delete("ref.collection").context("Removing ref.collection")?; + let _ = header.delete("ref.basepath").context("Removing ref.basepath")?; } debug!("Removing 'ref' header marker"); @@ -301,13 +301,13 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> /// /// If the entry is already a ref, this fails if `force` is false /// - fn make_ref(&mut self, path: P, collection_name: Coll, config: &Config, force: bool) + fn make_ref(&mut self, path: P, basepath_name: Coll, config: &Config, force: bool) -> Result<()> where P: AsRef, Coll: AsRef { trace!("Making ref out of {:?}", self.0); - trace!("Making ref with collection name {:?}", collection_name.as_ref()); + trace!("Making ref with basepath name {:?}", basepath_name.as_ref()); trace!("Making ref with config {:?}", config); trace!("Making ref forced = {}", force); @@ -316,7 +316,7 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> let _ = Err(err_msg("Entry is already a reference")).context("Making ref out of entry")?; } - let file_path = get_file_path(config, collection_name.as_ref(), &path)?; + let file_path = get_file_path(config, basepath_name.as_ref(), &path)?; if !file_path.exists() { let msg = format_err!("File '{:?}' does not exist", file_path); @@ -329,13 +329,13 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> trace!("hash = {}", hash); // stripping the prefix of "path" - let prefix = get_basepath(collection_name.as_ref(), config)?; + let prefix = get_basepath(basepath_name.as_ref(), config)?; trace!("Stripping = {}", prefix.display()); let relpath = path.as_ref().strip_prefix(prefix)?; trace!("Using relpath = {} to make header section", relpath.display()); - make_header_section(hash, H::NAME, relpath, collection_name) + make_header_section(hash, H::NAME, relpath, basepath_name) }) .and_then(|h| self.0.get_header_mut().insert("ref", h).map_err(Error::from)) .and_then(|_| self.0.set_isflag::()) @@ -355,8 +355,8 @@ impl<'a, H> MutRef for MutRefWithHasher<'a, H> /// /// # Warning /// -/// The `relpath` _must_ be relative to the configured path for that collection. -pub(crate) fn make_header_section(hash: String, hashname: H, relpath: P, collection: C) +/// The `relpath` _must_ be relative to the configured path for that basepath. +pub(crate) fn make_header_section(hash: String, hashname: H, relpath: P, basepath: C) -> Result where P: AsRef, C: AsRef, @@ -382,26 +382,26 @@ pub(crate) fn make_header_section(hash: String, hashname: H, relpath: P let _ = header_section.insert("hash", hash_table)?; } - let _ = header_section.insert("collection", Value::String(String::from(collection.as_ref()))); + let _ = header_section.insert("basepath", Value::String(String::from(basepath.as_ref()))); Ok(header_section) } -fn get_basepath<'a, Coll: AsRef>(collection_name: Coll, config: &'a Config) -> Result<&'a PathBuf> { - config.get(collection_name.as_ref()) - .ok_or_else(|| format_err!("Collection {} seems not to exist in config", - collection_name.as_ref())) +fn get_basepath<'a, Coll: AsRef>(basepath_name: Coll, config: &'a Config) -> Result<&'a PathBuf> { + config.get(basepath_name.as_ref()) + .ok_or_else(|| format_err!("basepath {} seems not to exist in config", + basepath_name.as_ref())) .map_err(Error::from) } -fn get_file_path

(config: &Config, collection_name: &str, path: P) -> Result +fn get_file_path

(config: &Config, basepath_name: &str, path: P) -> Result where P: AsRef { config - .get(collection_name) + .get(basepath_name) .map(PathBuf::clone) .ok_or_else(|| { - format_err!("Configuration missing for collection: '{}'", collection_name) + format_err!("Configuration missing for basepath: '{}'", basepath_name) }) .context("Making ref out of entry") .map_err(Error::from) @@ -459,14 +459,14 @@ mod test { let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref")).unwrap(); let file = PathBuf::from("/tmp"); // has to exist - let collection_name = "some_collection"; + let basepath_name = "some_basepath"; let config = Config({ let mut c = BTreeMap::new(); - c.insert(String::from("some_collection"), PathBuf::from("/")); + c.insert(String::from("some_basepath"), PathBuf::from("/")); c }); - let r = entry.as_ref_with_hasher_mut::().make_ref(file, collection_name, &config, false); + let r = entry.as_ref_with_hasher_mut::().make_ref(file, basepath_name, &config, false); assert!(r.is_ok()); } @@ -476,14 +476,14 @@ mod test { let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref_isref")).unwrap(); let file = PathBuf::from("/tmp"); // has to exists - let collection_name = "some_collection"; + let basepath_name = "some_basepath"; let config = Config({ let mut c = BTreeMap::new(); - c.insert(String::from("some_collection"), PathBuf::from("/")); + c.insert(String::from("some_basepath"), PathBuf::from("/")); c }); - let res = entry.as_ref_with_hasher_mut::().make_ref(file, collection_name, &config, false); + let res = entry.as_ref_with_hasher_mut::().make_ref(file, basepath_name, &config, false); assert!(res.is_ok(), "Expected to be ok: {:?}", res); assert!(entry.as_ref_with_hasher::().is_ref().unwrap()); @@ -495,14 +495,14 @@ mod test { let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref_is_ref_with_testhash")).unwrap(); let file = PathBuf::from("/tmp"); // has to exist - let collection_name = "some_collection"; + let basepath_name = "some_basepath"; let config = Config({ let mut c = BTreeMap::new(); - c.insert(String::from("some_collection"), PathBuf::from("/")); + c.insert(String::from("some_basepath"), PathBuf::from("/")); c }); - assert!(entry.as_ref_with_hasher_mut::().make_ref(file, collection_name, &config, false).is_ok()); + assert!(entry.as_ref_with_hasher_mut::().make_ref(file, basepath_name, &config, false).is_ok()); let check_isstr = |entry: &Entry, location, shouldbe| { let var = entry.get_header().read(location); @@ -519,7 +519,7 @@ mod test { check_isstr(&entry, "ref.relpath", "tmp"); check_isstr(&entry, "ref.hash.Testhasher", "/tmp"); // TestHasher hashes by returning the path itself - check_isstr(&entry, "ref.collection", "some_collection"); + check_isstr(&entry, "ref.basepath", "some_basepath"); } #[test] @@ -528,14 +528,14 @@ mod test { let store = get_store(); let mut entry = store.retrieve(PathBuf::from("test_makeref_remref")).unwrap(); let file = PathBuf::from("/"); // has to exist - let collection_name = "some_collection"; + let basepath_name = "some_basepath"; let config = Config({ let mut c = BTreeMap::new(); - c.insert(String::from("some_collection"), PathBuf::from("/")); + c.insert(String::from("some_basepath"), PathBuf::from("/")); c }); - assert!(entry.as_ref_with_hasher_mut::().make_ref(file, collection_name, &config, false).is_ok()); + assert!(entry.as_ref_with_hasher_mut::().make_ref(file, basepath_name, &config, false).is_ok()); assert!(entry.as_ref_with_hasher::().is_ref().unwrap()); let res = entry.as_ref_with_hasher_mut::().remove_ref(); assert!(res.is_ok(), "Expected to be ok: {:?}", res); From cf234437d78f6a63ba58371e74242321ba062c9a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 16 Apr 2019 18:18:43 +0200 Subject: [PATCH 3/3] Implement Partial for Config With this implemented, we can fetch the appropriate configuration easily from the configuration. Signed-off-by: Matthias Beyer --- lib/entry/libimagentryref/src/reference.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index 64c77a41..fe9014d8 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -28,6 +28,7 @@ use libimagerror::errors::ErrorMsg as EM; use toml::Value; use toml_query::read::TomlValueReadExt; +use toml_query::read::Partial; use toml_query::delete::TomlValueDeleteExt; use toml_query::insert::TomlValueInsertExt; use failure::Fallible as Result; @@ -66,6 +67,11 @@ impl Deref for Config { } } +impl<'a> Partial<'a> for Config { + const LOCATION: &'static str = "ref.basepathes"; + type Output = Self; +} + provide_kindflag_path!(pub IsRef, "ref.is_ref"); /// Fassade module