Remove StoreId::is_in_collection(), add StoreId::local()

Having a ::is_in_collection() is a nice thing, though it is _way_ better
if we just give the user of the `StoreId` object access to the local
part of the ID.

Using this new function, one can do all the actions one might need on
the Path for the actual entry without nasty copying or such.

`StoreId::is_in_collection()` can be replaced by
`StoreId::local().starts_with()` and everything is fine, as we do not
have to move a `PathBuf` object into the function anymore.
This commit is contained in:
Matthias Beyer 2016-09-04 10:40:38 +02:00
parent 253c8a6f54
commit ccffeb91a2
3 changed files with 5 additions and 31 deletions

View file

@ -18,8 +18,7 @@ impl IsInDiary for Entry {
impl IsInDiary for StoreId { impl IsInDiary for StoreId {
fn is_in_diary(&self, name: &str) -> bool { fn is_in_diary(&self, name: &str) -> bool {
use std::path::PathBuf; self.local().starts_with(format!("diary/{}", name))
self.is_in_collection(&PathBuf::from(format!("diary/{}", name)))
} }
} }

View file

@ -91,10 +91,8 @@ pub trait ExternalLinker : InternalLinker {
/// Check whether the StoreId starts with `/link/external/` /// Check whether the StoreId starts with `/link/external/`
pub fn is_external_link_storeid(id: &StoreId) -> bool { pub fn is_external_link_storeid(id: &StoreId) -> bool {
use std::path::PathBuf;
debug!("Checking whether this is a link/external/*: '{:?}'", id); debug!("Checking whether this is a link/external/*: '{:?}'", id);
id.is_in_collection(&PathBuf::from("link/external")) id.local().starts_with("link/external")
} }
fn get_external_link_from_file(entry: &FileLockEntry) -> Result<Url> { fn get_external_link_from_file(entry: &FileLockEntry) -> Result<Url> {

View file

@ -86,11 +86,9 @@ impl StoreId {
self.id.components() self.id.components()
} }
/// Convenience function over PathBuf::starts_with(). /// Get the _local_ part of a StoreId object, as in "the part from the store root to the entry".
/// pub fn local(&self) -> &PathBuf {
/// This function calls `PathBuf::starts_with()` on the _local_ part of `self`. &self.id
pub fn is_in_collection(&self, collspec: &PathBuf) -> bool {
self.id.starts_with(collspec)
} }
} }
@ -226,25 +224,4 @@ mod test {
assert_eq!(p.into_storeid().unwrap().to_str().unwrap(), "test/test"); assert_eq!(p.into_storeid().unwrap().to_str().unwrap(), "test/test");
} }
#[test]
fn storeid_in_collection() {
use std::path::PathBuf;
let p = module_path::ModuleEntryPath::new("1/2/3/4/5/6/7/8/9/0").into_storeid().unwrap();
assert!(p.is_in_collection(&PathBuf::from("test/1")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4/5")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4/5/6")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4/5/6/7")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4/5/6/7/8")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4/5/6/7/8/9")));
assert!(p.is_in_collection(&PathBuf::from("test/1/2/3/4/5/6/7/8/9/0")));
assert!(!p.is_in_collection(&PathBuf::from("test/0/2/3/4/5/6/7/8/9/0")));
assert!(!p.is_in_collection(&PathBuf::from("test/1/2/3/4/5/6/8")));
assert!(!p.is_in_collection(&PathBuf::from("test/1/2/3/leet/5/6/7")));
}
} }