Skip to main content

aria2_core/request/request_group/
status.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Status of a download task.
5///
6/// Custom serde impl serializes every variant as a plain lowercase string
7/// (e.g. "active", "waiting", "error"), matching C++ aria2's wire format that
8/// browser plugins expect. The `Error(String)` payload (error message) is not
9/// emitted here; callers surface it via `StatusInfo.error_message` instead.
10#[derive(Debug, Clone, PartialEq, Eq, Default)]
11pub enum DownloadStatus {
12    #[default]
13    Waiting,
14    Active,
15    Paused,
16    Error(String),
17    Complete,
18    Removed,
19}
20
21impl Serialize for DownloadStatus {
22    fn serialize<S: serde::Serializer>(
23        &self,
24        serializer: S,
25    ) -> std::result::Result<S::Ok, S::Error> {
26        serializer.serialize_str(self.as_str())
27    }
28}
29
30impl<'de> Deserialize<'de> for DownloadStatus {
31    fn deserialize<D: serde::Deserializer<'de>>(
32        deserializer: D,
33    ) -> std::result::Result<Self, D::Error> {
34        let s = String::deserialize(deserializer)?;
35        match s.as_str() {
36            "waiting" => Ok(DownloadStatus::Waiting),
37            "active" => Ok(DownloadStatus::Active),
38            "paused" => Ok(DownloadStatus::Paused),
39            "error" => Ok(DownloadStatus::Error(String::new())),
40            "complete" => Ok(DownloadStatus::Complete),
41            "removed" => Ok(DownloadStatus::Removed),
42            _ => Err(serde::de::Error::unknown_variant(
43                "DownloadStatus",
44                &[
45                    "waiting", "active", "paused", "error", "complete", "removed",
46                ],
47            )),
48        }
49    }
50}
51
52impl fmt::Display for DownloadStatus {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            DownloadStatus::Waiting => write!(f, "waiting"),
56            DownloadStatus::Active => write!(f, "active"),
57            DownloadStatus::Paused => write!(f, "paused"),
58            DownloadStatus::Error(_) => write!(f, "error"),
59            DownloadStatus::Complete => write!(f, "complete"),
60            DownloadStatus::Removed => write!(f, "removed"),
61        }
62    }
63}
64
65impl DownloadStatus {
66    /// Whether a download currently owns an active execution slot.
67    ///
68    /// `is_active` intentionally also includes `Waiting` for callers that
69    /// need the live, non-terminal task set. Runtime option compatibility
70    /// needs the narrower meaning used by aria2's active request list.
71    pub fn is_running(&self) -> bool {
72        matches!(self, DownloadStatus::Active)
73    }
74
75    pub fn is_active(&self) -> bool {
76        matches!(self, DownloadStatus::Active | DownloadStatus::Waiting)
77    }
78
79    pub fn is_completed(&self) -> bool {
80        matches!(self, DownloadStatus::Complete)
81    }
82
83    pub fn is_paused(&self) -> bool {
84        matches!(self, DownloadStatus::Paused)
85    }
86
87    pub fn is_stopped(&self) -> bool {
88        !self.is_active()
89    }
90
91    pub fn as_str(&self) -> &'static str {
92        match self {
93            DownloadStatus::Active => "active",
94            DownloadStatus::Waiting => "waiting",
95            DownloadStatus::Paused => "paused",
96            DownloadStatus::Error(_) => "error",
97            DownloadStatus::Complete => "complete",
98            DownloadStatus::Removed => "removed",
99        }
100    }
101}