aboutsummaryrefslogtreecommitdiff
path: root/apps/servers/octopus/supergit/src/files/tree_utils.rs
diff options
context:
space:
mode:
authorMx Kookie <kookie@spacekookie.de>2021-01-08 02:26:36 +0100
committerMx Kookie <kookie@spacekookie.de>2021-01-08 02:26:36 +0100
commit7a7c3f0adf32382b478b18f745ffd62dcaaa4940 (patch)
treeadba952a653849d9d23f52ab5da87ecc40078310 /apps/servers/octopus/supergit/src/files/tree_utils.rs
parent200e21a1548afda95e5c00b3772b5d14c06c1689 (diff)
octopus: supergit: implement initial tree history mechanism
Diffstat (limited to 'apps/servers/octopus/supergit/src/files/tree_utils.rs')
-rw-r--r--apps/servers/octopus/supergit/src/files/tree_utils.rs41
1 files changed, 38 insertions, 3 deletions
diff --git a/apps/servers/octopus/supergit/src/files/tree_utils.rs b/apps/servers/octopus/supergit/src/files/tree_utils.rs
index 55a6c2e0ffab..f5cb94a4031a 100644
--- a/apps/servers/octopus/supergit/src/files/tree_utils.rs
+++ b/apps/servers/octopus/supergit/src/files/tree_utils.rs
@@ -2,7 +2,33 @@
use crate::HashId;
use git2::{Repository, Tree};
-use std::{path::PathBuf, sync::Arc};
+use std::{iter::Peekable, path::PathBuf, sync::Arc};
+
+pub(super) trait PairIter<I, K>
+where
+ I: Iterator<Item = K>,
+{
+ /// Step through
+ fn pairs<'i, F: FnMut(K, Option<&K>)>(&'i mut self, cb: F);
+
+ fn next_pair<'i>(&'i mut self) -> (Option<K>, Option<&K>);
+}
+
+impl<I, K> PairIter<I, K> for Peekable<I>
+where
+ I: Iterator<Item = K>,
+{
+ fn pairs<'i, F: FnMut(K, Option<&K>)>(&'i mut self, mut cb: F) {
+ // Iterate until a->None
+ while let (Some(a), b) = self.next_pair() {
+ cb(a, b);
+ }
+ }
+
+ fn next_pair<'i>(&'i mut self) -> (Option<K>, Option<&K>) {
+ (self.next(), self.peek())
+ }
+}
/// Take a vector of path segments, and turn it into a valid offset path
///
@@ -32,8 +58,6 @@ pub(super) fn path_cmp(target: &Vec<&str>, path: &str, entry: &str) -> bool {
let mut buf = path_split(path);
buf.push(entry);
- eprintln!("{:?}", buf);
-
target == &buf
}
@@ -59,3 +83,14 @@ fn nested_path() {
String::from("foo/bar/baz")
);
}
+
+#[test]
+fn pair_iterator() {
+ // The pair iterator needs access to `peek()`
+ let mut i = vec![1, 2, 3, 4].into_iter().peekable();
+
+ assert_eq!(i.next_pair(), (Some(1), Some(&2)));
+ assert_eq!(i.next_pair(), (Some(2), Some(&3)));
+ assert_eq!(i.next_pair(), (Some(3), Some(&4)));
+ assert_eq!(i.next_pair(), (Some(4), None));
+}