Add Store::load_by_hash()

This commit is contained in:
Matthias Beyer 2015-12-28 15:00:02 +01:00
parent 5a55636c65
commit 3a5bb3a5ce

View file

@ -187,48 +187,27 @@ impl Store {
-> Option<Rc<RefCell<File>>> -> Option<Rc<RefCell<File>>>
where HP: FileHeaderParser where HP: FileHeaderParser
{ {
macro_rules! try_some {
($expr:expr) => (match $expr {
::std::option::Option::Some(val) => val,
::std::option::Option::None => return ::std::option::Option::None,
});
($expr:expr => return) => (match $expr {
::std::option::Option::Some(val) => val,
::std::option::Option::None => return,
})
}
use glob::{glob, Paths, PatternError}; use glob::{glob, Paths, PatternError};
let hashstr : String = hash.into(); let hashstr : String = hash.into();
let globstr = format!("{}/*-{}.imag", self.storepath, hashstr); let globstr = format!("{}/*-{}.imag", self.storepath, hashstr);
debug!("glob({})", globstr);
let globs = glob(&globstr[..]); glob(&globstr[..]).map(|paths| {
if globs.is_err() { // We assume there is only one ... TODO: Check whether there is actually just one
return None; paths.last().map(|path| {
if let Ok(pathbuf) = path {
let fname = pathbuf.file_name().and_then(|s| s.to_str());
fname.map(|s| {
FileID::parse(&String::from(s)).map(|id| {
self.load_in_cache(m, parser, id).map(|file| {
return Some(file)
})
});
});
} }
})
let path = globs.unwrap().last(); });
debug!("path = {:?}", path); None
let pathbuf = try_some!(path);
if pathbuf.is_err() { return None; }
let pathbuf_un = pathbuf.unwrap();
let filename = pathbuf_un.file_name();
let s = try_some!(filename).to_str();
let string = String::from(try_some!(s));
let id = try_some!(FileID::parse(&string));
debug!("Loaded ID = '{:?}'", id);
self.load_in_cache(m, parser, id)
.map(|file| {
debug!("Loaded File = '{:?}'", file);
Some(file)
}).unwrap_or(None)
} }
pub fn remove(&self, id: FileID) -> bool { pub fn remove(&self, id: FileID) -> bool {