Add storeid

This commit is contained in:
Marcel Müller 2016-01-16 19:52:06 +01:00
parent 0b7ad4e2fd
commit 044c19f0cc
No known key found for this signature in database
GPG key ID: DD4ED37D0CAC76E2
2 changed files with 78 additions and 9 deletions

View file

@ -1,11 +1,10 @@
use std::path::PathBuf;
use header::EntryHeader; use header::EntryHeader;
use content::EntryContent; use content::EntryContent;
use store::StoreId;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Entry { pub struct Entry {
location: PathBuf, location: StoreId,
header: EntryHeader, header: EntryHeader,
content: EntryContent, content: EntryContent,
} }

View file

@ -13,6 +13,52 @@ pub use error::StoreError;
pub type Result<T> = RResult<T, StoreError>; pub type Result<T> = RResult<T, StoreError>;
pub type StoreId = PathBuf;
trait IntoStoreId {
fn into_storeid(self) -> StoreId;
}
impl<'a> IntoStoreId for &'a str {
fn into_storeid(self) -> StoreId {
PathBuf::from(self)
}
}
impl<'a> IntoStoreId for &'a String{
fn into_storeid(self) -> StoreId {
PathBuf::from(self)
}
}
impl IntoStoreId for String{
fn into_storeid(self) -> StoreId {
PathBuf::from(self)
}
}
impl IntoStoreId for PathBuf {
fn into_storeid(self) -> StoreId {
self
}
}
impl<'a> IntoStoreId for &'a PathBuf {
fn into_storeid(self) -> StoreId {
self.clone()
}
}
impl<ISI: IntoStoreId> IntoStoreId for (ISI, ISI) {
fn into_storeid(self) -> StoreId {
let (first, second) = self;
let mut res : StoreId = first.into_storeid();
res.push(second.into_storeid());
res
}
}
pub struct Store { pub struct Store {
location: PathBuf, location: PathBuf,
@ -23,7 +69,7 @@ pub struct Store {
* *
* Could be optimized for a threadsafe HashMap * Could be optimized for a threadsafe HashMap
*/ */
entries: Arc<RwLock<HashMap<PathBuf, (File, Option<Entry>)>>>, entries: Arc<RwLock<HashMap<StoreId, (File, Option<Entry>)>>>,
} }
impl Store { impl Store {
@ -31,16 +77,16 @@ impl Store {
fn create(&self, entry: Entry) -> Result<()> { fn create(&self, entry: Entry) -> Result<()> {
unimplemented!(); unimplemented!();
} }
fn retrieve<'a>(&'a self, path: PathBuf) -> Result<FileLockEntry<'a>> { fn retrieve<'a>(&'a self, path: StoreId) -> Result<FileLockEntry<'a>> {
unimplemented!(); unimplemented!();
} }
fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> { fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> {
unimplemented!(); unimplemented!();
} }
fn retrieve_copy(&self, path: PathBuf) -> Result<Entry> { fn retrieve_copy(&self, path: StoreId) -> Result<Entry> {
unimplemented!(); unimplemented!();
} }
fn delete(&self, path: PathBuf) -> Result<()> { fn delete(&self, path: StoreId) -> Result<()> {
unimplemented!(); unimplemented!();
} }
} }
@ -62,11 +108,11 @@ impl Drop for Store {
pub struct FileLockEntry<'a> { pub struct FileLockEntry<'a> {
store: &'a Store, store: &'a Store,
entry: Entry, entry: Entry,
key: PathBuf, key: StoreId,
} }
impl<'a> FileLockEntry<'a, > { impl<'a> FileLockEntry<'a, > {
fn new(store: &'a Store, entry: Entry, key: PathBuf) -> FileLockEntry<'a> { fn new(store: &'a Store, entry: Entry, key: StoreId) -> FileLockEntry<'a> {
FileLockEntry { FileLockEntry {
store: store, store: store,
entry: entry, entry: entry,
@ -96,3 +142,27 @@ impl<'a> Drop for FileLockEntry<'a> {
*en = Some(self.entry.clone()); *en = Some(self.entry.clone());
} }
} }
#[cfg(test)]
mod test {
use std::path::PathBuf;
use store::{StoreId, IntoStoreId};
#[test]
fn into_storeid_trait() {
let buf = PathBuf::from("abc/def");
let test = ("abc", "def");
assert_eq!(buf, test.into_storeid());
let test = "abc/def";
assert_eq!(buf, test.into_storeid());
let test = String::from("abc/def");
assert_eq!(buf, test.into_storeid());
let test = PathBuf::from("abc/def");
assert_eq!(buf, test.into_storeid());
}
}