Skip to content

Commit 010e6fc

Browse files
committed
refactor: use composition for TrackerPorts in DockerComposeContext
- Changed DockerComposeContext from flattened port fields to composed TrackerPorts field - Added Serialize derive to TrackerPorts for proper JSON serialization - Simplified constructors to use direct field move instead of destructuring - Replaced 3 individual accessor methods with single ports() getter - Updated Tera template to use nested access pattern (ports.field_name) - Maintains all existing test contracts (9/9 tests passing) Benefits: - Single source of truth for port structure (DRY principle) - Better type safety and semantic grouping - Easier maintenance - change port structure in one place - More idiomatic Rust composition pattern
1 parent b056f2a commit 010e6fc

File tree

2 files changed

+17
-37
lines changed

2 files changed

+17
-37
lines changed

src/infrastructure/templating/docker_compose/template/wrappers/docker_compose/context.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ use crate::domain::prometheus::PrometheusConfig;
1616
pub struct DockerComposeContext {
1717
/// Database configuration
1818
pub database: DatabaseConfig,
19-
/// UDP tracker ports
20-
pub udp_tracker_ports: Vec<u16>,
21-
/// HTTP tracker ports
22-
pub http_tracker_ports: Vec<u16>,
23-
/// HTTP API port
24-
pub http_api_port: u16,
19+
/// Tracker port configuration
20+
pub ports: TrackerPorts,
2521
/// Prometheus configuration (optional)
2622
#[serde(skip_serializing_if = "Option::is_none")]
2723
pub prometheus_config: Option<PrometheusConfig>,
@@ -54,9 +50,7 @@ impl DockerComposeContext {
5450
driver: "sqlite3".to_string(),
5551
mysql: None,
5652
},
57-
udp_tracker_ports: ports.udp_tracker_ports,
58-
http_tracker_ports: ports.http_tracker_ports,
59-
http_api_port: ports.http_api_port,
53+
ports,
6054
prometheus_config: None,
6155
}
6256
}
@@ -112,9 +106,7 @@ impl DockerComposeContext {
112106
port,
113107
}),
114108
},
115-
udp_tracker_ports: ports.udp_tracker_ports,
116-
http_tracker_ports: ports.http_tracker_ports,
117-
http_api_port: ports.http_api_port,
109+
ports,
118110
prometheus_config: None,
119111
}
120112
}
@@ -136,22 +128,10 @@ impl DockerComposeContext {
136128
&self.database
137129
}
138130

139-
/// Get the UDP tracker ports
131+
/// Get the tracker ports configuration
140132
#[must_use]
141-
pub fn udp_tracker_ports(&self) -> &[u16] {
142-
&self.udp_tracker_ports
143-
}
144-
145-
/// Get the HTTP tracker ports
146-
#[must_use]
147-
pub fn http_tracker_ports(&self) -> &[u16] {
148-
&self.http_tracker_ports
149-
}
150-
151-
/// Get the HTTP API port
152-
#[must_use]
153-
pub fn http_api_port(&self) -> u16 {
154-
self.http_api_port
133+
pub fn ports(&self) -> &TrackerPorts {
134+
&self.ports
155135
}
156136

157137
/// Get the Prometheus configuration if present
@@ -162,7 +142,7 @@ impl DockerComposeContext {
162142
}
163143

164144
/// Tracker port configuration
165-
#[derive(Debug, Clone)]
145+
#[derive(Serialize, Debug, Clone)]
166146
pub struct TrackerPorts {
167147
/// UDP tracker ports
168148
pub udp_tracker_ports: Vec<u16>,
@@ -226,9 +206,9 @@ mod tests {
226206

227207
assert_eq!(context.database().driver(), "sqlite3");
228208
assert!(context.database().mysql().is_none());
229-
assert_eq!(context.udp_tracker_ports(), &[6868, 6969]);
230-
assert_eq!(context.http_tracker_ports(), &[7070]);
231-
assert_eq!(context.http_api_port(), 1212);
209+
assert_eq!(context.ports().udp_tracker_ports, vec![6868, 6969]);
210+
assert_eq!(context.ports().http_tracker_ports, vec![7070]);
211+
assert_eq!(context.ports().http_api_port, 1212);
232212
}
233213

234214
#[test]
@@ -257,9 +237,9 @@ mod tests {
257237
assert_eq!(mysql.password, "pass456");
258238
assert_eq!(mysql.port, 3306);
259239

260-
assert_eq!(context.udp_tracker_ports(), &[6868, 6969]);
261-
assert_eq!(context.http_tracker_ports(), &[7070]);
262-
assert_eq!(context.http_api_port(), 1212);
240+
assert_eq!(context.ports().udp_tracker_ports, vec![6868, 6969]);
241+
assert_eq!(context.ports().http_tracker_ports, vec![7070]);
242+
assert_eq!(context.ports().http_api_port, 1212);
263243
}
264244

265245
#[test]

templates/docker-compose/docker-compose.yml.tera

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ services:
3939
- backend_network
4040
ports:
4141
# UDP Tracker Ports (dynamically configured)
42-
{%- for port in udp_tracker_ports %}
42+
{%- for port in ports.udp_tracker_ports %}
4343
- {{ port }}:{{ port }}/udp
4444
{%- endfor %}
4545
# HTTP Tracker Ports (dynamically configured)
46-
{%- for port in http_tracker_ports %}
46+
{%- for port in ports.http_tracker_ports %}
4747
- {{ port }}:{{ port }}
4848
{%- endfor %}
4949
# HTTP API Port (dynamically configured)
50-
- {{ http_api_port }}:{{ http_api_port }}
50+
- {{ ports.http_api_port }}:{{ ports.http_api_port }}
5151
volumes:
5252
- ./storage/tracker/lib:/var/lib/torrust/tracker:Z
5353
- ./storage/tracker/log:/var/log/torrust/tracker:Z

0 commit comments

Comments
 (0)