forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion.rs
More file actions
65 lines (59 loc) · 2.34 KB
/
Copy pathconversion.rs
File metadata and controls
65 lines (59 loc) · 2.34 KB
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
//! Conversions from domain types to protocol DTOs.
//!
//! These functions bridge tracker-internal domain types (e.g., `Info`,
//! `BasicInfo`, `peer::Peer`) with transport-agnostic protocol DTOs.
use torrust_tracker_core::torrent::services::{BasicInfo, Info};
use torrust_tracker_primitives::{PeerId, peer as domain_peer};
use torrust_tracker_rest_api_protocol::v1::context::torrent::resources::peer as protocol_peer;
use torrust_tracker_rest_api_protocol::v1::context::torrent::resources::torrent::{ListItem, Torrent};
/// Convert a domain [`domain_peer::Peer`] into a protocol [`protocol_peer::Peer`].
#[must_use]
pub fn from_domain_peer(value: domain_peer::Peer) -> protocol_peer::Peer {
#[allow(deprecated)]
protocol_peer::Peer {
peer_id: from_domain_peer_id(value.peer_id),
peer_addr: value.peer_addr.to_string(),
updated: value.updated.as_millis(),
updated_milliseconds_ago: value.updated.as_millis(),
uploaded: value.uploaded.0,
downloaded: value.downloaded.0,
left: value.left.0,
event: format!("{:?}", value.event),
}
}
/// Convert a domain [`PeerId`] into a protocol [`protocol_peer::Id`].
#[must_use]
pub fn from_domain_peer_id(peer_id: PeerId) -> protocol_peer::Id {
let pid = domain_peer::Id::from(peer_id);
protocol_peer::Id {
id: pid.to_hex_string(),
client: pid.get_client_name(),
}
}
/// Convert a domain [`Info`] into a protocol [`Torrent`].
#[must_use]
pub fn from_domain_info(info: Info) -> Torrent {
let peers: Option<Vec<protocol_peer::Peer>> = info.peers.map(|peers| peers.into_iter().map(from_domain_peer).collect());
Torrent {
info_hash: info.info_hash.to_string(),
seeders: info.seeders,
completed: info.completed,
leechers: info.leechers,
peers,
}
}
/// Build a vector of [`ListItem`] from domain [`BasicInfo`] slices.
#[must_use]
pub fn list_items_from_domain(basic_info_vec: &[BasicInfo]) -> Vec<ListItem> {
basic_info_vec.iter().map(list_item_from_domain).collect()
}
/// Build a [`ListItem`] from a domain [`BasicInfo`].
#[must_use]
pub fn list_item_from_domain(basic_info: &BasicInfo) -> ListItem {
ListItem {
info_hash: basic_info.info_hash.to_string(),
seeders: basic_info.seeders,
completed: basic_info.completed,
leechers: basic_info.leechers,
}
}