Skip to content

Commit 701cc28

Browse files
committed
step: [#220] implement HttpTrackerSection DTO
1 parent 6c86c44 commit 701cc28

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

docs/implementation-plans/issue-220-test-command-architecture.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ Use this checklist to track implementation progress. **Mark as done after each s
4040

4141
```text
4242
Phase 0: Architecture Fix
43-
[ ] Step 0.1: Create tracker DTO module structure
44-
[ ] Step 0.2: Implement UdpTrackerSection DTO
45-
[ ] Step 0.3: Implement HttpTrackerSection DTO
43+
[x] Step 0.1: Create tracker DTO module structure
44+
[x] Step 0.2: Implement UdpTrackerSection DTO
45+
[x] Step 0.3: Implement HttpTrackerSection DTO
4646
[ ] Step 0.4: Implement HttpApiSection DTO
4747
[ ] Step 0.5: Implement TrackerCoreSection DTO
4848
[ ] Step 0.6: Implement TrackerSection DTO
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use std::net::SocketAddr;
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use crate::application::command_handlers::create::config::errors::CreateConfigError;
6+
use crate::domain::tracker::HttpTrackerConfig;
7+
8+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9+
pub struct HttpTrackerSection {
10+
pub bind_address: String,
11+
}
12+
13+
impl HttpTrackerSection {
14+
/// Converts this DTO to a domain `HttpTrackerConfig`
15+
///
16+
/// # Errors
17+
///
18+
/// Returns `CreateConfigError::InvalidBindAddress` if the bind address cannot be parsed as a valid IP:PORT combination.
19+
pub fn to_http_tracker_config(&self) -> Result<HttpTrackerConfig, CreateConfigError> {
20+
// Validate that the bind address can be parsed as SocketAddr
21+
let _bind_address = self.bind_address.parse::<SocketAddr>().map_err(|e| {
22+
CreateConfigError::InvalidBindAddress {
23+
address: self.bind_address.clone(),
24+
source: e,
25+
}
26+
})?;
27+
28+
// For now, keep as String since domain type still uses String
29+
// This will be updated in Step 0.7 when we enhance domain types
30+
Ok(HttpTrackerConfig {
31+
bind_address: self.bind_address.clone(),
32+
})
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[test]
41+
fn it_should_convert_valid_bind_address_to_http_tracker_config() {
42+
let section = HttpTrackerSection {
43+
bind_address: "0.0.0.0:7070".to_string(),
44+
};
45+
46+
let result = section.to_http_tracker_config();
47+
assert!(result.is_ok());
48+
49+
let config = result.unwrap();
50+
assert_eq!(config.bind_address, "0.0.0.0:7070");
51+
}
52+
53+
#[test]
54+
fn it_should_fail_for_invalid_bind_address() {
55+
let section = HttpTrackerSection {
56+
bind_address: "not-valid".to_string(),
57+
};
58+
59+
let result = section.to_http_tracker_config();
60+
assert!(result.is_err());
61+
62+
if let Err(CreateConfigError::InvalidBindAddress { address, .. }) = result {
63+
assert_eq!(address, "not-valid");
64+
} else {
65+
panic!("Expected InvalidBindAddress error");
66+
}
67+
}
68+
69+
#[test]
70+
fn it_should_be_serializable() {
71+
let section = HttpTrackerSection {
72+
bind_address: "0.0.0.0:7070".to_string(),
73+
};
74+
75+
let json = serde_json::to_string(&section).unwrap();
76+
assert!(json.contains("bind_address"));
77+
assert!(json.contains("0.0.0.0:7070"));
78+
}
79+
80+
#[test]
81+
fn it_should_be_deserializable() {
82+
let json = r#"{"bind_address":"0.0.0.0:7070"}"#;
83+
let section: HttpTrackerSection = serde_json::from_str(json).unwrap();
84+
assert_eq!(section.bind_address, "0.0.0.0:7070");
85+
}
86+
}

src/application/command_handlers/create/config/tracker/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! environment creation. These types use raw primitives (String) for
55
//! JSON deserialization and convert to rich domain types (`SocketAddr`).
66
7+
mod http_tracker_section;
78
mod udp_tracker_section;
89

10+
pub use http_tracker_section::HttpTrackerSection;
911
pub use udp_tracker_section::UdpTrackerSection;

0 commit comments

Comments
 (0)