1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum NetMode {
6 Ipv4Only,
8 Ipv6Only,
10 DualStack,
12}
13
14impl Default for NetMode {
15 fn default() -> Self {
16 Self::DualStack
17 }
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct TorrentInfo {
23 pub info_hash: String,
24 pub magnet_link: String,
25 pub name: String,
26 pub total_size: u64,
27 pub files: Vec<FileInfo>,
28 pub piece_length: u64,
29 pub peers: Vec<String>,
30 pub timestamp: u64,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct FileInfo {
36 pub path: String,
37 pub size: u64,
38}
39
40impl TorrentInfo {
41 pub fn format_size(&self) -> String {
42 format_bytes(self.total_size)
43 }
44}
45
46impl FileInfo {
47 pub fn format_size(&self) -> String {
48 format_bytes(self.size)
49 }
50}
51
52fn format_bytes(bytes: u64) -> String {
53 const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
54 let mut size = bytes as f64;
55 let mut unit_index = 0;
56
57 while size >= 1024.0 && unit_index < UNITS.len() - 1 {
58 size /= 1024.0;
59 unit_index += 1;
60 }
61
62 format!("{:.2} {}", size, UNITS[unit_index])
63}
64
65#[derive(Debug, Clone)]
67pub struct DHTOptions {
68 pub port: u16,
70
71 pub auto_metadata: bool,
73
74 pub metadata_timeout: u64,
76
77 pub max_metadata_queue_size: usize,
79
80 pub max_metadata_worker_count: usize,
82
83 pub netmode: NetMode,
85
86 pub node_queue_capacity: usize,
88}
89
90impl Default for DHTOptions {
91 fn default() -> Self {
92 Self {
93 port: 6881, auto_metadata: true,
95 metadata_timeout: 3,
97 max_metadata_queue_size: 100000,
99 max_metadata_worker_count: 1000,
101 netmode: NetMode::Ipv4Only,
103 node_queue_capacity: 100000,
105 }
106 }
107}