forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponses.rs
76 lines (69 loc) · 2.23 KB
/
responses.rs
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
//! Aquatic responses are not serializable. These are the serializable wrappers.
use std::net::{Ipv4Addr, Ipv6Addr};
use aquatic_udp_protocol::{AnnounceResponse, ScrapeResponse};
use serde::Serialize;
#[derive(Serialize)]
pub struct AnnounceResponseDto {
transaction_id: i32,
announce_interval: i32,
leechers: i32,
seeders: i32,
peers: Vec<String>,
}
impl From<AnnounceResponse<Ipv4Addr>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv4Addr>) -> Self {
Self {
transaction_id: announce.transaction_id.0,
announce_interval: announce.announce_interval.0,
leechers: announce.leechers.0,
seeders: announce.seeders.0,
peers: announce
.peers
.iter()
.map(|peer| format!("{}:{}", peer.ip_address, peer.port.0))
.collect::<Vec<_>>(),
}
}
}
impl From<AnnounceResponse<Ipv6Addr>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv6Addr>) -> Self {
Self {
transaction_id: announce.transaction_id.0,
announce_interval: announce.announce_interval.0,
leechers: announce.leechers.0,
seeders: announce.seeders.0,
peers: announce
.peers
.iter()
.map(|peer| format!("{}:{}", peer.ip_address, peer.port.0))
.collect::<Vec<_>>(),
}
}
}
#[derive(Serialize)]
pub struct ScrapeResponseDto {
transaction_id: i32,
torrent_stats: Vec<TorrentStats>,
}
impl From<ScrapeResponse> for ScrapeResponseDto {
fn from(scrape: ScrapeResponse) -> Self {
Self {
transaction_id: scrape.transaction_id.0,
torrent_stats: scrape
.torrent_stats
.iter()
.map(|torrent_scrape_statistics| TorrentStats {
seeders: torrent_scrape_statistics.seeders.0,
completed: torrent_scrape_statistics.completed.0,
leechers: torrent_scrape_statistics.leechers.0,
})
.collect::<Vec<_>>(),
}
}
}
#[derive(Serialize)]
struct TorrentStats {
seeders: i32,
completed: i32,
leechers: i32,
}