Introduce error if the store path exists but is a file

This commit is contained in:
Matthias Beyer 2016-01-17 23:12:38 +01:00
parent 4f71563eb4
commit 80b0501d03
2 changed files with 15 additions and 11 deletions

View file

@ -15,17 +15,19 @@ pub enum StoreErrorKind {
OutOfMemory, OutOfMemory,
FileNotFound, FileNotFound,
FileNotCreated, FileNotCreated,
StorePathExists,
// maybe more // maybe more
} }
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::FileError => "File Error",
&StoreErrorKind::IdLocked => "ID locked", &StoreErrorKind::IdLocked => "ID locked",
&StoreErrorKind::IdNotFound => "ID not found", &StoreErrorKind::IdNotFound => "ID not found",
&StoreErrorKind::OutOfMemory => "Out of Memory", &StoreErrorKind::OutOfMemory => "Out of Memory",
&StoreErrorKind::FileNotFound => "File corresponding to ID not found", &StoreErrorKind::FileNotFound => "File corresponding to ID not found",
&StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created", &StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created",
&StoreErrorKind::StorePathExists => "Store path exists",
} }
} }

View file

@ -54,19 +54,21 @@ pub struct Store {
impl Store { impl Store {
/// Create a new Store object /// Create a new Store object
pub fn new(location: PathBuf) -> Store { pub fn new(location: PathBuf) -> Result<Store> {
use std::fs::create_dir_all; use std::fs::create_dir_all;
if !location.exists() { if !location.exists() {
create_dir_all(location.clone()).ok(); // TODO: Error handling? create_dir_all(location.clone()).ok(); // TODO: Error handling?
} else {
if location.is_file() {
return Err(StoreError::new(StoreErrorKind::StorePathExists, None));
}
} }
// TODO: Path exists, but is a file? What now? Ok(Store {
Store {
location: location, location: location,
entries: Arc::new(RwLock::new(HashMap::new())), entries: Arc::new(RwLock::new(HashMap::new())),
} })
} }
/// Creates the Entry at the given location (inside the entry) /// Creates the Entry at the given location (inside the entry)