Merge pull request #143 from TheNeikos/add-store_update

Implement storenetry:write_entry
This commit is contained in:
Matthias Beyer 2016-01-24 20:25:57 +01:00
commit 82873d82ba

View file

@ -69,6 +69,18 @@ impl StoreEntry {
return Err(StoreError::new(StoreErrorKind::EntryAlreadyBorrowed, None)) return Err(StoreError::new(StoreErrorKind::EntryAlreadyBorrowed, None))
} }
} }
fn write_entry(&mut self, entry: &Entry) -> Result<()> {
if self.is_borrowed() {
use std::io::Write;
let file = try!(self.file.create_file());
assert_eq!(self.id, entry.location);
file.write_all(entry.to_str().as_bytes());
}
Ok(())
}
} }
/// The Store itself, through this object one can interact with IMAG's entries /// The Store itself, through this object one can interact with IMAG's entries
@ -156,7 +168,20 @@ impl Store {
/// This method assumes that entry is dropped _right after_ the call, hence /// This method assumes that entry is dropped _right after_ the call, hence
/// it is not public. /// it is not public.
fn _update<'a>(&'a self, entry: &FileLockEntry<'a>) -> Result<()> { fn _update<'a>(&'a self, entry: &FileLockEntry<'a>) -> Result<()> {
unimplemented!(); let hsmap = self.entries.write();
if hsmap.is_err() {
return Err(StoreError::new(StoreErrorKind::LockPoisoned, None))
}
let mut hsmap = hsmap.unwrap();
let mut se = try!(hsmap.get_mut(&entry.key)
.ok_or(StoreError::new(StoreErrorKind::IdNotFound, None)));
assert!(!se.is_borrowed(), "Tried to update a non borrowed entry.");
try!(se.write_entry(&entry.entry));
se.status = StoreEntryStatus::Present;
Ok(())
} }
/// Retrieve a copy of a given entry, this cannot be used to mutate /// Retrieve a copy of a given entry, this cannot be used to mutate
@ -400,6 +425,12 @@ impl Entry {
}) })
} }
pub fn to_str(&self) -> String {
format!("---{header}---\n{content}",
header = ::toml::encode_str(&self.header.toml),
content = self.content)
}
pub fn get_location(&self) -> &StoreId { pub fn get_location(&self) -> &StoreId {
&self.location &self.location
} }
@ -549,7 +580,7 @@ mod test {
static TEST_ENTRY : &'static str = "--- static TEST_ENTRY : &'static str = "---
[imag] [imag]
version = '0.0.3' version = \"0.0.3\"
--- ---
Hai"; Hai";
@ -564,6 +595,17 @@ Hai";
assert_eq!(entry.content, "Hai"); assert_eq!(entry.content, "Hai");
} }
#[test]
fn test_entry_to_str() {
use super::Entry;
use std::path::PathBuf;
println!("{}", TEST_ENTRY);
let entry = Entry::from_str(PathBuf::from("/test/foo~1.3"),
TEST_ENTRY).unwrap();
let string = entry.to_str();
assert_eq!(TEST_ENTRY, string);
}
} }