radicle_protocol/worker/
fetch.rs

1pub mod error;
2
3use std::collections::{BTreeMap, HashSet};
4
5use radicle::crypto::PublicKey;
6use radicle::git;
7use radicle::{identity::DocAt, storage::RefUpdate};
8
9#[derive(Debug, Clone)]
10pub struct FetchResult {
11    /// The set of updated references.
12    pub updated: Vec<RefUpdate>,
13    /// The canonical references that were updated as part of the fetch process.
14    pub canonical: UpdatedCanonicalRefs,
15    /// The set of remote namespaces that were updated.
16    pub namespaces: HashSet<PublicKey>,
17    /// The fetch was a full clone.
18    pub clone: bool,
19    /// Identity doc of fetched repo.
20    pub doc: DocAt,
21}
22
23impl FetchResult {
24    pub fn new(doc: DocAt) -> Self {
25        Self {
26            updated: vec![],
27            canonical: UpdatedCanonicalRefs::default(),
28            namespaces: HashSet::new(),
29            clone: false,
30            doc,
31        }
32    }
33}
34
35/// The set of canonical references, updated after a fetch, and their
36/// corresponding targets.
37#[derive(Clone, Default, Debug)]
38pub struct UpdatedCanonicalRefs {
39    inner: BTreeMap<git::Qualified<'static>, git::Oid>,
40}
41
42impl IntoIterator for UpdatedCanonicalRefs {
43    type Item = (git::Qualified<'static>, git::Oid);
44    type IntoIter = std::collections::btree_map::IntoIter<git::Qualified<'static>, git::Oid>;
45
46    fn into_iter(self) -> Self::IntoIter {
47        self.inner.into_iter()
48    }
49}
50
51impl UpdatedCanonicalRefs {
52    /// Insert a new updated entry for the canonical reference identified by
53    /// `refname` and its new `target.`
54    pub fn updated(&mut self, refname: git::Qualified<'static>, target: git::Oid) {
55        self.inner.insert(refname, target);
56    }
57
58    /// Return an iterator of all the updates.
59    pub fn iter(&self) -> impl Iterator<Item = (&git::Qualified<'static>, &git::Oid)> {
60        self.inner.iter()
61    }
62}
63
64#[cfg(any(test, feature = "test"))]
65impl qcheck::Arbitrary for FetchResult {
66    fn arbitrary(g: &mut qcheck::Gen) -> Self {
67        FetchResult {
68            updated: vec![],
69            canonical: UpdatedCanonicalRefs::default(),
70            namespaces: HashSet::arbitrary(g),
71            clone: bool::arbitrary(g),
72            doc: DocAt::arbitrary(g),
73        }
74    }
75}