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
This commit is contained in:
Marcel Müller 2016-01-19 19:00:35 +01:00
parent a85d658f33
commit 560e7106f8
4 changed files with 78 additions and 73 deletions

View file

@ -1,6 +1,6 @@
use header::EntryHeader; use header::EntryHeader;
use content::EntryContent; use content::EntryContent;
use store::StoreId; use storeid::StoreId;
/** /**
* An Entry of the store * An Entry of the store

View file

@ -1,7 +1,9 @@
extern crate fs2; extern crate fs2;
extern crate toml; extern crate toml;
extern crate tempdir; extern crate tempdir;
extern crate semver;
pub mod storeid;
pub mod content; pub mod content;
pub mod entry; pub mod entry;
pub mod error; pub mod error;

View file

@ -10,58 +10,11 @@ use fs2::FileExt;
use entry::Entry; use entry::Entry;
use error::{StoreError, StoreErrorKind}; use error::{StoreError, StoreErrorKind};
use storeid::StoreId;
/// The Result Type returned by any interaction with the store that could fail /// The Result Type returned by any interaction with the store that could fail
pub type Result<T> = RResult<T, StoreError>; pub type Result<T> = RResult<T, StoreError>;
/// 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<ISI: IntoStoreId> 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)] #[derive(PartialEq)]
enum StoreEntryPresence { enum StoreEntryPresence {
Present, Present,
@ -220,27 +173,3 @@ impl<'a> Drop for FileLockEntry<'a> {
self.store._update(self).unwrap() 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());
}
}

View file

@ -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<P: AsRef<Path>>(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");
}
}