use super::checksum::ExpectedChecksum;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(
Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
#[repr(i8)]
pub enum DownloadPriority {
Low = -1,
#[default]
Normal = 0,
High = 1,
Critical = 2,
}
impl std::fmt::Display for DownloadPriority {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Low => write!(f, "low"),
Self::Normal => write!(f, "normal"),
Self::High => write!(f, "high"),
Self::Critical => write!(f, "critical"),
}
}
}
impl std::str::FromStr for DownloadPriority {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"low" | "-1" => Ok(Self::Low),
"normal" | "0" => Ok(Self::Normal),
"high" | "1" => Ok(Self::High),
"critical" | "2" => Ok(Self::Critical),
_ => Err(format!("Invalid priority: {}", s)),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DownloadOptions {
#[serde(default)]
pub priority: DownloadPriority,
pub save_dir: Option<PathBuf>,
pub filename: Option<String>,
pub user_agent: Option<String>,
pub referer: Option<String>,
pub headers: Vec<(String, String)>,
pub cookies: Option<Vec<String>>,
pub checksum: Option<ExpectedChecksum>,
pub mirrors: Vec<String>,
pub max_connections: Option<usize>,
pub max_download_speed: Option<u64>,
pub max_upload_speed: Option<u64>,
pub seed_ratio: Option<f64>,
pub selected_files: Option<Vec<usize>>,
pub sequential: Option<bool>,
}
impl DownloadOptions {
pub fn new() -> Self {
Self::default()
}
pub fn priority(mut self, priority: DownloadPriority) -> Self {
self.priority = priority;
self
}
pub fn save_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.save_dir = Some(dir.into());
self
}
pub fn filename(mut self, name: impl Into<String>) -> Self {
self.filename = Some(name.into());
self
}
pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
self.user_agent = Some(ua.into());
self
}
pub fn referer(mut self, referer: impl Into<String>) -> Self {
self.referer = Some(referer.into());
self
}
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.push((key.into(), value.into()));
self
}
pub fn cookies(mut self, cookies: Vec<String>) -> Self {
self.cookies = Some(cookies);
self
}
pub fn checksum(mut self, checksum: ExpectedChecksum) -> Self {
self.checksum = Some(checksum);
self
}
pub fn mirror(mut self, url: impl Into<String>) -> Self {
self.mirrors.push(url.into());
self
}
pub fn max_connections(mut self, n: usize) -> Self {
self.max_connections = Some(n);
self
}
pub fn max_download_speed(mut self, bps: u64) -> Self {
self.max_download_speed = Some(bps);
self
}
pub fn max_upload_speed(mut self, bps: u64) -> Self {
self.max_upload_speed = Some(bps);
self
}
pub fn seed_ratio(mut self, ratio: f64) -> Self {
self.seed_ratio = Some(ratio);
self
}
pub fn selected_files(mut self, indices: Vec<usize>) -> Self {
self.selected_files = Some(indices);
self
}
pub fn sequential(mut self, enabled: bool) -> Self {
self.sequential = Some(enabled);
self
}
}