imag/libimagstore/src/storeid.rs

228 lines
5.5 KiB
Rust
Raw Normal View History

use std::path::PathBuf;
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;
use std::path::Components;
2016-08-24 13:55:26 +00:00
use libimagerror::into::IntoError;
use error::StoreErrorKind as SEK;
use store::Result;
use store::Store;
/// The Index into the Store
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
2016-08-07 14:45:41 +00:00
pub struct StoreId {
2016-08-07 14:53:33 +00:00
base: Option<PathBuf>,
id: PathBuf,
2016-08-07 14:45:41 +00:00
}
impl StoreId {
pub fn new(base: Option<PathBuf>, id: PathBuf) -> Result<StoreId> {
2016-08-25 12:29:15 +00:00
StoreId::new_baseless(id).map(|mut sid| { sid.base = base; sid })
}
pub fn new_baseless(id: PathBuf) -> Result<StoreId> {
if id.is_absolute() {
Err(SEK::StoreIdLocalPartAbsoluteError.into_error())
} else {
Ok(StoreId {
2016-08-25 12:29:15 +00:00
base: None,
id: id
})
}
2016-08-07 14:54:36 +00:00
}
2016-08-25 14:17:23 +00:00
pub fn without_base(mut self) -> StoreId {
self.base = None;
self
}
2016-08-25 15:54:14 +00:00
pub fn with_base(mut self, base: PathBuf) -> Self {
self.base = Some(base);
self
}
pub fn storified(self, store: &Store) -> StoreId {
2016-08-22 10:03:31 +00:00
StoreId {
base: Some(store.path().clone()),
id: self.id
}
}
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-08-24 13:55:26 +00:00
pub fn to_str(&self) -> Result<String> {
if self.base.is_some() {
let mut base = self.base.as_ref().cloned().unwrap();
base.push(self.id.clone());
base
} else {
self.id.clone()
}
.to_str()
.map(String::from)
.ok_or(SEK::StoreIdHandlingError.into_error())
}
/// Returns the components of the `id` part of the StoreId object.
///
/// Can be used to check whether a StoreId points to an entry in a specific collection of
/// StoreIds.
pub fn components(&self) -> Components {
self.id.components()
}
/// Get the _local_ part of a StoreId object, as in "the part from the store root to the entry".
pub fn local(&self) -> &PathBuf {
&self.id
}
}
impl Into<PathBuf> for StoreId {
fn into(self) -> PathBuf {
2016-08-07 14:53:33 +00:00
let mut base = self.base.unwrap_or(PathBuf::from("/"));
2016-08-07 14:46:21 +00:00
base.push(self.id);
base
}
}
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
}
}
}
/// 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) -> Result<StoreId>;
}
2016-05-03 13:45:06 +00:00
impl IntoStoreId for StoreId {
fn into_storeid(self) -> Result<StoreId> {
Ok(self)
2016-05-03 13:45:06 +00:00
}
}
impl IntoStoreId for PathBuf {
fn into_storeid(self) -> Result<StoreId> {
StoreId::new_baseless(self)
}
}
#[macro_export]
macro_rules! module_entry_path_mod {
($name: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 std::convert::AsRef;
use std::path::Path;
use std::path::PathBuf;
2016-05-03 12:53:14 +00:00
use $crate::storeid::StoreId;
use $crate::store::Result;
2016-05-03 12:53:14 +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();
path.push(format!("{}", $name));
path.push(pa.as_ref().clone());
let name = pa.as_ref().file_name().unwrap()
.to_str().unwrap();
2016-08-30 08:52:00 +00:00
path.set_file_name(name);
ModuleEntryPath(path)
}
}
impl $crate::storeid::IntoStoreId for ModuleEntryPath {
fn into_storeid(self) -> Result<$crate::storeid::StoreId> {
2016-08-07 14:54:48 +00:00
StoreId::new(None, self.0)
}
}
}
)
}
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 {
pub fn new(iter: Box<Iterator<Item = StoreId>>) -> StoreIdIterator {
2016-01-24 11:17:25 +00:00
StoreIdIterator {
iter: iter,
2016-01-24 11:17:25 +00:00
}
}
}
impl Iterator for StoreIdIterator {
type Item = StoreId;
fn next(&mut self) -> Option<StoreId> {
self.iter.next()
2016-01-24 11:17:25 +00:00
}
}
#[cfg(test)]
mod test {
use storeid::IntoStoreId;
module_entry_path_mod!("test");
#[test]
fn correct_path() {
let p = module_path::ModuleEntryPath::new("test");
2016-08-30 08:52:00 +00:00
assert_eq!(p.into_storeid().unwrap().to_str().unwrap(), "test/test");
}
}