2016-01-19 18:00:35 +00:00
|
|
|
use std::path::PathBuf;
|
2016-04-14 11:20:35 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::borrow::Borrow;
|
2016-05-03 12:45:33 +00:00
|
|
|
use std::ops::Deref;
|
2016-04-14 11:20:35 +00:00
|
|
|
|
2016-03-13 19:56:23 +00:00
|
|
|
use semver::Version;
|
2016-06-30 09:02:58 +00:00
|
|
|
use std::fmt::{Display, Debug, Formatter};
|
2016-04-18 16:40:59 +00:00
|
|
|
use std::fmt::Error as FmtError;
|
|
|
|
use std::result::Result as RResult;
|
2016-03-13 19:56:23 +00:00
|
|
|
|
2016-05-26 20:19:29 +00:00
|
|
|
use error::StoreErrorKind as SEK;
|
2016-03-13 19:56:23 +00:00
|
|
|
use store::Result;
|
|
|
|
use store::Store;
|
2016-01-19 18:00:35 +00:00
|
|
|
|
|
|
|
/// The Index into the Store
|
2016-04-14 11:20:35 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
|
2016-08-07 14:45:41 +00:00
|
|
|
pub struct StoreId {
|
|
|
|
store_location: PathBuf,
|
|
|
|
id: PathBuf,
|
|
|
|
}
|
2016-04-14 11:20:35 +00:00
|
|
|
|
2016-07-02 15:52:12 +00:00
|
|
|
impl StoreId {
|
|
|
|
|
|
|
|
pub fn storified(self, store: &Store) -> StoreId {
|
2016-07-04 10:49:12 +00:00
|
|
|
if self.starts_with(store.path()) {
|
|
|
|
debug!("Not storifying {:?}, because it is already.", self);
|
|
|
|
self
|
|
|
|
} else {
|
2016-08-07 14:46:02 +00:00
|
|
|
debug!("Create new store id out of: {:?} and {:?}", store.path(), self.id);
|
|
|
|
|
|
|
|
let new_id = StoreId { store_location: store.path().clone(), self.id };
|
|
|
|
|
2016-07-04 10:49:12 +00:00
|
|
|
debug!("Created: '{:?}'", new_id);
|
2016-08-07 14:46:02 +00:00
|
|
|
new_id
|
2016-07-04 10:49:12 +00:00
|
|
|
}
|
2016-07-02 15:52:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 14:48:40 +00:00
|
|
|
pub fn exists(&self) -> bool {
|
|
|
|
let pb : PathBuf = self.clone().into();
|
|
|
|
pb.exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_file(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_dir(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2016-07-02 15:52:12 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 11:20:35 +00:00
|
|
|
impl Into<PathBuf> for StoreId {
|
|
|
|
|
|
|
|
fn into(self) -> PathBuf {
|
2016-08-07 14:46:21 +00:00
|
|
|
let mut base = self.store_location;
|
|
|
|
base.push(self.id);
|
|
|
|
base
|
2016-04-14 11:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-30 09:02:58 +00:00
|
|
|
impl Display for StoreId {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
|
2016-08-07 14:46:31 +00:00
|
|
|
match self.id.to_str() {
|
2016-06-30 09:02:58 +00:00
|
|
|
Some(s) => write!(fmt, "{}", s),
|
2016-08-07 14:46:31 +00:00
|
|
|
None => write!(fmt, "{}", self.id.to_string_lossy()),
|
2016-06-30 09:02:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-14 11:20:35 +00:00
|
|
|
impl From<PathBuf> for StoreId {
|
|
|
|
|
|
|
|
fn from(pb: PathBuf) -> StoreId {
|
|
|
|
StoreId(pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for StoreId {
|
|
|
|
|
|
|
|
fn from(string: String) -> StoreId {
|
|
|
|
StoreId(string.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Path> for StoreId {
|
|
|
|
|
|
|
|
fn as_ref(&self) -> &Path {
|
2016-08-07 14:49:04 +00:00
|
|
|
self.id.as_ref()
|
2016-04-14 11:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:00:35 +00:00
|
|
|
/// This Trait allows you to convert various representations to a single one
|
|
|
|
/// suitable for usage in the Store
|
2016-01-29 16:00:29 +00:00
|
|
|
pub trait IntoStoreId {
|
2016-01-19 18:00:35 +00:00
|
|
|
fn into_storeid(self) -> StoreId;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoStoreId for PathBuf {
|
|
|
|
fn into_storeid(self) -> StoreId {
|
2016-05-03 12:36:24 +00:00
|
|
|
StoreId(self)
|
2016-01-19 18:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 13:45:06 +00:00
|
|
|
impl IntoStoreId for StoreId {
|
|
|
|
fn into_storeid(self) -> StoreId {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 19:56:23 +00:00
|
|
|
pub fn build_entry_path(store: &Store, path_elem: &str) -> Result<PathBuf> {
|
2016-03-13 19:49:26 +00:00
|
|
|
debug!("Checking path element for version");
|
2016-05-03 21:10:32 +00:00
|
|
|
if path_elem.split('~').last().map_or(false, |v| Version::parse(v).is_err()) {
|
2016-03-13 19:56:23 +00:00
|
|
|
debug!("Version cannot be parsed from {:?}", path_elem);
|
|
|
|
debug!("Path does not contain version!");
|
2016-05-26 20:19:29 +00:00
|
|
|
return Err(SEK::StorePathLacksVersion.into());
|
2016-03-13 19:49:26 +00:00
|
|
|
}
|
|
|
|
debug!("Version checking succeeded");
|
|
|
|
|
|
|
|
debug!("Building path from {:?}", path_elem);
|
2016-03-13 19:56:23 +00:00
|
|
|
let mut path = store.path().clone();
|
2016-03-13 19:49:26 +00:00
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
if path_elem.starts_with('/') {
|
|
|
|
path.push(&path_elem[1..]);
|
2016-03-13 19:49:26 +00:00
|
|
|
} else {
|
|
|
|
path.push(path_elem);
|
|
|
|
}
|
|
|
|
|
2016-03-13 19:56:23 +00:00
|
|
|
Ok(path)
|
2016-03-13 19:49:26 +00:00
|
|
|
}
|
2016-01-19 18:00:35 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! module_entry_path_mod {
|
|
|
|
($name:expr, $version:expr) => (
|
2016-01-22 17:22:01 +00:00
|
|
|
#[deny(missing_docs,
|
|
|
|
missing_copy_implementations,
|
|
|
|
trivial_casts, trivial_numeric_casts,
|
|
|
|
unsafe_code,
|
|
|
|
unstable_features,
|
|
|
|
unused_import_braces, unused_qualifications,
|
|
|
|
unused_imports)]
|
2016-01-29 16:02:35 +00:00
|
|
|
/// A helper module to create valid module entry paths
|
2016-01-19 18:00:35 +00:00
|
|
|
pub mod module_path {
|
|
|
|
use semver::Version;
|
|
|
|
use std::convert::AsRef;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2016-05-03 12:53:14 +00:00
|
|
|
use $crate::storeid::StoreId;
|
|
|
|
|
2016-01-19 18:00:35 +00:00
|
|
|
/// 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();
|
2016-03-10 17:44:24 +00:00
|
|
|
path.push(format!("{}", $name));
|
2016-01-19 18:00:35 +00:00
|
|
|
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 {
|
2016-03-21 18:40:19 +00:00
|
|
|
fn into_storeid(self) -> $crate::storeid::StoreId {
|
2016-05-03 12:53:14 +00:00
|
|
|
StoreId::from(self.0)
|
2016-01-19 18:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:21:04 +00:00
|
|
|
pub struct StoreIdIterator {
|
|
|
|
iter: Box<Iterator<Item = StoreId>>,
|
|
|
|
}
|
|
|
|
|
2016-04-18 16:40:59 +00:00
|
|
|
impl Debug for StoreIdIterator {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
|
|
|
|
write!(fmt, "StoreIdIterator")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 11:17:25 +00:00
|
|
|
impl StoreIdIterator {
|
|
|
|
|
2016-05-12 15:21:04 +00:00
|
|
|
pub fn new(iter: Box<Iterator<Item = StoreId>>) -> StoreIdIterator {
|
2016-01-24 11:17:25 +00:00
|
|
|
StoreIdIterator {
|
2016-05-12 15:21:04 +00:00
|
|
|
iter: iter,
|
2016-01-24 11:17:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for StoreIdIterator {
|
|
|
|
type Item = StoreId;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<StoreId> {
|
2016-05-12 15:21:04 +00:00
|
|
|
self.iter.next()
|
2016-01-24 11:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:00:35 +00:00
|
|
|
#[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");
|
|
|
|
|
2016-03-10 18:12:00 +00:00
|
|
|
assert_eq!(p.into_storeid().to_str().unwrap(), "test/test~0.2.0-alpha+leet1337");
|
2016-01-19 18:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|