diff --git a/libimagstore/Cargo.lock b/libimagstore/Cargo.lock index 6eb4a715..572560d8 100644 --- a/libimagstore/Cargo.lock +++ b/libimagstore/Cargo.lock @@ -3,6 +3,7 @@ name = "libimagstore" version = "0.1.0" dependencies = [ "fs2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -22,7 +23,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -37,7 +38,12 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.4" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nom" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -46,7 +52,7 @@ version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -55,6 +61,14 @@ name = "rustc-serialize" version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "semver" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempdir" version = "0.3.4" diff --git a/libimagstore/Cargo.toml b/libimagstore/Cargo.toml index 6285e2d9..c9f4aacd 100644 --- a/libimagstore/Cargo.toml +++ b/libimagstore/Cargo.toml @@ -8,3 +8,4 @@ authors = ["Matthias Beyer "] fs2 = "0.2.2" toml = "0.1.25" tempdir = "0.3.4" +semver = "0.2" diff --git a/libimagstore/src/entry.rs b/libimagstore/src/entry.rs index 27ef3dcb..b463a81e 100644 --- a/libimagstore/src/entry.rs +++ b/libimagstore/src/entry.rs @@ -1,6 +1,6 @@ use header::EntryHeader; use content::EntryContent; -use store::StoreId; +use storeid::StoreId; /** * An Entry of the store diff --git a/libimagstore/src/lib.rs b/libimagstore/src/lib.rs index 30a7f999..2c62c0f5 100644 --- a/libimagstore/src/lib.rs +++ b/libimagstore/src/lib.rs @@ -1,7 +1,9 @@ extern crate fs2; extern crate toml; extern crate tempdir; +extern crate semver; +pub mod storeid; pub mod content; pub mod entry; pub mod error; diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index a95f20b5..89e8ea89 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -10,58 +10,11 @@ use fs2::FileExt; use entry::Entry; use error::{StoreError, StoreErrorKind}; +use storeid::StoreId; /// The Result Type returned by any interaction with the store that could fail pub type Result = RResult; -/// The Index into the Store -pub type StoreId = PathBuf; - -/// This Trait allows you to convert various representations to a single one -/// suitable for usage in the Store -trait IntoStoreId { - fn into_storeid(self) -> StoreId; -} - -impl<'a> IntoStoreId for &'a str { - fn into_storeid(self) -> StoreId { - PathBuf::from(self) - } -} - -impl<'a> IntoStoreId for &'a String{ - fn into_storeid(self) -> StoreId { - PathBuf::from(self) - } -} - -impl IntoStoreId for String{ - fn into_storeid(self) -> StoreId { - PathBuf::from(self) - } -} - -impl IntoStoreId for PathBuf { - fn into_storeid(self) -> StoreId { - self - } -} - -impl<'a> IntoStoreId for &'a PathBuf { - fn into_storeid(self) -> StoreId { - self.clone() - } -} - -impl IntoStoreId for (ISI, ISI) { - fn into_storeid(self) -> StoreId { - let (first, second) = self; - let mut res : StoreId = first.into_storeid(); - res.push(second.into_storeid()); - res - } -} - #[derive(PartialEq)] enum StoreEntryPresence { Present, @@ -205,27 +158,3 @@ impl<'a> Drop for FileLockEntry<'a> { self.store._update(self).unwrap() } } - -#[cfg(test)] -mod test { - use std::path::PathBuf; - use store::{StoreId, IntoStoreId}; - - #[test] - fn into_storeid_trait() { - let buf = PathBuf::from("abc/def"); - - let test = ("abc", "def"); - assert_eq!(buf, test.into_storeid()); - - let test = "abc/def"; - assert_eq!(buf, test.into_storeid()); - - let test = String::from("abc/def"); - assert_eq!(buf, test.into_storeid()); - - let test = PathBuf::from("abc/def"); - assert_eq!(buf, test.into_storeid()); - } -} - diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs new file mode 100644 index 00000000..cc826585 --- /dev/null +++ b/libimagstore/src/storeid.rs @@ -0,0 +1,81 @@ +use std::path::PathBuf; + +/// The Index into the Store +pub type StoreId = PathBuf; + +/// This Trait allows you to convert various representations to a single one +/// suitable for usage in the Store +trait IntoStoreId { + fn into_storeid(self) -> StoreId; +} + +impl IntoStoreId for PathBuf { + fn into_storeid(self) -> StoreId { + self + } +} + + +#[macro_export] +macro_rules! module_entry_path_mod { + ($name:expr, $version:expr) => ( + #[deny(missing_docs, + missing_copy_implementations, + trivial_casts, trivial_numeric_casts, + unsafe_code, + unstable_features, + unused_import_braces, unused_qualifications, + unused_imports)] + pub mod module_path { + use semver::Version; + use std::convert::AsRef; + use std::path::Path; + use std::path::PathBuf; + + /// A Struct giving you the ability to choose store entries assigned + /// to it. + /// + /// It is created through a call to `new`. + pub struct ModuleEntryPath(PathBuf); + + impl ModuleEntryPath { + /// Path has to be a valid UTF-8 string or this will panic! + pub fn new>(pa: P) -> ModuleEntryPath { + let mut path = PathBuf::new(); + path.push(format!("/{}", $name)); + path.push(pa.as_ref().clone()); + let version = Version::parse($version).unwrap(); + let name = pa.as_ref().file_name().unwrap() + .to_str().unwrap(); + path.set_file_name(format!("{}~{}", + name, + version)); + ModuleEntryPath(path) + } + } + + impl $crate::storeid::IntoStoreId for ModuleEntryPath { + fn into_storeid(mut self) -> $crate::storeid::StoreId { + self.0 + } + } + } + ) +} + +#[cfg(test)] +mod test { + + use storeid::IntoStoreId; + + module_entry_path_mod!("test", "0.2.0-alpha+leet1337"); + + #[test] + fn correct_path() { + let p = module_path::ModuleEntryPath::new("test"); + + assert_eq!(p.into_storeid().to_str().unwrap(), + "/test/test~0.2.0-alpha+leet1337"); + } + +}