Skip to content

Commit 5fdee78

Browse files
committed
refactor: rename enum variant
1 parent b2fc663 commit 5fdee78

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

packages/tracker-core/src/announce_handler.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl AnnounceHandler {
111111
pub enum PeersWanted {
112112
/// The peer wants as many peers as possible in the announce response.
113113
#[default]
114-
All,
114+
AsManyAsPossible,
115115
/// The peer only wants a certain amount of peers in the announce response.
116116
Only { amount: usize },
117117
}
@@ -124,7 +124,7 @@ impl PeersWanted {
124124

125125
fn limit(&self) -> usize {
126126
match self {
127-
PeersWanted::All => TORRENT_PEERS_LIMIT,
127+
PeersWanted::AsManyAsPossible => TORRENT_PEERS_LIMIT,
128128
PeersWanted::Only { amount } => *amount,
129129
}
130130
}
@@ -133,7 +133,7 @@ impl PeersWanted {
133133
impl From<i32> for PeersWanted {
134134
fn from(value: i32) -> Self {
135135
if value <= 0 {
136-
return PeersWanted::All;
136+
return PeersWanted::AsManyAsPossible;
137137
}
138138

139139
// This conversion is safe because `value > 0`
@@ -148,7 +148,7 @@ impl From<i32> for PeersWanted {
148148
impl From<u32> for PeersWanted {
149149
fn from(value: u32) -> Self {
150150
if value == 0 {
151-
return PeersWanted::All;
151+
return PeersWanted::AsManyAsPossible;
152152
}
153153

154154
let amount = value as usize;
@@ -350,7 +350,8 @@ mod tests {
350350

351351
let mut peer = sample_peer();
352352

353-
let announce_data = announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::All);
353+
let announce_data =
354+
announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
354355

355356
assert_eq!(announce_data.peers, vec![]);
356357
}
@@ -364,11 +365,12 @@ mod tests {
364365
&sample_info_hash(),
365366
&mut previously_announced_peer,
366367
&peer_ip(),
367-
&PeersWanted::All,
368+
&PeersWanted::AsManyAsPossible,
368369
);
369370

370371
let mut peer = sample_peer_2();
371-
let announce_data = announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::All);
372+
let announce_data =
373+
announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
372374

373375
assert_eq!(announce_data.peers, vec![Arc::new(previously_announced_peer)]);
374376
}
@@ -382,15 +384,15 @@ mod tests {
382384
&sample_info_hash(),
383385
&mut previously_announced_peer_1,
384386
&peer_ip(),
385-
&PeersWanted::All,
387+
&PeersWanted::AsManyAsPossible,
386388
);
387389

388390
let mut previously_announced_peer_2 = sample_peer_2();
389391
announce_handler.announce(
390392
&sample_info_hash(),
391393
&mut previously_announced_peer_2,
392394
&peer_ip(),
393-
&PeersWanted::All,
395+
&PeersWanted::AsManyAsPossible,
394396
);
395397

396398
let mut peer = sample_peer_3();
@@ -418,7 +420,7 @@ mod tests {
418420
let mut peer = seeder();
419421

420422
let announce_data =
421-
announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::All);
423+
announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
422424

423425
assert_eq!(announce_data.stats.complete, 1);
424426
}
@@ -430,7 +432,7 @@ mod tests {
430432
let mut peer = leecher();
431433

432434
let announce_data =
433-
announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::All);
435+
announce_handler.announce(&sample_info_hash(), &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
434436

435437
assert_eq!(announce_data.stats.incomplete, 1);
436438
}
@@ -441,11 +443,20 @@ mod tests {
441443

442444
// We have to announce with "started" event because peer does not count if peer was not previously known
443445
let mut started_peer = started_peer();
444-
announce_handler.announce(&sample_info_hash(), &mut started_peer, &peer_ip(), &PeersWanted::All);
446+
announce_handler.announce(
447+
&sample_info_hash(),
448+
&mut started_peer,
449+
&peer_ip(),
450+
&PeersWanted::AsManyAsPossible,
451+
);
445452

446453
let mut completed_peer = completed_peer();
447-
let announce_data =
448-
announce_handler.announce(&sample_info_hash(), &mut completed_peer, &peer_ip(), &PeersWanted::All);
454+
let announce_data = announce_handler.announce(
455+
&sample_info_hash(),
456+
&mut completed_peer,
457+
&peer_ip(),
458+
&PeersWanted::AsManyAsPossible,
459+
);
449460

450461
assert_eq!(announce_data.stats.downloaded, 1);
451462
}
@@ -494,11 +505,11 @@ mod tests {
494505
let mut peer = sample_peer();
495506

496507
peer.event = AnnounceEvent::Started;
497-
let announce_data = announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::All);
508+
let announce_data = announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
498509
assert_eq!(announce_data.stats.downloaded, 0);
499510

500511
peer.event = AnnounceEvent::Completed;
501-
let announce_data = announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::All);
512+
let announce_data = announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
502513
assert_eq!(announce_data.stats.downloaded, 1);
503514

504515
// Remove the newly updated torrent from memory
@@ -533,7 +544,7 @@ mod tests {
533544

534545
#[test]
535546
fn it_should_return_74_at_the_most_if_the_client_wants_them_all() {
536-
let peers_wanted = PeersWanted::All;
547+
let peers_wanted = PeersWanted::AsManyAsPossible;
537548

538549
assert_eq!(peers_wanted.limit(), TORRENT_PEERS_LIMIT);
539550
}

packages/tracker-core/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ mod tests {
460460
&info_hash,
461461
&mut complete_peer,
462462
&IpAddr::V4(Ipv4Addr::new(126, 0, 0, 10)),
463-
&PeersWanted::All,
463+
&PeersWanted::AsManyAsPossible,
464464
);
465465

466466
// Announce an "incomplete" peer for the torrent
@@ -469,7 +469,7 @@ mod tests {
469469
&info_hash,
470470
&mut incomplete_peer,
471471
&IpAddr::V4(Ipv4Addr::new(126, 0, 0, 11)),
472-
&PeersWanted::All,
472+
&PeersWanted::AsManyAsPossible,
473473
);
474474

475475
// Scrape
@@ -510,11 +510,11 @@ mod tests {
510510
let info_hash = "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap(); // DevSkim: ignore DS173237
511511

512512
let mut peer = incomplete_peer();
513-
announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::All);
513+
announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
514514

515515
// Announce twice to force non zeroed swarm metadata
516516
let mut peer = complete_peer();
517-
announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::All);
517+
announce_handler.announce(&info_hash, &mut peer, &peer_ip(), &PeersWanted::AsManyAsPossible);
518518

519519
let scrape_data = scrape_handler.scrape(&vec![info_hash]).await;
520520

src/servers/http/v1/handlers/announce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async fn handle_announce(
175175
let mut peer = peer_from_request(announce_request, &peer_ip);
176176
let peers_wanted = match announce_request.numwant {
177177
Some(numwant) => PeersWanted::only(numwant),
178-
None => PeersWanted::All,
178+
None => PeersWanted::AsManyAsPossible,
179179
};
180180

181181
let announce_data = services::announce::invoke(

src/servers/http/v1/services/announce.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ mod tests {
195195
core_http_tracker_services.http_stats_event_sender.clone(),
196196
sample_info_hash(),
197197
&mut peer,
198-
&PeersWanted::All,
198+
&PeersWanted::AsManyAsPossible,
199199
)
200200
.await;
201201

@@ -232,7 +232,7 @@ mod tests {
232232
http_stats_event_sender,
233233
sample_info_hash(),
234234
&mut peer,
235-
&PeersWanted::All,
235+
&PeersWanted::AsManyAsPossible,
236236
)
237237
.await;
238238
}
@@ -277,7 +277,7 @@ mod tests {
277277
http_stats_event_sender,
278278
sample_info_hash(),
279279
&mut peer,
280-
&PeersWanted::All,
280+
&PeersWanted::AsManyAsPossible,
281281
)
282282
.await;
283283
}
@@ -303,7 +303,7 @@ mod tests {
303303
http_stats_event_sender,
304304
sample_info_hash(),
305305
&mut peer,
306-
&PeersWanted::All,
306+
&PeersWanted::AsManyAsPossible,
307307
)
308308
.await;
309309
}

src/servers/http/v1/services/scrape.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ mod tests {
182182
// Announce a new peer to force scrape data to contain not zeroed data
183183
let mut peer = sample_peer();
184184
let original_peer_ip = peer.ip();
185-
announce_handler.announce(&info_hash, &mut peer, &original_peer_ip, &PeersWanted::All);
185+
announce_handler.announce(&info_hash, &mut peer, &original_peer_ip, &PeersWanted::AsManyAsPossible);
186186

187187
let scrape_data = invoke(&scrape_handler, &http_stats_event_sender, &info_hashes, &original_peer_ip).await;
188188

@@ -267,7 +267,7 @@ mod tests {
267267
// Announce a new peer to force scrape data to contain not zeroed data
268268
let mut peer = sample_peer();
269269
let original_peer_ip = peer.ip();
270-
announce_handler.announce(&info_hash, &mut peer, &original_peer_ip, &PeersWanted::All);
270+
announce_handler.announce(&info_hash, &mut peer, &original_peer_ip, &PeersWanted::AsManyAsPossible);
271271

272272
let scrape_data = fake(&http_stats_event_sender, &info_hashes, &original_peer_ip).await;
273273

0 commit comments

Comments
 (0)