Add from_str to Entry
This commit is contained in:
parent
fdc3dde95b
commit
c7f584a81a
2 changed files with 40 additions and 7 deletions
|
@ -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<ParserError> 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,
|
||||
|
|
|
@ -334,21 +334,24 @@ impl Entry {
|
|||
}
|
||||
|
||||
fn from_file(loc: StoreId, file: &mut File) -> Result<Entry> {
|
||||
let text = {
|
||||
use std::io::Read;
|
||||
let file = {
|
||||
let mut buff = String::new();
|
||||
file.read_to_string(&mut buff);
|
||||
buff
|
||||
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<Entry> {
|
||||
let re = Regex::new(r"(?smx)
|
||||
^---$
|
||||
(?P<header>.*) # Header
|
||||
^---$
|
||||
^---$\n
|
||||
(?P<content>.*) # 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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue