Merge pull request #690 from matthiasbeyer/rewrite-storeid-type-general-cleanup

General cleanup for #667
This commit is contained in:
Matthias Beyer 2016-09-04 18:03:20 +02:00 committed by GitHub
commit 3a0739c1cf
3 changed files with 5 additions and 45 deletions

View file

@ -18,7 +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 {
self.is_in_collection(&["diary", name]) self.local().starts_with(format!("diary/{}", name))
} }
} }

View file

@ -92,7 +92,7 @@ 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 {
debug!("Checking whether this is a link/external/*: '{:?}'", id); debug!("Checking whether this is a link/external/*: '{:?}'", id);
id.is_in_collection(&["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,27 +86,9 @@ impl StoreId {
self.id.components() self.id.components()
} }
/// Check whether a StoreId points to an entry in a specific collection. /// Get the _local_ part of a StoreId object, as in "the part from the store root to the entry".
/// pub fn local(&self) -> &PathBuf {
/// A "collection" here is simply a directory. So `foo/bar/baz` is an entry which is in &self.id
/// collection ["foo", "bar"].
///
/// # Warning
///
/// The collection specification _has_ to start with the module name. Otherwise this function
/// may return false negatives.
///
pub fn is_in_collection(&self, colls: &[&str]) -> bool {
use std::path::Component;
self.id
.components()
.zip(colls)
.map(|(component, pred_coll)| match component {
Component::Normal(ref s) => s.to_str().map(|ref s| s == pred_coll).unwrap_or(false),
_ => false
})
.all(|x| x) && colls.last().map(|last| !self.id.ends_with(last)).unwrap_or(false)
} }
} }
@ -242,26 +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() {
let p = module_path::ModuleEntryPath::new("1/2/3/4/5/6/7/8/9/0").into_storeid().unwrap();
assert!(p.is_in_collection(&["test", "1"]));
assert!(p.is_in_collection(&["test", "1", "2"]));
assert!(p.is_in_collection(&["test", "1", "2", "3"]));
assert!(p.is_in_collection(&["test", "1", "2", "3", "4"]));
assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5"]));
assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6"]));
assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7"]));
assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7", "8"]));
assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7", "8", "9"]));
// "0" is the filename, not a collection
assert!(!p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]));
assert!(!p.is_in_collection(&["test", "0", "2", "3", "4", "5", "6", "7", "8", "9", "0"]));
assert!(!p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "8"]));
assert!(!p.is_in_collection(&["test", "1", "2", "3", "leet", "5", "6", "7"]));
}
} }