Merge pull request #143 from TheNeikos/add-store_update
Implement storenetry:write_entry
This commit is contained in:
commit
82873d82ba
1 changed files with 44 additions and 2 deletions
|
@ -69,6 +69,18 @@ impl StoreEntry {
|
|||
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
|
||||
|
@ -156,7 +168,20 @@ impl Store {
|
|||
/// This method assumes that entry is dropped _right after_ the call, hence
|
||||
/// it is not public.
|
||||
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
|
||||
|
@ -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 {
|
||||
&self.location
|
||||
}
|
||||
|
@ -549,7 +580,7 @@ mod test {
|
|||
|
||||
static TEST_ENTRY : &'static str = "---
|
||||
[imag]
|
||||
version = '0.0.3'
|
||||
version = \"0.0.3\"
|
||||
---
|
||||
Hai";
|
||||
|
||||
|
@ -564,6 +595,17 @@ 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue