Skip to main content

romm_api/core/download/
job.rs

1//! Per-ROM download job state.
2
3use std::sync::atomic::{AtomicUsize, Ordering};
4
5/// High-level status of a single download.
6#[derive(Debug, Clone)]
7pub enum DownloadStatus {
8    Downloading,
9    Done,
10    SkippedAlreadyExists,
11    Cancelled,
12    FinalizeFailed(String),
13    Error(String),
14}
15
16/// A single background download job (for one ROM).
17#[derive(Debug, Clone)]
18pub struct DownloadJob {
19    pub id: usize,
20    pub rom_id: u64,
21    pub name: String,
22    pub platform: String,
23    /// 0.0 ..= 1.0
24    pub progress: f64,
25    pub status: DownloadStatus,
26}
27
28static NEXT_JOB_ID: AtomicUsize = AtomicUsize::new(0);
29
30impl DownloadJob {
31    /// Construct a new job in the `Downloading` state.
32    pub fn new(rom_id: u64, name: String, platform: String) -> Self {
33        Self {
34            id: NEXT_JOB_ID.fetch_add(1, Ordering::Relaxed),
35            rom_id,
36            name,
37            platform,
38            progress: 0.0,
39            status: DownloadStatus::Downloading,
40        }
41    }
42
43    /// Progress as percentage 0..=100.
44    pub fn percent(&self) -> u16 {
45        (self.progress * 100.0).round().min(100.0) as u16
46    }
47}