Merge pull request #978 from matthiasbeyer/libimagstore/fixes
Libimagstore/fixes
This commit is contained in:
commit
17bab5b0b9
3 changed files with 42 additions and 14 deletions
|
@ -33,7 +33,7 @@ use storeid::StoreId;
|
||||||
|
|
||||||
use libimagerror::into::IntoError;
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
struct BackendEntry {
|
struct BackendEntry {
|
||||||
header: serde_json::Value,
|
header: serde_json::Value,
|
||||||
content: String,
|
content: String,
|
||||||
|
@ -53,7 +53,7 @@ impl BackendEntry {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
struct Document {
|
struct Document {
|
||||||
version: String,
|
version: String,
|
||||||
store: HashMap<PathBuf, BackendEntry>,
|
store: HashMap<PathBuf, BackendEntry>,
|
||||||
|
@ -72,9 +72,13 @@ impl JsonMapper {
|
||||||
impl Mapper for JsonMapper {
|
impl Mapper for JsonMapper {
|
||||||
fn read_to_fs<R: Read>(&self, r: &mut R, hm: &mut HashMap<PathBuf, Entry>) -> Result<()> {
|
fn read_to_fs<R: Read>(&self, r: &mut R, hm: &mut HashMap<PathBuf, Entry>) -> Result<()> {
|
||||||
let mut document = {
|
let mut document = {
|
||||||
|
debug!("Reading Document");
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
try!(r.read_to_string(&mut s).map_err_into(SEK::IoError));
|
try!(r.read_to_string(&mut s).map_err_into(SEK::IoError));
|
||||||
|
debug!("Document = {:?}", s);
|
||||||
|
debug!("Parsing Document");
|
||||||
let doc : Document = try!(serde_json::from_str(&s).map_err_into(SEK::IoError));
|
let doc : Document = try!(serde_json::from_str(&s).map_err_into(SEK::IoError));
|
||||||
|
debug!("Document = {:?}", doc);
|
||||||
doc
|
doc
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -84,6 +88,10 @@ impl Mapper for JsonMapper {
|
||||||
// safe because cargo does not compile if crate version is not valid
|
// safe because cargo does not compile if crate version is not valid
|
||||||
let crate_version = ::semver::Version::parse(version!()).unwrap();
|
let crate_version = ::semver::Version::parse(version!()).unwrap();
|
||||||
|
|
||||||
|
debug!("Document version vs. own version: {doc_vers} > {crate_vers}",
|
||||||
|
doc_vers = doc_vers,
|
||||||
|
crate_vers = crate_version);
|
||||||
|
|
||||||
if doc_vers > crate_version {
|
if doc_vers > crate_version {
|
||||||
Err(SEK::VersionError.into_error())
|
Err(SEK::VersionError.into_error())
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,9 +100,11 @@ impl Mapper for JsonMapper {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (key, val) in document.store.drain() {
|
for (key, val) in document.store.drain() {
|
||||||
|
debug!("(key, value) ({:?}, {:?})", key, val);
|
||||||
let res = val
|
let res = val
|
||||||
.to_string()
|
.to_string()
|
||||||
.and_then(|vals| {
|
.and_then(|vals| {
|
||||||
|
debug!("value string = {:?}", vals);
|
||||||
StoreId::new_baseless(key.clone())
|
StoreId::new_baseless(key.clone())
|
||||||
.and_then(|id| Entry::from_str(id, &vals))
|
.and_then(|id| Entry::from_str(id, &vals))
|
||||||
.map(|entry| hm.insert(key, entry))
|
.map(|entry| hm.insert(key, entry))
|
||||||
|
@ -153,6 +163,18 @@ mod test {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty_json_to_fs() {
|
||||||
|
let json = r#"{"version":"0.3.0","store":{}}"#;
|
||||||
|
let mut json = Cursor::new(String::from(json).into_bytes());
|
||||||
|
let mapper = JsonMapper::new();
|
||||||
|
let mut hm = HashMap::new();
|
||||||
|
|
||||||
|
let io_res = mapper.read_to_fs(&mut json, &mut hm);
|
||||||
|
assert!(io_res.is_ok());
|
||||||
|
assert!(hm.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_json_to_fs() {
|
fn test_json_to_fs() {
|
||||||
let json = r#"
|
let json = r#"
|
||||||
|
|
|
@ -105,14 +105,19 @@ impl Iterator for Walk {
|
||||||
type Item = StoreObject;
|
type Item = StoreObject;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
|
||||||
while let Some(something) = self.dirwalker.next() {
|
while let Some(something) = self.dirwalker.next() {
|
||||||
|
debug!("[Walk] Processing next item: {:?}", something);
|
||||||
match something {
|
match something {
|
||||||
Ok(next) => if next.file_type().is_dir() {
|
Ok(next) => if next.file_type().is_dir() {
|
||||||
|
debug!("Found directory...");
|
||||||
return Some(StoreObject::Collection(next.path().to_path_buf()))
|
return Some(StoreObject::Collection(next.path().to_path_buf()))
|
||||||
} else if next.file_type().is_file() {
|
} else /* if next.file_type().is_file() */ {
|
||||||
|
debug!("Found file...");
|
||||||
let n = next.path().to_path_buf();
|
let n = next.path().to_path_buf();
|
||||||
let sid = match StoreId::new(Some(self.store_path.clone()), n) {
|
let sid = match StoreId::from_full_path(&self.store_path, n) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
debug!("Could not construct StoreId object from it");
|
||||||
trace_error(&e);
|
trace_error(&e);
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
|
|
|
@ -67,6 +67,7 @@ impl StoreId {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_baseless(id: PathBuf) -> Result<StoreId> {
|
pub fn new_baseless(id: PathBuf) -> Result<StoreId> {
|
||||||
|
debug!("Trying to get a new baseless id from: {:?}", id);
|
||||||
if id.is_absolute() {
|
if id.is_absolute() {
|
||||||
Err(SEK::StoreIdLocalPartAbsoluteError.into_error())
|
Err(SEK::StoreIdLocalPartAbsoluteError.into_error())
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue