1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "snake_case")]
8pub enum FactsOrigin {
9 DiskCache,
10 Network,
11 LocalFile,
12}
13
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
16#[serde(tag = "state", rename_all = "snake_case")]
17pub enum CloudFactsState {
18 #[default]
20 Off,
21 Inert,
23 BundledOnly,
25 Verified {
27 channel: String,
28 facts_version: u64,
29 key_id: String,
30 fetched_at: u64,
31 origin: FactsOrigin,
32 stale: bool,
33 patches: usize,
34 defaults: usize,
35 announcements: usize,
36 },
37 Rejected { reason: String, at: u64 },
39 NotApplicable { applies_to: String },
41 Failed {
43 last_error: String,
44 at: u64,
45 keeping: Option<u64>,
46 },
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
51pub struct CloudFactsStatus {
52 pub state: CloudFactsState,
53 pub last_attempt: Option<u64>,
54 pub etag: Option<String>,
55 pub source_label: String,
56}
57
58#[must_use]
60pub fn age_label(then: u64, now: u64) -> String {
61 let secs = now.saturating_sub(then);
62 if secs < 60 {
63 "just now".to_string()
64 } else if secs < 3600 {
65 format!("{}m ago", secs / 60)
66 } else if secs < 86_400 {
67 format!("{}h ago", secs / 3600)
68 } else {
69 format!("{}d ago", secs / 86_400)
70 }
71}
72
73impl CloudFactsStatus {
74 #[must_use]
76 pub fn label(&self, now_unix: u64) -> String {
77 self.state.label(now_unix)
78 }
79}
80
81impl CloudFactsState {
82 #[must_use]
84 pub fn label(&self, now_unix: u64) -> String {
85 match self {
86 CloudFactsState::Off => "off (bundled)".to_string(),
87 CloudFactsState::Inert => "inert (no trusted keys; bundled)".to_string(),
88 CloudFactsState::BundledOnly => "enabled, none verified yet (bundled)".to_string(),
89 CloudFactsState::Verified {
90 channel,
91 facts_version,
92 key_id,
93 fetched_at,
94 origin,
95 stale,
96 patches,
97 defaults,
98 announcements,
99 } => {
100 let origin = match origin {
101 FactsOrigin::DiskCache => "disk cache",
102 FactsOrigin::Network => "network",
103 FactsOrigin::LocalFile => "local file",
104 };
105 let mut out = format!(
106 "{channel} v{facts_version} · verified {key_id} · fetched {} ({origin})",
107 age_label(*fetched_at, now_unix)
108 );
109 if *stale {
110 out.push_str(" · stale");
111 }
112 let _ = std::fmt::Write::write_fmt(
113 &mut out,
114 format_args!(
115 " · {patches} patch{}, {defaults} default{}, {announcements} notice{}",
116 if *patches == 1 { "" } else { "es" },
117 if *defaults == 1 { "" } else { "s" },
118 if *announcements == 1 { "" } else { "s" },
119 ),
120 );
121 out
122 }
123 CloudFactsState::Rejected { reason, at } => {
124 format!(
125 "rejected: {reason} ({}; bundled in use)",
126 age_label(*at, now_unix)
127 )
128 }
129 CloudFactsState::NotApplicable { applies_to } => {
130 format!("not applicable to this build ({applies_to}; bundled in use)")
131 }
132 CloudFactsState::Failed {
133 last_error,
134 at,
135 keeping,
136 } => match keeping {
137 Some(version) => format!(
138 "fetch failed {} ({last_error}); keeping v{version}",
139 age_label(*at, now_unix)
140 ),
141 None => format!(
142 "fetch failed {} ({last_error}); bundled in use",
143 age_label(*at, now_unix)
144 ),
145 },
146 }
147 }
148}