imag/libimagstore/src/storeid.rs

135 lines
3.6 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
2016-01-24 11:17:41 +00:00
use glob::Paths;
/// 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
2016-01-29 16:00:29 +00:00
pub trait IntoStoreId {
fn into_storeid(self) -> StoreId;
}
impl IntoStoreId for PathBuf {
fn into_storeid(self) -> StoreId {
self
}
}
pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf {
debug!("Checking path element for version");
{
let contains_version = {
path_elem.split("~")
.last()
.map(|version| Version::parse(version).is_ok())
.unwrap_or(false)
};
if !contains_version {
debug!("Version cannot be parsed inside {:?}", path_elem);
warn!("Path does not contain version. Will panic now!");
panic!("No version in path");
}
}
debug!("Version checking succeeded");
debug!("Building path from {:?}", path_elem);
let mut path = rt.store().path().clone();
if path_elem.chars().next() == Some('/') {
path.push(&path_elem[1..path_elem.len()]);
} else {
path.push(path_elem);
}
path
}
#[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)]
/// A helper module to create valid module entry paths
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
}
}
}
)
}
2016-01-24 11:17:41 +00:00
pub struct StoreIdIterator {
paths: Paths,
2016-01-24 11:17:25 +00:00
}
impl StoreIdIterator {
2016-01-24 11:17:41 +00:00
pub fn new(paths: Paths) -> StoreIdIterator {
2016-01-24 11:17:25 +00:00
StoreIdIterator {
2016-01-24 11:17:41 +00:00
paths: paths,
2016-01-24 11:17:25 +00:00
}
}
}
impl Iterator for StoreIdIterator {
type Item = StoreId;
fn next(&mut self) -> Option<StoreId> {
2016-01-24 11:17:41 +00:00
self.paths.next().and_then(|o| o.ok())
2016-01-24 11:17:25 +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");
assert_eq!(p.into_storeid().to_str().unwrap(), "test/test~0.2.0-alpha+leet1337");
}
}