Skip to main content

radicle_protocol/fetcher/state/
command.rs

1use std::time;
2
3use radicle_core::{NodeId, RepoId};
4
5use crate::fetcher::RefsToFetch;
6
7/// Commands for transitioning the [`FetcherState`].
8///
9/// [`FetcherState`]: super::FetcherState
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub enum Command {
12    Fetch(Fetch),
13    Fetched(Fetched),
14    Cancel(Cancel),
15}
16
17impl From<Fetch> for Command {
18    fn from(v: Fetch) -> Self {
19        Self::Fetch(v)
20    }
21}
22
23impl From<Fetched> for Command {
24    fn from(v: Fetched) -> Self {
25        Self::Fetched(v)
26    }
27}
28
29impl From<Cancel> for Command {
30    fn from(v: Cancel) -> Self {
31        Self::Cancel(v)
32    }
33}
34
35impl Command {
36    pub fn fetch(from: NodeId, rid: RepoId, refs: RefsToFetch, timeout: time::Duration) -> Self {
37        Self::from(Fetch {
38            from,
39            rid,
40            refs,
41            timeout,
42        })
43    }
44
45    pub fn fetched(from: NodeId, rid: RepoId) -> Self {
46        Self::from(Fetched { from, rid })
47    }
48
49    pub fn cancel(from: NodeId) -> Self {
50        Self::from(Cancel { from })
51    }
52}
53
54/// A fetch wants to be marked as active.
55#[derive(Clone, Debug, PartialEq, Eq)]
56pub struct Fetch {
57    /// The node from which the repository is being fetched from.
58    pub from: NodeId,
59    /// The repository to fetch.
60    pub rid: RepoId,
61    /// The references to fetch.
62    pub refs: RefsToFetch,
63    /// The timeout for the fetch process.
64    pub timeout: time::Duration,
65}
66
67/// A fetch wants to be marked as completed.
68#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
69pub struct Fetched {
70    /// The node from which the repository was fetched from.
71    pub from: NodeId,
72    /// The repository that was fetched.
73    pub rid: RepoId,
74}
75
76/// Any fetches are canceled for the given node.
77#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
78pub struct Cancel {
79    /// The node for which the fetches should be canceled.
80    pub from: NodeId,
81}