Skip to main content

hypha/cache/
types.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5/// Status of a single cached item
6#[derive(Debug, Clone, Serialize, Deserialize, Default)]
7pub struct FetchStatus {
8    #[serde(skip_serializing_if = "Option::is_none")]
9    pub fetched_at_epoch_ms: Option<u64>,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub failed_at_epoch_ms: Option<u64>,
12    #[serde(default)]
13    pub retry_count: u32,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub error: Option<String>,
16}
17
18impl FetchStatus {
19    pub fn success() -> Self {
20        Self {
21            fetched_at_epoch_ms: Some(crate::time::now_epoch_ms()),
22            failed_at_epoch_ms: None,
23            retry_count: 0,
24            error: None,
25        }
26    }
27
28    pub fn failure(error: &str, previous: Option<&FetchStatus>) -> Self {
29        Self {
30            fetched_at_epoch_ms: previous.and_then(|p| p.fetched_at_epoch_ms),
31            failed_at_epoch_ms: Some(crate::time::now_epoch_ms()),
32            retry_count: previous.map(|p| p.retry_count + 1).unwrap_or(1),
33            error: Some(error.to_string()),
34        }
35    }
36
37    /// Check if this cache entry is still fresh within the given TTL (milliseconds)
38    pub fn is_fresh(&self, ttl_ms: u64) -> bool {
39        match self.fetched_at_epoch_ms {
40            Some(ts) => crate::time::now_epoch_ms().saturating_sub(ts) < ttl_ms,
41            None => false,
42        }
43    }
44}
45
46/// Cached taste verdict for a spore — alias for the substrate standard type.
47pub type TasteVerdictCache = substrate::TasteVerdictRecord;
48
49/// A cached key trust entry — records that a domain confirmed ownership of a key
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct KeyTrustEntry {
52    pub key: String,
53    pub confirmed_at_epoch_ms: u64,
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    pub retired_at_epoch_ms: Option<u64>,
56}
57
58/// Pinned domain-state for cmn.json anti-rollback checks.
59#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
60pub struct DomainStatePin {
61    pub serial: u64,
62    pub capsules_digest: String,
63    pub current_key: String,
64    pub pinned_at_epoch_ms: u64,
65}
66
67/// Cache status for domain metadata (cmn, mycelium)
68#[derive(Debug, Clone, Serialize, Deserialize, Default)]
69pub struct CacheStatus {
70    #[serde(default)]
71    pub cmn: FetchStatus,
72    #[serde(default)]
73    pub mycelium: FetchStatus,
74}
75
76/// Information about a cached spore
77pub struct CachedSpore {
78    pub domain: String,
79    pub hash: String,
80    pub name: String,
81    pub synopsis: String,
82    pub path: PathBuf,
83    pub size: u64,
84    pub verdict: Option<substrate::TasteVerdict>,
85}