diff --git a/src/storage/file/mod.rs b/src/storage/file/mod.rs index 9a11abb6..b23c2ed0 100644 --- a/src/storage/file/mod.rs +++ b/src/storage/file/mod.rs @@ -5,6 +5,7 @@ use std::fmt; use regex::Regex; pub mod id; +pub mod id_type; pub mod header; pub mod hash; diff --git a/src/storage/path.rs b/src/storage/path.rs new file mode 100644 index 00000000..3d775eca --- /dev/null +++ b/src/storage/path.rs @@ -0,0 +1,102 @@ +use std::path::PathBuf; + +use glob::glob; +use glob::Paths; +use glob::PatternError; + +use storage::file::id::FileID; +use storage::file::id_type::FileIDType; +use storage::file::hash::FileHash; + +/* + * A path represents either a GLOB ("/tmp/store/module-*-*.imag" for example) or a full path + * + * It can be used to generate a File or iterate over some files + * + */ +struct Path<'a> { + + /* + * The base part ("/tmp/") + */ + base: PathBuf, + + /* + * The store part ("/store/") + */ + store: PathBuf, + + /* + * The module + */ + module: &'a Module, + + /* + * The ID + */ + idtype: Option, + idhash: Option, + id: Option, + +} + +impl<'a> Path<'a> { + + fn new(base: PathBuf, store: PathBuf, m: &Module, id: FileID) -> Path { + Path { + base: base, + store: store, + module: m, + idtype: Some(id.get_type()), + idhash: Some(id.get_id()), + id: Some(id), + } + } + + fn new_with_idtype(base: PathBuf, store: PathBuf, m: &Module, id: FileIDType) -> Path { + Path { + base: base, + store: store, + module: m, + idtype: Some(id), + idhash: None, + id: None, + } + } + + fn new_with_idhash(base: PathBuf, store: PathBuf, m: &Module, id: FileHash) -> Path { + Path { + base: base, + store: store, + module: m, + idtype: None, + idhash: Some(id), + id: None, + } + } + +} + +/* + * Transform Path into str, so we can call glob() on it + */ +impl<'a> Into<&'a str> for Path<'a> { + + fn into(self) -> &'a str { + let s = self.base.clone(); + s.push(self.store.clone()); + s.push(self.module.name()); + s.push(self.id.into()); + s.set_extension("imag"); + s.to_str().unwrap_or("") + } +} + +impl<'a> Into> for Path<'a> { + + fn into(self) -> Result { + let s : &str = self.into(); + glob(s) + } + +}