Add entry_creation

This commit is contained in:
Marcel Müller 2016-01-23 16:15:34 +01:00
parent d949cddc65
commit ba2e52788b
No known key found for this signature in database
GPG key ID: DD4ED37D0CAC76E2
4 changed files with 59 additions and 12 deletions

View file

@ -16,6 +16,14 @@ pub struct Entry {
impl Entry {
fn new(loc: StoreId) -> Entry {
Entry {
location: loc,
header: EntryHeader::new_current(),
content: EntryContent::new()
}
}
pub fn get_location(&self) -> &StoreId {
&self.location
}

View file

@ -18,6 +18,7 @@ pub enum StoreErrorKind {
StorePathExists,
StorePathCreate,
LockPoisoned,
EntryAlreadyBorrowed
// maybe more
}
@ -33,6 +34,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
&StoreErrorKind::StorePathCreate => "Store path create",
&StoreErrorKind::LockPoisoned
=> "The internal Store Lock has been poisoned",
&StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed",
}
}

View file

@ -1,6 +1,7 @@
use std::collections::BTreeMap;
use std::error::Error;
use std::result::Result as RResult;
use std::collections::BTreeMap;
use toml::{Array, Table, Value};
use version;
@ -102,6 +103,23 @@ impl EntryHeader {
}
}
pub fn new_current() -> EntryHeader {
EntryHeader {
toml: {
let mut header = BTreeMap::new();
let sub = {
let mut sub = BTreeMap::new();
sub.insert("version".into(), Value::String(version!()));
Value::Table(sub)
};
header.insert("imag".into(), sub);
header
}
}
}
/**
* Get the table which lives in the background
*/

View file

@ -11,21 +11,29 @@ use fs2::FileExt;
use entry::Entry;
use error::{StoreError, StoreErrorKind};
use storeid::StoreId;
use lazyfile::LazyFile;
/// The Result Type returned by any interaction with the store that could fail
pub type Result<T> = RResult<T, StoreError>;
#[derive(PartialEq)]
enum StoreEntryPresence {
Present,
Borrowed
}
/// A store entry, depending on the option type it is either borrowed currently
/// or not.
struct StoreEntry {
file: File,
entry: StoreEntryPresence
enum StoreEntry {
Present(StoreId, LazyFile),
Borrowed
}
impl PartialEq for StoreEntry {
fn eq(&self, other: &StoreEntry) -> bool {
use store::StoreEntry::*;
match (*self, *other) {
(Borrowed, Borrowed) => true,
(Borrowed, Present(_,_)) => false,
(Present(_,_), Borrowed) => false,
(Present(ref a,_), Present(ref b, _)) => a == b
}
}
}
@ -33,7 +41,20 @@ impl StoreEntry {
/// The entry is currently borrowed, meaning that some thread is currently
/// mutating it
fn is_borrowed(&self) -> bool {
self.entry == StoreEntryPresence::Borrowed
*self == StoreEntry::Borrowed
}
fn get_entry(&self) -> Result<Entry> {
if let &StoreEntry::Present(ref id, ref file) = self {
let file = file.get_file();
if let Err(StoreError{err_type: StoreErrorKind::FileNotFound, ..}) = file {
Ok(Entry::new(id.clone()))
} else {
unimplemented!()
}
} else {
return Err(StoreError::new(StoreErrorKind::EntryAlreadyBorrowed, None))
}
}
}
@ -134,11 +155,9 @@ impl Drop for Store {
/**
* Unlock all files on drop
*
* TODO: Error message when file cannot be unlocked?
* TODO: Unlock them
*/
fn drop(&mut self) {
self.entries.write().unwrap()
.iter().map(|f| f.1.file.unlock());
}
}