Merge pull request #339 from matthiasbeyer/libimagstore/storeid-no-typedef
Remove typedef for StoreId type
This commit is contained in:
commit
3e792ad024
3 changed files with 80 additions and 18 deletions
|
@ -32,7 +32,7 @@ impl Lister for PathLister {
|
||||||
if self.absolute {
|
if self.absolute {
|
||||||
pb.canonicalize().map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
|
pb.canonicalize().map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
|
||||||
} else {
|
} else {
|
||||||
Ok(pb)
|
Ok(pb.into())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.and_then(|pb| {
|
.and_then(|pb| {
|
||||||
|
|
|
@ -23,7 +23,7 @@ use glob::glob;
|
||||||
|
|
||||||
use error::{ParserErrorKind, ParserError};
|
use error::{ParserErrorKind, ParserError};
|
||||||
use error::{StoreError, StoreErrorKind};
|
use error::{StoreError, StoreErrorKind};
|
||||||
use storeid::{StoreId, StoreIdIterator};
|
use storeid::{IntoStoreId, StoreId, StoreIdIterator};
|
||||||
use lazyfile::LazyFile;
|
use lazyfile::LazyFile;
|
||||||
|
|
||||||
use hook::aspect::Aspect;
|
use hook::aspect::Aspect;
|
||||||
|
@ -57,7 +57,7 @@ impl StoreEntry {
|
||||||
fn new(id: StoreId) -> StoreEntry {
|
fn new(id: StoreId) -> StoreEntry {
|
||||||
StoreEntry {
|
StoreEntry {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
file: LazyFile::Absent(id),
|
file: LazyFile::Absent(id.into()),
|
||||||
status: StoreEntryStatus::Present,
|
status: StoreEntryStatus::Present,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,12 +241,12 @@ impl Store {
|
||||||
let mut new_id = self.location.clone();
|
let mut new_id = self.location.clone();
|
||||||
new_id.push(id);
|
new_id.push(id);
|
||||||
debug!("Created: '{:?}'", new_id);
|
debug!("Created: '{:?}'", new_id);
|
||||||
new_id
|
StoreId::from(new_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the Entry at the given location (inside the entry)
|
/// Creates the Entry at the given location (inside the entry)
|
||||||
pub fn create<'a>(&'a self, id: StoreId) -> Result<FileLockEntry<'a>> {
|
pub fn create<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
|
||||||
let id = self.storify_id(id);
|
let id = self.storify_id(id.into_storeid());
|
||||||
if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) {
|
if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) {
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
|
@ -273,8 +273,8 @@ impl Store {
|
||||||
|
|
||||||
/// Borrow a given Entry. When the `FileLockEntry` is either `update`d or
|
/// Borrow a given Entry. When the `FileLockEntry` is either `update`d or
|
||||||
/// dropped, the new Entry is written to disk
|
/// dropped, the new Entry is written to disk
|
||||||
pub fn retrieve<'a>(&'a self, id: StoreId) -> Result<FileLockEntry<'a>> {
|
pub fn retrieve<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
|
||||||
let id = self.storify_id(id);
|
let id = self.storify_id(id.into_storeid());
|
||||||
if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) {
|
if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) {
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
|
@ -356,8 +356,8 @@ impl Store {
|
||||||
|
|
||||||
/// Retrieve a copy of a given entry, this cannot be used to mutate
|
/// Retrieve a copy of a given entry, this cannot be used to mutate
|
||||||
/// the one on disk
|
/// the one on disk
|
||||||
pub fn retrieve_copy(&self, id: StoreId) -> Result<Entry> {
|
pub fn retrieve_copy<S: IntoStoreId>(&self, id: S) -> Result<Entry> {
|
||||||
let id = self.storify_id(id);
|
let id = self.storify_id(id.into_storeid());
|
||||||
let entries_lock = self.entries.write();
|
let entries_lock = self.entries.write();
|
||||||
if entries_lock.is_err() {
|
if entries_lock.is_err() {
|
||||||
return Err(StoreError::new(StoreErrorKind::LockPoisoned, None))
|
return Err(StoreError::new(StoreErrorKind::LockPoisoned, None))
|
||||||
|
@ -374,8 +374,8 @@ impl Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete an entry
|
/// Delete an entry
|
||||||
pub fn delete(&self, id: StoreId) -> Result<()> {
|
pub fn delete<S: IntoStoreId>(&self, id: S) -> Result<()> {
|
||||||
let id = self.storify_id(id);
|
let id = self.storify_id(id.into_storeid());
|
||||||
if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) {
|
if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) {
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
|
@ -1119,7 +1119,7 @@ impl Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_file(loc: StoreId, file: &mut File) -> Result<Entry> {
|
pub fn from_file<S: IntoStoreId>(loc: S, file: &mut File) -> Result<Entry> {
|
||||||
let text = {
|
let text = {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
|
@ -1129,7 +1129,7 @@ impl Entry {
|
||||||
Self::from_str(loc, &text[..])
|
Self::from_str(loc, &text[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(loc: StoreId, s: &str) -> Result<Entry> {
|
pub fn from_str<S: IntoStoreId>(loc: S, s: &str) -> Result<Entry> {
|
||||||
debug!("Building entry from string");
|
debug!("Building entry from string");
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref RE: Regex = Regex::new(r"(?smx)
|
static ref RE: Regex = Regex::new(r"(?smx)
|
||||||
|
@ -1157,7 +1157,7 @@ impl Entry {
|
||||||
|
|
||||||
debug!("Header and content found. Yay! Building Entry object now");
|
debug!("Header and content found. Yay! Building Entry object now");
|
||||||
Ok(Entry {
|
Ok(Entry {
|
||||||
location: loc,
|
location: loc.into_storeid(),
|
||||||
header: try!(EntryHeader::parse(header.unwrap())),
|
header: try!(EntryHeader::parse(header.unwrap())),
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
use glob::Paths;
|
use glob::Paths;
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
@ -10,7 +14,57 @@ use store::Result;
|
||||||
use store::Store;
|
use store::Store;
|
||||||
|
|
||||||
/// The Index into the Store
|
/// The Index into the Store
|
||||||
pub type StoreId = PathBuf;
|
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct StoreId(PathBuf);
|
||||||
|
|
||||||
|
impl Into<PathBuf> for StoreId {
|
||||||
|
|
||||||
|
fn into(self) -> PathBuf {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for StoreId {
|
||||||
|
type Target = PathBuf;
|
||||||
|
|
||||||
|
fn deref(&self) -> &PathBuf {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
self.0.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Borrow<Path> for StoreId {
|
||||||
|
|
||||||
|
fn borrow(&self) -> &Path {
|
||||||
|
self.0.borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// This Trait allows you to convert various representations to a single one
|
/// This Trait allows you to convert various representations to a single one
|
||||||
/// suitable for usage in the Store
|
/// suitable for usage in the Store
|
||||||
|
@ -19,6 +73,12 @@ pub trait IntoStoreId {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoStoreId for PathBuf {
|
impl IntoStoreId for PathBuf {
|
||||||
|
fn into_storeid(self) -> StoreId {
|
||||||
|
StoreId(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoStoreId for StoreId {
|
||||||
fn into_storeid(self) -> StoreId {
|
fn into_storeid(self) -> StoreId {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -62,6 +122,8 @@ macro_rules! module_entry_path_mod {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use $crate::storeid::StoreId;
|
||||||
|
|
||||||
/// A Struct giving you the ability to choose store entries assigned
|
/// A Struct giving you the ability to choose store entries assigned
|
||||||
/// to it.
|
/// to it.
|
||||||
///
|
///
|
||||||
|
@ -86,7 +148,7 @@ macro_rules! module_entry_path_mod {
|
||||||
|
|
||||||
impl $crate::storeid::IntoStoreId for ModuleEntryPath {
|
impl $crate::storeid::IntoStoreId for ModuleEntryPath {
|
||||||
fn into_storeid(self) -> $crate::storeid::StoreId {
|
fn into_storeid(self) -> $crate::storeid::StoreId {
|
||||||
self.0
|
StoreId::from(self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,7 +181,7 @@ impl Iterator for StoreIdIterator {
|
||||||
type Item = StoreId;
|
type Item = StoreId;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<StoreId> {
|
fn next(&mut self) -> Option<StoreId> {
|
||||||
self.paths.next().and_then(|o| o.ok())
|
self.paths.next().and_then(|o| o.ok()).map(|p| StoreId::from(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue