From cb677ad04c59235ce73e1ba4b953b7d87fd405a8 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 14 Feb 2020 13:06:31 +0000 Subject: [PATCH] Pull parent check out and start from the current Storkable instead of the parent --- stork/src/lib.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/stork/src/lib.rs b/stork/src/lib.rs index f9c67a9..364722b 100644 --- a/stork/src/lib.rs +++ b/stork/src/lib.rs @@ -48,7 +48,7 @@ use failure::ResultExt; /// be called with a value of `T`, and is expected to return all the /// values of `T` that can be found on the given `T`. #[derive(Debug, Clone)] -pub struct Storkable> { +pub struct Storkable> { value: T, filters: FilterSet, client: Arc, @@ -97,6 +97,22 @@ impl<'a, T: Unpin + PartialEq + 'a, C: StorkClient + 'a> Storkable { self.parent.as_ref().map(Arc::as_ref) } + /// Checks if this Storkable, or any parent Storkables have the same + /// value as the one given. + fn check_parent_is(&self, value: &T) -> bool { + // loop through all parents (starting with ourselves) to see if + // they happen to have the same value. + let mut current_parent = Some(self); + while let Some(parent) = current_parent { + if &parent.value == value { + return true; + } + current_parent = parent.parent(); + } + + false + } + /// Start storking this [Storkable]. /// /// Finds all the followable links on this [Storkable] and returns @@ -108,7 +124,7 @@ impl<'a, T: Unpin + PartialEq + 'a, C: StorkClient + 'a> Storkable { try_stream! { let mut children = this.client.run(this.val()); - 'child: while let Some(child) = children.next().await { + while let Some(child) = children.next().await { let child = child.context(StorkError::ClientError)?; if !this.filters.matches(&child) { @@ -118,14 +134,8 @@ impl<'a, T: Unpin + PartialEq + 'a, C: StorkClient + 'a> Storkable { // ensure we're not going to cause a recursive loop by // checking that the page we're about to yield isn't a // parent of it - // TODO: should this happen before or after we try to - // TODO: match it against the filters - let mut current_parent = this.parent(); - while let Some(parent) = current_parent { - if parent.value == this.value { - continue 'child; - } - current_parent = parent.parent(); + if this.check_parent_is(&child) { + continue; } yield Storkable { -- libgit2 1.7.2