From 8362b077e2b4269a1a32d76bc4d9e65985f71de8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 5 Sep 2016 18:21:34 +0200 Subject: [PATCH] Add StoreId::from_full_path() --- libimagstore/src/storeid.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 4bb26bf6..5cf2037a 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; +use std::path::Path; use std::path::PathBuf; use std::fmt::{Display, Debug, Formatter}; @@ -8,6 +10,7 @@ use std::path::Components; use libimagerror::into::IntoError; use error::StoreErrorKind as SEK; +use error::MapErrInto; use store::Result; use store::Store; @@ -24,6 +27,21 @@ impl StoreId { StoreId::new_baseless(id).map(|mut sid| { sid.base = base; sid }) } + /// Try to create a StoreId object from a filesystem-absolute path. + /// + /// Automatically creates a StoreId object which has a `base` set to `store_part` if stripping + /// the `store_part` from the `full_path` succeeded. + /// + /// Returns a `StoreErrorKind::StoreIdBuildFromFullPathError` if stripping failes. + pub fn from_full_path(store_part: &PathBuf, full_path: D) -> Result + where D: Deref + { + let p = try!( + full_path.strip_prefix(store_part).map_err_into(SEK::StoreIdBuildFromFullPathError) + ); + StoreId::new(Some(store_part.clone()), PathBuf::from(p)) + } + pub fn new_baseless(id: PathBuf) -> Result { if id.is_absolute() { Err(SEK::StoreIdLocalPartAbsoluteError.into_error())