-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [#639] UDP client. Extract aquatic reponses wrappers
for serialization to JSON.
- Loading branch information
1 parent
d1fdb8f
commit ce9e2e8
Showing
3 changed files
with
109 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod app; | ||
pub mod checker; | ||
pub mod responses; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! 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 Peer { | ||
seeders: i32, | ||
completed: i32, | ||
leechers: i32, | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct TorrentStats { | ||
seeders: i32, | ||
completed: i32, | ||
leechers: i32, | ||
} |