diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 0e3fc32f..cdb21a41 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -32,6 +32,7 @@ pub enum StoreErrorKind { HookExecutionError, PreHookExecuteError, PostHookExecuteError, + StorePathLacksVersion, // maybe more } @@ -60,6 +61,7 @@ fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { &StoreErrorKind::HookExecutionError => "Hook execution error", &StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error", &StoreErrorKind::PostHookExecuteError => "Post-Hook execution error", + &StoreErrorKind::StorePathLacksVersion => "The supplied store path has no version part", } } diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 9b6420e7..1a608565 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -1,5 +1,10 @@ use std::path::PathBuf; use glob::Paths; +use semver::Version; + +use error::{StoreError, StoreErrorKind}; +use store::Result; +use store::Store; /// The Index into the Store pub type StoreId = PathBuf; @@ -16,26 +21,17 @@ impl IntoStoreId for PathBuf { } } -pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf { +pub fn build_entry_path(store: &Store, path_elem: &str) -> Result { 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"); - } + if path_elem.split("~").last().map(|v| Version::parse(v).is_err()).unwrap_or(false) { + debug!("Version cannot be parsed from {:?}", path_elem); + debug!("Path does not contain version!"); + return Err(StoreError::new(StoreErrorKind::StorePathLacksVersion, None)); } debug!("Version checking succeeded"); debug!("Building path from {:?}", path_elem); - let mut path = rt.store().path().clone(); + let mut path = store.path().clone(); if path_elem.chars().next() == Some('/') { path.push(&path_elem[1..path_elem.len()]); @@ -43,7 +39,7 @@ pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf { path.push(path_elem); } - path + Ok(path) } #[macro_export]