Auto merge of #111 - neithernut:impl-store-delete, r=matthiasbeyer

Implement `Store::delete()`

Targets #106.
This commit is contained in:
Homu 2016-01-17 10:12:30 -08:00
commit bec97f8fc2
2 changed files with 17 additions and 3 deletions

View file

@ -6,6 +6,8 @@ use std::clone::Clone;
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum StoreErrorKind { pub enum StoreErrorKind {
FileError,
IdLocked,
IdNotFound, IdNotFound,
OutOfMemory, OutOfMemory,
// maybe more // maybe more
@ -13,6 +15,8 @@ pub enum StoreErrorKind {
fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
match e { match e {
&StoreErrorKind::FileError => "File Error",
&StoreErrorKind::IdLocked => "ID locked",
&StoreErrorKind::IdNotFound => "ID not found", &StoreErrorKind::IdNotFound => "ID not found",
&StoreErrorKind::OutOfMemory => "Out of Memory", &StoreErrorKind::OutOfMemory => "Out of Memory",
} }

View file

@ -1,5 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::{File, remove_file};
use std::ops::Drop; use std::ops::Drop;
use std::path::PathBuf; use std::path::PathBuf;
use std::result::Result as RResult; use std::result::Result as RResult;
@ -9,7 +9,7 @@ use std::sync::RwLock;
use fs2::FileExt; use fs2::FileExt;
use entry::Entry; use entry::Entry;
use error::StoreError; use error::{StoreError, StoreErrorKind};
/// The Result Type returned by any interaction with the store that could fail /// The Result Type returned by any interaction with the store that could fail
pub type Result<T> = RResult<T, StoreError>; pub type Result<T> = RResult<T, StoreError>;
@ -156,7 +156,17 @@ impl Store {
/// Delete an entry /// Delete an entry
pub fn delete(&self, id: StoreId) -> Result<()> { pub fn delete(&self, id: StoreId) -> Result<()> {
unimplemented!(); let mut entries_lock = self.entries.write();
let mut entries = entries_lock.unwrap();
// if the entry is currently modified by the user, we cannot drop it
if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) {
return Err(StoreError::new(StoreErrorKind::IdLocked, None));
}
// remove the entry first, then the file
entries.remove(&id);
remove_file(&id).map_err(|e| StoreError::new(StoreErrorKind::FileError, Some(Box::new(e))))
} }
} }