diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 41d90c13..9cd19814 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -17,6 +17,7 @@ pub enum StoreErrorKind { OutOfMemory, FileNotFound, FileNotCreated, + IoError, StorePathExists, StorePathCreate, LockPoisoned, @@ -33,6 +34,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { &StoreErrorKind::OutOfMemory => "Out of Memory", &StoreErrorKind::FileNotFound => "File corresponding to ID not found", &StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created", + &StoreErrorKind::IoError => "File Error", &StoreErrorKind::StorePathExists => "Store path exists", &StoreErrorKind::StorePathCreate => "Store path create", &StoreErrorKind::LockPoisoned @@ -113,6 +115,15 @@ impl From for StoreError { } } +impl From<::std::io::Error> for StoreError { + fn from(ps: ::std::io::Error) -> StoreError { + StoreError { + err_type: StoreErrorKind::IoError, + cause: Some(Box::new(ps)), + } + } +} + #[derive(Clone)] pub enum ParserErrorKind { TOMLParserErrors, diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 245e591c..f6e78611 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -334,21 +334,24 @@ impl Entry { } fn from_file(loc: StoreId, file: &mut File) -> Result { - use std::io::Read; - let file = { - let mut buff = String::new(); - file.read_to_string(&mut buff); - buff + let text = { + use std::io::Read; + let mut s = String::new(); + try!(file.read_to_string(&mut s)); + s }; + Self::from_str(loc, &text[..]) + } + fn from_str(loc: StoreId, s: &str) -> Result { let re = Regex::new(r"(?smx) ^---$ (?P
.*) # Header - ^---$ + ^---$\n (?P.*) # Content ").unwrap(); - let matches = re.captures(&file[..]); + let matches = re.captures(s); if matches.is_none() { return Err(StoreError::new(StoreErrorKind::MalformedEntry, None)); @@ -516,6 +519,25 @@ mod test { assert!(verify_header_consistency(header).is_ok()); } + + static TEST_ENTRY : &'static str = "--- +[imag] +version = '0.0.3' +--- +Hai"; + + #[test] + fn test_entry_from_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(); + + assert_eq!(entry.content, "Hai"); + } + + }