diff --git a/libimagentrylist/src/listers/path.rs b/libimagentrylist/src/listers/path.rs index 0f1deac5..e3602770 100644 --- a/libimagentrylist/src/listers/path.rs +++ b/libimagentrylist/src/listers/path.rs @@ -32,7 +32,7 @@ impl Lister for PathLister { if self.absolute { pb.canonicalize().map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e)))) } else { - Ok(pb) + Ok(pb.into()) } }) .and_then(|pb| { diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 9d174e24..faf1d784 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -23,7 +23,7 @@ use glob::glob; use error::{ParserErrorKind, ParserError}; use error::{StoreError, StoreErrorKind}; -use storeid::{StoreId, StoreIdIterator}; +use storeid::{IntoStoreId, StoreId, StoreIdIterator}; use lazyfile::LazyFile; use hook::aspect::Aspect; @@ -57,7 +57,7 @@ impl StoreEntry { fn new(id: StoreId) -> StoreEntry { StoreEntry { id: id.clone(), - file: LazyFile::Absent(id), + file: LazyFile::Absent(id.into()), status: StoreEntryStatus::Present, } } @@ -241,12 +241,12 @@ impl Store { let mut new_id = self.location.clone(); new_id.push(id); debug!("Created: '{:?}'", new_id); - new_id + StoreId::from(new_id) } /// Creates the Entry at the given location (inside the entry) - pub fn create<'a>(&'a self, id: StoreId) -> Result> { - let id = self.storify_id(id); + pub fn create<'a, S: IntoStoreId>(&'a self, id: S) -> Result> { + let id = self.storify_id(id.into_storeid()); if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) { return Err(e); } @@ -273,8 +273,8 @@ impl Store { /// Borrow a given Entry. When the `FileLockEntry` is either `update`d or /// dropped, the new Entry is written to disk - pub fn retrieve<'a>(&'a self, id: StoreId) -> Result> { - let id = self.storify_id(id); + pub fn retrieve<'a, S: IntoStoreId>(&'a self, id: S) -> Result> { + let id = self.storify_id(id.into_storeid()); if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) { return Err(e); } @@ -356,8 +356,8 @@ impl Store { /// Retrieve a copy of a given entry, this cannot be used to mutate /// the one on disk - pub fn retrieve_copy(&self, id: StoreId) -> Result { - let id = self.storify_id(id); + pub fn retrieve_copy(&self, id: S) -> Result { + let id = self.storify_id(id.into_storeid()); let entries_lock = self.entries.write(); if entries_lock.is_err() { return Err(StoreError::new(StoreErrorKind::LockPoisoned, None)) @@ -374,8 +374,8 @@ impl Store { } /// Delete an entry - pub fn delete(&self, id: StoreId) -> Result<()> { - let id = self.storify_id(id); + pub fn delete(&self, id: S) -> Result<()> { + let id = self.storify_id(id.into_storeid()); if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) { return Err(e); } @@ -1119,7 +1119,7 @@ impl Entry { } } - pub fn from_file(loc: StoreId, file: &mut File) -> Result { + pub fn from_file(loc: S, file: &mut File) -> Result { let text = { use std::io::Read; let mut s = String::new(); @@ -1129,7 +1129,7 @@ impl Entry { Self::from_str(loc, &text[..]) } - pub fn from_str(loc: StoreId, s: &str) -> Result { + pub fn from_str(loc: S, s: &str) -> Result { debug!("Building entry from string"); lazy_static! { static ref RE: Regex = Regex::new(r"(?smx) @@ -1157,7 +1157,7 @@ impl Entry { debug!("Header and content found. Yay! Building Entry object now"); Ok(Entry { - location: loc, + location: loc.into_storeid(), header: try!(EntryHeader::parse(header.unwrap())), content: content.into(), }) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 7c5940c1..d5229e2b 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -1,4 +1,8 @@ use std::path::PathBuf; +use std::path::Path; +use std::borrow::Borrow; +use std::ops::Deref; + use glob::Paths; use semver::Version; use std::fmt::{Debug, Formatter}; @@ -10,7 +14,57 @@ use store::Result; use store::Store; /// The Index into the Store -pub type StoreId = PathBuf; +#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)] +pub struct StoreId(PathBuf); + +impl Into for StoreId { + + fn into(self) -> PathBuf { + self.0 + } + +} + +impl Deref for StoreId { + type Target = PathBuf; + + fn deref(&self) -> &PathBuf { + &self.0 + } + +} + +impl From for StoreId { + + fn from(pb: PathBuf) -> StoreId { + StoreId(pb) + } + +} + +impl From for StoreId { + + fn from(string: String) -> StoreId { + StoreId(string.into()) + } + +} + +impl AsRef for StoreId { + + fn as_ref(&self) -> &Path { + self.0.as_ref() + } + +} + +impl Borrow for StoreId { + + fn borrow(&self) -> &Path { + self.0.borrow() + } + +} /// This Trait allows you to convert various representations to a single one /// suitable for usage in the Store @@ -19,6 +73,12 @@ pub trait IntoStoreId { } impl IntoStoreId for PathBuf { + fn into_storeid(self) -> StoreId { + StoreId(self) + } +} + +impl IntoStoreId for StoreId { fn into_storeid(self) -> StoreId { self } @@ -62,6 +122,8 @@ macro_rules! module_entry_path_mod { use std::path::Path; use std::path::PathBuf; + use $crate::storeid::StoreId; + /// A Struct giving you the ability to choose store entries assigned /// to it. /// @@ -86,7 +148,7 @@ macro_rules! module_entry_path_mod { impl $crate::storeid::IntoStoreId for ModuleEntryPath { fn into_storeid(self) -> $crate::storeid::StoreId { - self.0 + StoreId::from(self.0) } } } @@ -119,7 +181,7 @@ impl Iterator for StoreIdIterator { type Item = StoreId; fn next(&mut self) -> Option { - self.paths.next().and_then(|o| o.ok()) + self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p)) } }