radicle/node/
seed.rs

1pub mod store;
2pub use store::{Error, Store};
3
4use localtime::LocalTime;
5
6use crate::git;
7use crate::node::KnownAddress;
8use crate::prelude::NodeId;
9use crate::storage::{refs::RefsAt, ReadRepository, RemoteId};
10
11/// Holds an oid and timestamp.
12#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
13#[serde(rename_all = "camelCase")]
14#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
15pub struct SyncedAt {
16    /// Head of `rad/sigrefs`.
17    #[cfg_attr(feature = "schemars", schemars(with = "crate::schemars_ext::git::Oid"))]
18    pub oid: git_ext::Oid,
19    /// When these refs were synced.
20    #[serde(with = "crate::serde_ext::localtime::time")]
21    #[cfg_attr(
22        feature = "schemars",
23        schemars(with = "crate::schemars_ext::localtime::LocalDurationInSeconds")
24    )]
25    pub timestamp: LocalTime,
26}
27
28impl SyncedAt {
29    /// Load a new [`SyncedAt`] for the given remote.
30    pub fn load<S: ReadRepository>(repo: &S, remote: RemoteId) -> Result<Self, git::ext::Error> {
31        let refs = RefsAt::new(repo, remote)?;
32        let oid = refs.at;
33
34        Self::new(oid, repo)
35    }
36
37    /// Create a new [`SyncedAt`] given an OID, by looking up the timestamp in the repo.
38    pub fn new<S: ReadRepository>(oid: git::ext::Oid, repo: &S) -> Result<Self, git::ext::Error> {
39        let timestamp = repo.commit(oid)?.time();
40        let timestamp = LocalTime::from_secs(timestamp.seconds() as u64);
41
42        Ok(Self { oid, timestamp })
43    }
44}
45
46impl Ord for SyncedAt {
47    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
48        self.timestamp.cmp(&other.timestamp)
49    }
50}
51
52impl PartialOrd for SyncedAt {
53    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
54        Some(self.cmp(other))
55    }
56}
57
58/// Seed of a specific repository that has been synced at least once.
59#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
60#[serde(rename_all = "camelCase")]
61pub struct SyncedSeed {
62    /// The Node ID.
63    pub nid: NodeId,
64    /// Known addresses for this node.
65    pub addresses: Vec<KnownAddress>,
66    /// Sync information for a given repo.
67    pub synced_at: SyncedAt,
68}