octo-tui/src/downloads.rs
src/downloads.rsRaw
1use serde::Deserialize;
2use std::time::Instant;
3
4#[derive(Clone, Debug, Default, Deserialize)]
5#[serde(default, rename_all = "camelCase")]
6pub struct Acquisition {
7 pub id: String,
8 pub state: String,
9 pub progress: Option<f64>,
10 pub bytes_done: Option<u64>,
11 pub bytes_total: Option<u64>,
12 pub source: Option<String>,
13 pub started_at: Option<String>,
14 pub library_id: Option<String>,
15}
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum Phase {
19 Requesting,
20 Requested,
21 Queued,
22 Searching,
23 Downloading,
24 Verifying,
25 Importing,
26 Done,
27 Failed,
28 Unknown,
29}
30
31pub struct Download {
32 pub phase: Phase,
33 progress: Option<f64>,
34 source: Option<String>,
35 pub unavailable: bool,
36 pub last_seen: Instant,
37 pub library_id: Option<String>,
38 pub library_resolved: bool,
39 pub resolve_pending: bool,
40 pub next_resolve: Instant,
41}
42
43impl Download {
44 pub fn requesting() -> Self {
45 Self {
46 phase: Phase::Requesting,
47 progress: None,
48 source: None,
49 unavailable: false,
50 last_seen: Instant::now(),
51 library_id: None,
52 library_resolved: false,
53 resolve_pending: false,
54 next_resolve: Instant::now(),
55 }
56 }
57 pub fn in_flight(&self) -> bool {
58 !matches!(self.phase, Phase::Done | Phase::Failed)
59 }
60 pub fn pollable(&self) -> bool {
61 ((self.in_flight() && self.phase != Phase::Requesting)
62 || (self.phase == Phase::Done && !self.library_resolved))
63 && !self.unavailable
64 }
65 /// Keep server progress distinct from the client's request acknowledgement.
66 pub fn update(&mut self, acquisition: &Acquisition) {
67 self.phase = match acquisition.state.as_str() {
68 "queued" => Phase::Queued,
69 "searching" => Phase::Searching,
70 "downloading" => Phase::Downloading,
71 "verifying" => Phase::Verifying,
72 "importing" => Phase::Importing,
73 "done" => Phase::Done,
74 "failed" => Phase::Failed,
75 _ => Phase::Unknown,
76 };
77 self.progress = acquisition
78 .progress
79 .filter(|p| p.is_finite())
80 .map(|p| p.clamp(0., 1.))
81 .or_else(|| {
82 acquisition
83 .bytes_done
84 .zip(acquisition.bytes_total)
85 .filter(|(_, total)| *total > 0)
86 .map(|(done, total)| (done as f64 / total as f64).clamp(0., 1.))
87 });
88 self.source = acquisition.source.clone().filter(|s| !s.trim().is_empty());
89 if let Some(id) = acquisition
90 .library_id
91 .clone()
92 .filter(|id| !id.trim().is_empty())
93 {
94 self.library_id = Some(id);
95 }
96 self.unavailable = false;
97 self.last_seen = Instant::now();
98 }
99 pub fn label(&self) -> String {
100 match self.phase {
101 Phase::Requesting => "REQUEST".into(),
102 Phase::Requested => "REQUESTED".into(),
103 Phase::Queued => "QUEUED".into(),
104 Phase::Searching => "SEARCHING".into(),
105 Phase::Downloading => self
106 .progress
107 .map(|p| format!("↓ {:.0}%", p * 100.))
108 .unwrap_or_else(|| "↓ FETCH".into()),
109 Phase::Verifying => "VERIFYING".into(),
110 Phase::Importing => "IMPORTING".into(),
111 Phase::Done => "✓ ADDED".into(),
112 Phase::Failed => "FAILED".into(),
113 Phase::Unknown => "WAITING".into(),
114 }
115 }
116 pub fn detail(&self) -> String {
117 let mut detail = self.label();
118 if let Some(source) = &self.source {
119 detail.push_str(&format!(" · {source}"));
120 }
121 if self.phase == Phase::Done && !self.library_resolved {
122 detail.push_str(" · waiting for library metadata");
123 }
124 if self.unavailable {
125 detail.push_str(" · server status unavailable");
126 }
127 detail
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134 #[test]
135 fn unknown_server_stage_keeps_polling_until_completion() {
136 let mut download = Download::requesting();
137 download.update(&Acquisition {
138 state: "future-stage".into(),
139 ..Default::default()
140 });
141 assert!(download.pollable());
142 assert_eq!(download.label(), "WAITING");
143 download.update(&Acquisition {
144 state: "failed".into(),
145 ..Default::default()
146 });
147 assert!(!download.pollable());
148 }
149 #[test]
150 fn server_progress_uses_bytes_when_fraction_is_missing() {
151 let mut download = Download::requesting();
152 download.update(&Acquisition {
153 state: "downloading".into(),
154 bytes_done: Some(50),
155 bytes_total: Some(200),
156 source: Some("Soulseek".into()),
157 ..Default::default()
158 });
159 assert_eq!(download.label(), "↓ 25%");
160 assert!(download.detail().contains("Soulseek"));
161 download.update(&Acquisition {
162 state: "done".into(),
163 ..Default::default()
164 });
165 assert!(download.pollable());
166 download.library_resolved = true;
167 assert!(!download.pollable());
168 assert_eq!(download.label(), "✓ ADDED");
169 }
170}