1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::time::SystemTime;

use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum StatusKey {
    Gid,
    Status,
    TotalLength,
    CompletedLength,
    UploadLength,
    Bitfield,
    DownloadSpeed,
    UploadSpeed,
    InfoHash,
    NumSeeders,
    Seeder,
    PieceLength,
    NumPieces,
    Connections,
    ErrorCode,
    ErrorMessage,
    FollowedBy,
    Following,
    BelongsTo,
    Dir,
    Files,
    Bittorrent,
    VerifiedLength,
    VerifyIntegrityPending,
}

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Status {
    pub gid: Option<SmolStr>,
    pub status: Option<TaskStatus>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub total_length: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub completed_length: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub upload_length: Option<u64>,
    pub bitfield: Option<String>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub download_speed: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub upload_speed: Option<u64>,
    pub info_hash: Option<String>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub num_seeders: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub seeder: Option<bool>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub piece_length: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub num_pieces: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub connections: Option<u64>,
    pub error_code: Option<String>,
    pub error_message: Option<String>,
    pub followed_by: Option<Vec<String>>,
    pub following: Option<String>,
    pub belongs_to: Option<String>,
    pub dir: Option<String>,
    pub files: Option<Vec<File>>,
    pub bittorrent: Option<BittorrentStatus>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
#[repr(u8)]
pub enum TaskStatus {
    Active,
    Waiting,
    Paused,
    Error,
    Complete,
    Removed,
}

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct File {
    #[serde_as(as = "serde_with::DisplayFromStr")]
    pub index: u64,
    pub path: String,
    #[serde_as(as = "serde_with::DisplayFromStr")]
    pub length: u64,
    #[serde_as(as = "serde_with::DisplayFromStr")]
    pub completed_length: u64,
    #[serde_as(as = "serde_with::DisplayFromStr")]
    pub selected: bool,
    pub uris: Vec<Uri>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Uri {
    pub uri: String,
    pub status: UriStatus,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum UriStatus {
    Used,
    Waiting,
}

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BittorrentStatus {
    pub announce_list: Vec<Vec<String>>,
    pub comment: Option<String>,
    #[serde_as(as = "Option<serde_with::TimestampSeconds<i64>>")]
    pub creation_date: Option<SystemTime>,
    pub mode: Option<BitTorrentMode>,
    pub info: Option<BittorrentInfo>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum BitTorrentMode {
    Single,
    Multi,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct BittorrentInfo {
    pub name: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum StatKey {
    DownloadSpeed,
    UploadSpeed,
    NumActive,
    NumWaiting,
    NumStopped,
    NumStoppedTotal,
}

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Stat {
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub download_speed: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub upload_speed: Option<u64>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub num_active: Option<u32>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub num_waiting: Option<u32>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub num_stopped: Option<u32>,
    #[serde_as(as = "Option<serde_with::DisplayFromStr>")]
    pub num_stopped_total: Option<u32>,
}