From 8b402a03058001e5615fe5028dd6a7b04ec9f008 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Aug 2016 13:39:37 +0200 Subject: [PATCH] Add StoreId::is_in_collection() --- libimagstore/src/storeid.rs | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/libimagstore/src/storeid.rs b/libimagstore/src/storeid.rs index 59868d95..211efbe8 100644 --- a/libimagstore/src/storeid.rs +++ b/libimagstore/src/storeid.rs @@ -124,6 +124,29 @@ impl StoreId { &self.id } + /// Check whether a StoreId points to an entry in a specific collection. + /// + /// A "collection" here is simply a directory. So `foo/bar/baz` is an entry which is in + /// collection ["foo", "bar", "baz"], but also in ["foo", "bar"] and ["foo"]. + /// + /// # 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) + } + pub fn local_push>(&mut self, path: P) { self.id.push(path) } @@ -345,4 +368,24 @@ mod test { assert_eq!(pb.unwrap(), PathBuf::from("/tmp/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"])); + 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"])); + } + }