Skip to main content

AirLibrary/Downloader/
Types.rs

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