From 560e7106f8a451e97083eda10f8e1e1c971f553c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Tue, 19 Jan 2016 19:00:35 +0100 Subject: [PATCH] iQIcBAABCgAGBQJWomPqAAoJEN1O030MrHbiYEEQALGQfJYEfbCgvVejgiwOKMjv m7U7nEQbMMVYHVbeCdUs2PXHwCFr8slSccyWoF0QNHX4/Tx5t+XJ9JnRsyIhQyzi UWRQo3mHDErn2lP+M+4kfDWPpOdJicJKAxyiLgGo/Aw5CblX9MAaKfE85rZh7atx JM567tN+O/Cflt676Mva3xnzpisHFKVJKG58k+QzmZOgNtROWg3PTyAb+/T2c6Vd Ty+zW4zt7LwKjGlK9YEDqEwpg3STKUPT9T5S6ZFkPHBbjPVtM5yXK1oTWOpE7zn+ iXSjCqfC2Ok+MT9YB1loexY2f6bzwqZkmWHl3M+4f1fJfhBQR0Zdz4a/hyp2YLcv hVW/X/AeVCdoMOBZ9PCaFjY6aXFycB6HlDDU1jldCN5U08BfY+h6hDtW5xaPLj2s WWeyIZgnqLJsGk7VK+/KAdDZMAZVwoC69AVXQS+IjejetQWHY5OFhCBfRVZ02/aX jwdZSfw5oNCGsvrzknkcECL9bPQAGRptoLbGuuLNPaACNd8ebWTmw4gwxFYwwAGY Dq6NDA33S3uRDvK24MpUA8g5OxsU8kzblhP6iYqGTg0dLnGFRzioKDg3v7NFKN9/ Yy7g1uQCZIVbrVnZF6w68jfuX6IoIHWl6eg6De2AmMNClYGZflDzbSoYCZkiz1d4 AR6HHXLT84nAJ2tCgK0S =6TMR -----END PGP SIGNATURE----- Add macro to create ModuleEntryPath --- libimagstore/src/entry.rs | 2 +- libimagstore/src/lib.rs | 2 + libimagstore/src/store.rs | 73 +----------------------------------- libimagstore/src/storeid.rs | 74 +++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 73 deletions(-) create mode 100644 libimagstore/src/storeid.rs 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 dddfd302..cf7226d7 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, @@ -220,27 +173,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..132e94bd --- /dev/null +++ b/libimagstore/src/storeid.rs @@ -0,0 +1,74 @@ +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) => ( + 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"); + } + +}