summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-08-25 13:46:08 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-09-01 13:36:39 +0200
commit84135b1961bc2406a2c5f76cf3bf7da9d44be000 (patch)
treefe5a0db160218d38fb8927abfd38f273482afbcd
parent47044d9ffea98e1f28efea113175cff61cfd9ef2 (diff)
downloadimag-84135b1961bc2406a2c5f76cf3bf7da9d44be000.zip
imag-84135b1961bc2406a2c5f76cf3bf7da9d44be000.tar.gz
Add Entries::{find_by_id_substr, find_by_id_startswith}
This patch adds functions to the Entries type which can be used for filtering by id, either with `contains()` or `starts_with()`. This is useful for the end-user functionality where the user specifies the ID of an entry only partially. The implementation still iterates over all entries. This could be improved, of course, by implementing a `find`-like function on `Store` directly. But for now, this is good enough. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/core/libimagstore/src/iter.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs
index 1c80d16..3dfd2b2 100644
--- a/lib/core/libimagstore/src/iter.rs
+++ b/lib/core/libimagstore/src/iter.rs
@@ -222,6 +222,31 @@ impl<'a> Entries<'a> {
StoreRetrieveIterator::new(Box::new(self.0.map(|r| r.map(|id| id.without_base()))), self.1)
}
+ /// Find entries where the id contains a substring
+ ///
+ /// This is useful for finding entries if the user supplied only a part of the ID, for example
+ /// if the ID contains a UUID where the user did not specify the full UUID, E.G.:
+ ///
+ /// ```ignore
+ /// imag foo show 827d8596-fad1-4
+ /// ```
+ ///
+ /// # Note
+ ///
+ /// The substring match is done with `contains()`.
+ ///
+ pub fn find_by_id_substr<'b>(self, id_substr: &'b str) -> FindContains<'a, 'b> {
+ FindContains(self, id_substr)
+ }
+
+ /// Find entries where the id starts with a substring
+ ///
+ /// Same as `Entries::find_by_id_substr()`, but using `starts_with()` rather than `contains`.
+ ///
+ pub fn find_by_id_startswith<'b>(self, id_substr: &'b str) -> FindStartsWith<'a, 'b> {
+ FindStartsWith(self, id_substr)
+ }
+
}
impl<'a> Iterator for Entries<'a> {
@@ -232,6 +257,42 @@ impl<'a> Iterator for Entries<'a> {
}
}
+pub struct FindContains<'a, 'b>(Entries<'a>, &'b str);
+
+impl<'a, 'b> Iterator for FindContains<'a, 'b> {
+ type Item = Result<StoreId>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match self.0.next() {
+ None => return None,
+ Some(Err(e)) => return Some(Err(e)),
+ Some(Ok(next)) => if next.local().to_string_lossy().contains(self.1) {
+ return Some(Ok(next))
+ }, // else loop
+ }
+ }
+ }
+}
+
+pub struct FindStartsWith<'a, 'b>(Entries<'a>, &'b str);
+
+impl<'a, 'b> Iterator for FindStartsWith<'a, 'b> {
+ type Item = Result<StoreId>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match self.0.next() {
+ None => return None,
+ Some(Err(e)) => return Some(Err(e)),
+ Some(Ok(next)) => if next.local().to_string_lossy().starts_with(self.1) {
+ return Some(Ok(next))
+ }, // else loop
+ }
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
extern crate env_logger;