diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-10-14 13:41:44 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-10-14 14:05:56 +0200 |
commit | d37de44c9414374839c17cc7fd7ac46a87195082 (patch) | |
tree | 228ce95e421ac97124142496d5450daedcdaca3c /lib/entry/libimagentryref | |
parent | 6c387e893c3421cd68ca8b5251d09614fc68f5ba (diff) |
Add RefStore::find_storeid_by_partial_hash() helper
Diffstat (limited to 'lib/entry/libimagentryref')
-rw-r--r-- | lib/entry/libimagentryref/src/refstore.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/entry/libimagentryref/src/refstore.rs b/lib/entry/libimagentryref/src/refstore.rs index b1ac9381..102d3d94 100644 --- a/lib/entry/libimagentryref/src/refstore.rs +++ b/lib/entry/libimagentryref/src/refstore.rs @@ -23,6 +23,7 @@ use std::fs::File; use libimagstore::store::FileLockEntry; use libimagstore::storeid::IntoStoreId; +use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreIdIterator; use libimagstore::store::Store; @@ -47,6 +48,10 @@ pub trait RefStore { /// Returns None if the hash cannot be found. fn get_by_hash<'a>(&'a self, hash: String) -> Result<Option<FileLockEntry<'a>>>; + /// Find a store id by partial ref (also see documentation for + /// `RefStore::get_by_partitial_hash()`. + fn find_storeid_by_partial_hash(&self, hash: &String) -> Result<Option<StoreId>>; + /// Get a Ref object from the store by (eventually partial) hash. /// /// If the hash is complete, `RefStore::get_by_hash()` should be used as it is cheaper. @@ -123,23 +128,32 @@ impl RefStore for Store { .map_err(From::from) } - /// Get a Ref object from the store by (eventually partial) hash. - /// - /// If the hash is complete, `RefStore::get_by_hash()` should be used as it is cheaper. - /// If the hash comes from user input and thus might be abbreviated, this function can be used. - fn get_by_partitial_hash<'a>(&'a self, hash: &String) -> Result<Option<FileLockEntry<'a>>> { + fn find_storeid_by_partial_hash(&self, hash: &String) -> Result<Option<StoreId>> { + debug!("Trying to find '{}' in store...", hash); for id in self.retrieve_for_module("ref")? { let components_have_hash = id .components() .any(|c| c.as_os_str().to_str().map(|s| s.contains(hash)).unwrap_or(false)); if components_have_hash { - return self.get(id).map_err(From::from); + debug!("Found hash '{}' in {:?}", hash, id); + return Ok(Some(id)) } } Ok(None) } + /// Get a Ref object from the store by (eventually partial) hash. + /// + /// If the hash is complete, `RefStore::get_by_hash()` should be used as it is cheaper. + /// If the hash comes from user input and thus might be abbreviated, this function can be used. + fn get_by_partitial_hash<'a>(&'a self, hash: &String) -> Result<Option<FileLockEntry<'a>>> { + match self.find_storeid_by_partial_hash(hash)? { + Some(id) => self.get(id).map_err(From::from), + None => Ok(None), + } + } + /// Delete a ref by hash /// /// If the returned Result contains an error, the ref might not be deleted. |