Auto merge of #125 - TheNeikos:add-store_utils, r=matthiasbeyer

Add store utils

Do not merge yet!!
This commit is contained in:
Homu 2016-01-22 10:31:02 -08:00
commit 2e77afbb3c
6 changed files with 103 additions and 76 deletions

View file

@ -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"

View file

@ -8,3 +8,4 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
fs2 = "0.2.2"
toml = "0.1.25"
tempdir = "0.3.4"
semver = "0.2"

View file

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

View file

@ -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;

View file

@ -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<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)]
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());
}
}

View file

@ -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<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");
}
}