gosh_dl/protocol/
options.rs

1//! Download options and priority types
2//!
3//! Types for configuring individual downloads.
4
5use super::checksum::ExpectedChecksum;
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8
9/// Priority levels for downloads
10#[derive(
11    Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
12)]
13#[serde(rename_all = "lowercase")]
14#[repr(i8)]
15pub enum DownloadPriority {
16    /// Low priority - downloads last
17    Low = -1,
18    /// Normal priority - default for most downloads
19    #[default]
20    Normal = 0,
21    /// High priority - downloads before normal
22    High = 1,
23    /// Critical priority - downloads first
24    Critical = 2,
25}
26
27impl std::fmt::Display for DownloadPriority {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::Low => write!(f, "low"),
31            Self::Normal => write!(f, "normal"),
32            Self::High => write!(f, "high"),
33            Self::Critical => write!(f, "critical"),
34        }
35    }
36}
37
38impl std::str::FromStr for DownloadPriority {
39    type Err = String;
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        match s.to_lowercase().as_str() {
43            "low" | "-1" => Ok(Self::Low),
44            "normal" | "0" => Ok(Self::Normal),
45            "high" | "1" => Ok(Self::High),
46            "critical" | "2" => Ok(Self::Critical),
47            _ => Err(format!("Invalid priority: {}", s)),
48        }
49    }
50}
51
52/// Options for adding a new download
53#[derive(Debug, Clone, Default, Serialize, Deserialize)]
54pub struct DownloadOptions {
55    /// Download priority (affects queue ordering)
56    #[serde(default)]
57    pub priority: DownloadPriority,
58    /// Directory to save files
59    pub save_dir: Option<PathBuf>,
60    /// Output filename
61    pub filename: Option<String>,
62    /// Custom user agent
63    pub user_agent: Option<String>,
64    /// Referer header
65    pub referer: Option<String>,
66    /// Additional headers
67    pub headers: Vec<(String, String)>,
68    /// Cookies for authenticated downloads (e.g., ["session=abc123", "token=xyz"])
69    pub cookies: Option<Vec<String>>,
70    /// Expected checksum for verification after download (e.g., MD5 or SHA256)
71    pub checksum: Option<ExpectedChecksum>,
72    /// Mirror/fallback URLs for redundancy (tried in order on failure)
73    pub mirrors: Vec<String>,
74    /// Max connections for this download
75    pub max_connections: Option<usize>,
76    /// Max download speed (bytes/sec)
77    pub max_download_speed: Option<u64>,
78    /// Max upload speed (bytes/sec, torrent only)
79    pub max_upload_speed: Option<u64>,
80    /// Seed ratio limit (torrent only)
81    pub seed_ratio: Option<f64>,
82    /// Selected file indices (torrent only)
83    pub selected_files: Option<Vec<usize>>,
84    /// Sequential download mode (torrent only) - downloads pieces in order for streaming
85    pub sequential: Option<bool>,
86}