1#![warn(missing_docs)]
2pub mod compact;
8mod error;
9mod http;
10mod udp;
11
12pub use compact::{
13 encode_compact_peers, encode_compact_peers6, parse_compact_peers, parse_compact_peers6,
14};
15pub use error::{Error, Result};
16pub use http::{HttpAnnounceResponse, HttpScrapeResponse, HttpTracker};
17pub use udp::{UdpAnnounceResponse, UdpScrapeResponse, UdpTracker, UdpTrackerOption};
18
19use std::net::SocketAddr;
20
21use irontide_core::Id20;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct ScrapeInfo {
26 pub complete: u32,
28 pub incomplete: u32,
30 pub downloaded: u32,
32}
33
34#[must_use]
39pub fn announce_url_to_scrape(url: &str) -> Option<String> {
40 let last_pos = url.rfind("announce")?;
41 let mut result = String::with_capacity(url.len());
42 result.push_str(&url[..last_pos]);
43 result.push_str("scrape");
44 result.push_str(&url[last_pos + "announce".len()..]);
45 Some(result)
46}
47
48#[derive(Debug, Clone)]
50pub struct AnnounceRequest {
51 pub info_hash: Id20,
53 pub peer_id: Id20,
55 pub port: u16,
57 pub uploaded: u64,
59 pub downloaded: u64,
61 pub left: u64,
63 pub event: AnnounceEvent,
65 pub num_want: Option<i32>,
67 pub compact: bool,
69 pub i2p_destination: Option<String>,
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum AnnounceEvent {
76 None = 0,
78 Completed = 1,
80 Started = 2,
82 Stopped = 3,
84}
85
86#[derive(Debug, Clone)]
88pub struct AnnounceResponse {
89 pub interval: u32,
91 pub seeders: Option<u32>,
93 pub leechers: Option<u32>,
95 pub peers: Vec<SocketAddr>,
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn scrape_info_equality() {
105 let a = ScrapeInfo {
106 complete: 5,
107 incomplete: 3,
108 downloaded: 50,
109 };
110 let b = ScrapeInfo {
111 complete: 5,
112 incomplete: 3,
113 downloaded: 50,
114 };
115 assert_eq!(a, b);
116 }
117
118 #[test]
119 fn announce_to_scrape_url_http() {
120 assert_eq!(
121 announce_url_to_scrape("http://t.co/announce"),
122 Some("http://t.co/scrape".into()),
123 );
124 }
125
126 #[test]
127 fn announce_to_scrape_url_with_path() {
128 assert_eq!(
129 announce_url_to_scrape("http://t.co/path/announce"),
130 Some("http://t.co/path/scrape".into()),
131 );
132 }
133
134 #[test]
135 fn announce_to_scrape_url_no_announce() {
136 assert_eq!(announce_url_to_scrape("http://t.co/track"), None);
137 }
138}