Skip to main content

AirLibrary/Downloader/
Types.rs

1//! Value types for the download lifecycle: state, priority, queue entries,
2//! results, statistics, and per-download configuration.
3//!
4//! These structs are the data contract between `DownloadManager` methods,
5//! IPC callers (Cocoon VSIX installs, Mountain status queries), and tests.
6
7use std::{path::PathBuf, sync::Arc, time::Duration};
8
9use serde::{Deserialize, Serialize};
10
11/// Fine-grained state of a single download.
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
13pub enum DownloadState {
14	Pending,
15
16	Queued,
17
18	Downloading,
19
20	Verifying,
21
22	Completed,
23
24	Failed,
25
26	Cancelled,
27
28	Paused,
29
30	Resuming,
31}
32
33/// Scheduling priority for the download queue.
34#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
35pub enum DownloadPriority {
36	High = 3,
37
38	Normal = 2,
39
40	Low = 1,
41
42	Background = 0,
43}
44
45/// Live status snapshot for one active download.
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct DownloadStatus {
48	pub DownloadId:String,
49
50	pub url:String,
51
52	pub destination:PathBuf,
53
54	pub TotalSize:u64,
55
56	pub downloaded:u64,
57
58	pub progress:f32,
59
60	pub status:DownloadState,
61
62	pub error:Option<String>,
63
64	pub StartedAt:Option<chrono::DateTime<chrono::Utc>>,
65
66	pub CompletedAt:Option<chrono::DateTime<chrono::Utc>>,
67
68	pub ChunksCompleted:usize,
69
70	pub TotalChunks:usize,
71
72	pub DownloadRateBytesPerSec:u64,
73
74	pub ExpectedChecksum:Option<String>,
75
76	pub ActualChecksum:Option<String>,
77}
78
79/// Entry in the priority download queue.
80#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct QueuedDownload {
82	pub DownloadId:String,
83
84	pub url:String,
85
86	pub destination:PathBuf,
87
88	pub checksum:String,
89
90	pub priority:DownloadPriority,
91
92	pub AddedAt:chrono::DateTime<chrono::Utc>,
93
94	pub MaxFileSize:Option<u64>,
95
96	pub ValidateDiskSpace:bool,
97}
98
99/// Final outcome of a completed download.
100#[derive(Debug, Clone)]
101pub struct DownloadResult {
102	pub path:String,
103
104	pub size:u64,
105
106	pub checksum:String,
107
108	pub duration:Duration,
109
110	pub AverageRate:u64,
111}
112
113/// Aggregate statistics across all downloads in this session.
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct DownloadStatistics {
116	pub TotalDownloads:u64,
117
118	pub SuccessfulDownloads:u64,
119
120	pub FailedDownloads:u64,
121
122	pub CancelledDownloads:u64,
123
124	pub TotalBytesDownloaded:u64,
125
126	pub TotalDownloadTimeSecs:f64,
127
128	pub AverageDownloadRate:f64,
129
130	pub PeakDownloadRate:u64,
131
132	pub ActiveDownloads:usize,
133
134	pub QueuedDownloads:usize,
135}
136
137/// Type alias for progress callbacks registered with a download.
138pub type ProgressCallback = Arc<dyn Fn(DownloadStatus) + Send + Sync>;
139
140/// Per-download configuration including URL, destination, checksum, and limits.
141#[derive(Debug, Clone)]
142pub struct DownloadConfig {
143	pub url:String,
144
145	pub destination:String,
146
147	pub checksum:String,
148
149	pub MaxFileSize:Option<u64>,
150
151	pub ChunkSize:usize,
152
153	pub MaxRetries:u32,
154
155	pub TimeoutSecs:u64,
156
157	pub priority:DownloadPriority,
158
159	pub ValidateDiskSpace:bool,
160}
161
162impl Default for DownloadConfig {
163	fn default() -> Self {
164		Self {
165			url:String::new(),
166
167			destination:String::new(),
168
169			checksum:String::new(),
170
171			MaxFileSize:None,
172
173			ChunkSize:1024 * 1024, // 1 MB
174
175			MaxRetries:3,
176
177			TimeoutSecs:300,
178
179			priority:DownloadPriority::Normal,
180
181			ValidateDiskSpace:true,
182		}
183	}
184}