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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! BitTorrent download support.
//!
//! This module provides torrent downloading capabilities through multiple backends:
//!
//! - **Native** (`torrent-native` feature): Built-in BitTorrent client using librqbit
//! - **Transmission** (`torrent-transmission` feature): Transmission daemon RPC
//! - **External**: Opens magnet links in the system's default torrent client
//!
//! # Native Torrent Client
//!
//! The native client provides full BitTorrent protocol support:
//! - Magnet link parsing and metadata download
//! - DHT (Distributed Hash Table) for peer discovery
//! - Parallel piece downloading
//! - Progress callbacks for UI integration
//!
//! # Example
//!
//! ```rust,no_run
//! use kget::torrent::{download_magnet, TorrentCallbacks};
//! use kget::{ProxyConfig, Optimizer};
//! use std::sync::Arc;
//!
//! let callbacks = TorrentCallbacks {
//! status: Some(Arc::new(|msg| println!("{}", msg))),
//! progress: Some(Arc::new(|p| println!("{:.1}%", p * 100.0))),
//! };
//!
//! download_magnet(
//! "magnet:?xt=urn:btih:...",
//! "./downloads",
//! false,
//! ProxyConfig::default(),
//! Optimizer::new(),
//! callbacks,
//! ).expect("Torrent download failed");
//! ```
//!
//! # Backend Selection
//!
//! The backend is selected via the `KGET_TORRENT_BACKEND` environment variable:
//! - `native`: Use built-in client (requires `torrent-native` feature)
//! - `transmission`: Use Transmission RPC (requires `torrent-transmission` feature)
//! - Any other value: Open in system's default torrent client
//!
//! If not set, defaults to `native` when available, otherwise `external`.
use Error;
use Arc;
use crateProxyConfig;
use crateOptimizer;
/// Type alias for status message callbacks.
pub type StatusCb = ;
/// Type alias for progress callbacks (0.0 to 1.0).
pub type ProgressCb = ;
/// Callbacks for torrent download progress and status updates.
///
/// Both callbacks are optional. If not provided, no updates are sent.
///
/// # Example
///
/// ```rust
/// use kget::torrent::TorrentCallbacks;
/// use std::sync::Arc;
///
/// // With callbacks
/// let callbacks = TorrentCallbacks {
/// status: Some(Arc::new(|msg| println!("Status: {}", msg))),
/// progress: Some(Arc::new(|p| println!("Progress: {:.1}%", p * 100.0))),
/// };
///
/// // Without callbacks
/// let silent = TorrentCallbacks::default();
/// ```
/// Return true when a magnet link looks like a supported BitTorrent magnet.
/// Download a torrent from a magnet link.
///
/// This function automatically selects the best available backend:
/// 1. Native client (if `torrent-native` feature is enabled)
/// 2. Transmission RPC (if `torrent-transmission` feature is enabled)
/// 3. External client (opens in system default torrent app)
///
/// Override the backend with `KGET_TORRENT_BACKEND` environment variable.
///
/// # Arguments
///
/// * `magnet` - Magnet link starting with `magnet:?`
/// * `output_dir` - Directory to save downloaded files
/// * `quiet` - Suppress console output
/// * `proxy` - Proxy configuration (native backend only)
/// * `optimizer` - Optimizer for peer limits
/// * `cb` - Callbacks for progress and status updates
///
/// # Example
///
/// ```rust,no_run
/// use kget::torrent::{download_magnet, TorrentCallbacks};
/// use kget::{ProxyConfig, Optimizer};
/// use std::sync::Arc;
///
/// // Simple download
/// download_magnet(
/// "magnet:?xt=urn:btih:HASH&dn=filename",
/// "/home/user/Downloads",
/// false,
/// ProxyConfig::default(),
/// Optimizer::new(),
/// TorrentCallbacks::default(),
/// ).unwrap();
///
/// // With progress tracking
/// let callbacks = TorrentCallbacks {
/// status: Some(Arc::new(|msg| println!("{}", msg))),
/// progress: Some(Arc::new(|p| {
/// update_progress_bar(p);
/// })),
/// };
///
/// download_magnet(
/// "magnet:?xt=urn:btih:HASH",
/// "./downloads",
/// true, // quiet mode
/// ProxyConfig::default(),
/// Optimizer::new(),
/// callbacks,
/// ).unwrap();
///
/// fn update_progress_bar(p: f32) {
/// // Update UI
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - Magnet link is invalid
/// - Network connection fails
/// - Output directory cannot be accessed
/// - Download is interrupted