Skip to content

Commit dc153f0

Browse files
committed
refactor: [#255] eliminate duplication in binding collection
- Extract register_binding() helper to remove repeated HashMap operations - Extract register_trackers() to handle multiple tracker instances generically - Add HasBindAddress trait for uniform tracker handling - Service names now declared once at call site (DRY principle) - Reduces code from ~30 lines to ~15 lines in collect_bindings() - Improves testability with focused helper methods
1 parent 170ce02 commit dc153f0

1 file changed

Lines changed: 76 additions & 23 deletions

File tree

  • src/domain/tracker/config

src/domain/tracker/config/mod.rs

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,38 +248,91 @@ impl TrackerConfig {
248248
let mut bindings: HashMap<BindingAddress, Vec<String>> = HashMap::new();
249249

250250
// Add UDP trackers
251-
for (i, udp) in self.udp_trackers.iter().enumerate() {
252-
let binding = BindingAddress::new(udp.bind_address, Protocol::Udp);
253-
bindings
254-
.entry(binding)
255-
.or_default()
256-
.push(format!("UDP Tracker #{}", i + 1));
257-
}
251+
Self::register_trackers(
252+
&mut bindings,
253+
&self.udp_trackers,
254+
Protocol::Udp,
255+
"UDP Tracker",
256+
);
258257

259258
// Add HTTP trackers
260-
for (i, http) in self.http_trackers.iter().enumerate() {
261-
let binding = BindingAddress::new(http.bind_address, Protocol::Tcp);
262-
bindings
263-
.entry(binding)
264-
.or_default()
265-
.push(format!("HTTP Tracker #{}", i + 1));
266-
}
259+
Self::register_trackers(
260+
&mut bindings,
261+
&self.http_trackers,
262+
Protocol::Tcp,
263+
"HTTP Tracker",
264+
);
267265

268266
// Add HTTP API
269-
let api_binding = BindingAddress::new(self.http_api.bind_address, Protocol::Tcp);
270-
bindings
271-
.entry(api_binding)
272-
.or_default()
273-
.push("HTTP API".to_string());
267+
Self::register_binding(
268+
&mut bindings,
269+
self.http_api.bind_address,
270+
Protocol::Tcp,
271+
"HTTP API",
272+
);
274273

275274
// Add Health Check API
276-
let health_binding = BindingAddress::new(self.health_check_api.bind_address, Protocol::Tcp);
275+
Self::register_binding(
276+
&mut bindings,
277+
self.health_check_api.bind_address,
278+
Protocol::Tcp,
279+
"Health Check API",
280+
);
281+
277282
bindings
278-
.entry(health_binding)
279-
.or_default()
280-
.push("Health Check API".to_string());
283+
}
281284

285+
/// Registers multiple tracker instances in the bindings map
286+
///
287+
/// Creates numbered service names for each tracker instance (e.g., "UDP Tracker #1").
288+
fn register_trackers<T>(
289+
bindings: &mut HashMap<BindingAddress, Vec<String>>,
290+
trackers: &[T],
291+
protocol: Protocol,
292+
service_name: &str,
293+
) where
294+
T: HasBindAddress,
295+
{
296+
for (i, tracker) in trackers.iter().enumerate() {
297+
let service_label = format!("{service_name} #{}", i + 1);
298+
Self::register_binding(bindings, tracker.bind_address(), protocol, &service_label);
299+
}
300+
}
301+
302+
/// Registers a single binding in the bindings map
303+
///
304+
/// Associates the given service name with the socket address and protocol.
305+
fn register_binding(
306+
bindings: &mut HashMap<BindingAddress, Vec<String>>,
307+
address: SocketAddr,
308+
protocol: Protocol,
309+
service_name: &str,
310+
) {
311+
let binding = BindingAddress::new(address, protocol);
282312
bindings
313+
.entry(binding)
314+
.or_default()
315+
.push(service_name.to_string());
316+
}
317+
}
318+
319+
/// Trait for types that have a bind address
320+
///
321+
/// Used for generic tracker registration in validation logic.
322+
trait HasBindAddress {
323+
/// Returns the socket address this service binds to
324+
fn bind_address(&self) -> SocketAddr;
325+
}
326+
327+
impl HasBindAddress for UdpTrackerConfig {
328+
fn bind_address(&self) -> SocketAddr {
329+
self.bind_address
330+
}
331+
}
332+
333+
impl HasBindAddress for HttpTrackerConfig {
334+
fn bind_address(&self) -> SocketAddr {
335+
self.bind_address
283336
}
284337
}
285338

0 commit comments

Comments
 (0)