Radish alpha
r
rad:z2UcCU1LgMshWvXj6hXSDDrwB8q8M
Radicle Job Collaborative Object
Radicle
Git
feat: add FindByCommit iterator
Fintan Halpenny committed 9 months ago
commit 0c82a9a934564c825dbdc2d9b3d38ae314d217f0
parent 574129a
1 file changed +44 -0
modified src/lib.rs
@@ -607,6 +607,50 @@ where
    pub fn get(&self, id: &JobId) -> Result<Option<Job>, store::Error> {
        self.raw.get(id.as_object_id())
    }
+

+
    /// Find the [`Job`]s that are associated with the `wanted` commit.
+
    pub fn find_by_commit(&self, wanted: Oid) -> Result<FindByCommit<'a>, store::Error> {
+
        FindByCommit::new(self, wanted)
+
    }
+
}
+

+
/// [`Iterator`] for finding each [`Job`] where the [`Job::oid`] matches the
+
/// wanted commit. See [`Jobs::find_by_commit`].
+
pub struct FindByCommit<'a> {
+
    jobs: Box<dyn Iterator<Item = Result<(ObjectId, Job), cob::store::Error>> + 'a>,
+
    needle: Oid,
+
}
+

+
impl<'a> FindByCommit<'a> {
+
    fn new<R>(jobs: &Jobs<'a, R>, needle: Oid) -> Result<Self, cob::store::Error>
+
    where
+
        R: ReadRepository + cob::Store<Namespace = NodeId>,
+
    {
+
        Ok(Self {
+
            jobs: Box::new(jobs.all()?),
+
            needle,
+
        })
+
    }
+

+
    fn wanted(&self, job: &Job) -> bool {
+
        self.needle == *job.oid()
+
    }
+
}
+

+
impl Iterator for FindByCommit<'_> {
+
    type Item = Result<(JobId, Job), cob::store::Error>;
+

+
    fn next(&mut self) -> Option<Self::Item> {
+
        let job = self.jobs.next()?;
+
        job.and_then(|(id, job)| {
+
            if self.wanted(&job) {
+
                Ok(Some((JobId::from(id), job)))
+
            } else {
+
                self.next().transpose()
+
            }
+
        })
+
        .transpose()
+
    }
}

impl<'a, R> Jobs<'a, R>