Skip to main content

radicle_protocol/
fetcher.rs

1use nonempty::NonEmpty;
2use radicle::storage::refs::RefsAt;
3use serde::{Deserialize, Serialize};
4
5pub mod service;
6pub use service::FetcherService;
7
8pub mod state;
9pub use state::{
10    ActiveFetch, Config, FetchConfig, FetcherState, MaxQueueSize, Queue, QueueIter, QueuedFetch,
11};
12
13#[cfg(test)]
14mod test;
15
16// TODO(finto): `Service::fetch_refs_at` and the use of `refs_status_of` is a
17// layer above the `Fetcher` where it would perform I/O, mocked out by a trait,
18// to check if there are wants and add a fetch to the Fetcher.
19
20/// Represents references to fetch, in the context of a repository.
21#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
22pub enum RefsToFetch {
23    /// Indicates that all references should be fetched.
24    All,
25    /// Contains a non-empty collection of specific references to fetch.
26    Refs(NonEmpty<RefsAt>),
27}
28
29impl RefsToFetch {
30    /// Merges another `RefsToFetch` into this one, resulting in a new
31    /// `RefsToFetch` that represents the combined set of references to fetch.
32    /// If either `RefsToFetch` is `All`, the result will be `All`. If both are
33    /// `Refs`, their contents will be combined into a single `Refs` variant.
34    pub(super) fn merge(self, other: RefsToFetch) -> Self {
35        match (self, other) {
36            (RefsToFetch::All, _) | (_, RefsToFetch::All) => RefsToFetch::All,
37            (RefsToFetch::Refs(mut ours), RefsToFetch::Refs(theirs)) => {
38                ours.extend(theirs);
39                RefsToFetch::Refs(ours)
40            }
41        }
42    }
43
44    #[cfg(test)]
45    pub fn len(&self) -> Option<std::num::NonZeroUsize> {
46        match self {
47            RefsToFetch::All => None,
48            RefsToFetch::Refs(refs) => std::num::NonZeroUsize::new(refs.len()),
49        }
50    }
51}
52
53impl From<RefsToFetch> for Vec<RefsAt> {
54    fn from(val: RefsToFetch) -> Self {
55        match val {
56            RefsToFetch::All => Vec::new(),
57            RefsToFetch::Refs(refs) => refs.into(),
58        }
59    }
60}
61
62impl From<Vec<RefsAt>> for RefsToFetch {
63    fn from(refs_at: Vec<RefsAt>) -> Self {
64        match NonEmpty::from_vec(refs_at) {
65            Some(refs) => RefsToFetch::Refs(refs),
66            None => RefsToFetch::All,
67        }
68    }
69}