grass/dev/iterator/
location.rs

1use tracing::warn;
2
3use crate::dev::{
4    get_repository_change_status,
5    strategy::{
6        alias::SupportsAlias,
7        git::{RepositoryChangeStatus, SupportsGit},
8    },
9    Api, RepositoryLocation,
10};
11
12pub struct WithChangeStatusIterator<'a, T, U>
13where
14    T: Iterator<Item = RepositoryLocation>,
15    U: SupportsGit + SupportsAlias,
16{
17    source: &'a mut T,
18    api: &'a Api<U>,
19}
20
21impl<'a, T: Iterator<Item = RepositoryLocation>, U: SupportsGit + SupportsAlias> Iterator
22    for WithChangeStatusIterator<'a, T, U>
23{
24    type Item = (RepositoryLocation, RepositoryChangeStatus);
25
26    fn next(&mut self) -> Option<Self::Item> {
27        for next in &mut self.source {
28            let change_status = match get_repository_change_status(self.api, next.clone()) {
29                Ok(change_status) => change_status,
30                Err(error) => {
31                    warn!(
32                        "Could not get the repository change status of repository {}\nReason:\n{}",
33                        next, error
34                    );
35                    continue;
36                }
37            };
38
39            return Some((next, change_status));
40        }
41        None
42    }
43}
44
45pub trait LocationIterExtensions: Iterator<Item = RepositoryLocation> + Sized {
46    fn with_change_status<'a, T: SupportsGit + SupportsAlias>(
47        &'a mut self,
48        api: &'a Api<T>,
49    ) -> WithChangeStatusIterator<'a, Self, T>;
50}
51
52impl<T: Iterator<Item = RepositoryLocation> + Sized> LocationIterExtensions for T {
53    fn with_change_status<'a, U: SupportsGit + SupportsAlias>(
54        &'a mut self,
55        api: &'a Api<U>,
56    ) -> WithChangeStatusIterator<'a, Self, U> {
57        WithChangeStatusIterator { source: self, api }
58    }
59}