minecraft_pinger/java/
config.rs1use crate::config::{DEFAULT_PROTOCOL_VERSION, PingConfig, PingConfigBuilder};
2use std::time::Duration;
3
4#[derive(Clone)]
5pub struct JavaPingConfig {
6 common: PingConfig,
7 protocol_version: i32,
8 hostname: Option<String>,
9}
10
11impl JavaPingConfig {
12 pub fn builder() -> JavaPingConfigBuilder {
13 JavaPingConfigBuilder::new()
14 }
15
16 pub fn from(config: &PingConfigBuilder) -> JavaPingConfigBuilder {
17 JavaPingConfigBuilder::from(config)
18 }
19
20 pub fn protocol_version(&self) -> i32 {
21 self.protocol_version
22 }
23
24 pub fn common(&self) -> &PingConfig {
25 &self.common
26 }
27
28 pub fn hostname(&self) -> &Option<String> {
29 &self.hostname
30 }
31}
32
33impl Default for JavaPingConfig {
34 fn default() -> Self {
35 Self::builder().build()
36 }
37}
38
39pub struct JavaPingConfigBuilder {
40 common: PingConfigBuilder,
41 protocol_version: i32,
42 hostname: Option<String>,
43}
44
45impl Default for JavaPingConfigBuilder {
46 fn default() -> Self {
47 Self::new()
48 }
49}
50
51impl JavaPingConfigBuilder {
52 pub fn new() -> Self {
53 JavaPingConfigBuilder {
54 common: PingConfigBuilder::new(),
55 protocol_version: DEFAULT_PROTOCOL_VERSION,
56 hostname: None,
57 }
58 }
59
60 pub fn from(config: &PingConfigBuilder) -> Self {
61 JavaPingConfigBuilder {
62 common: config.clone(),
63 protocol_version: DEFAULT_PROTOCOL_VERSION,
64 hostname: None,
65 }
66 }
67
68 pub fn set_protocol_version(mut self, protocol_version: i32) -> Self {
69 self.protocol_version = protocol_version;
70 self
71 }
72
73 pub fn set_hostname(mut self, hostname: Option<String>) -> Self {
74 self.hostname = hostname;
75 self
76 }
77
78 pub fn set_timeout(mut self, timeout: Duration) -> Self {
79 self.common = self.common.set_timeout(timeout);
80 self
81 }
82
83 pub fn deny_non_public_ips(mut self) -> Self {
84 self.common = self.common.deny_non_public_ips();
85 self
86 }
87
88 pub fn build(self) -> JavaPingConfig {
89 JavaPingConfig {
90 common: self.common.build(),
91 hostname: self.hostname,
92 protocol_version: self.protocol_version,
93 }
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use std::time::Duration;
101
102 #[test]
103 fn test_java_ping_config_builder_default() {
104 let config = JavaPingConfigBuilder::new().build();
105 assert_eq!(config.protocol_version(), DEFAULT_PROTOCOL_VERSION);
106 assert_eq!(config.common().timeout(), Duration::from_secs(1));
107 assert!(config.hostname().is_none());
108 }
109
110 #[test]
111 fn test_java_ping_config_builder_custom_protocol() {
112 let config = JavaPingConfigBuilder::new()
113 .set_protocol_version(765)
114 .build();
115 assert_eq!(config.protocol_version(), 765);
116 }
117
118 #[test]
119 fn test_java_ping_config_builder_custom_hostname() {
120 let config = JavaPingConfigBuilder::new()
121 .set_hostname(Some("custom.example.com".to_string()))
122 .build();
123 assert_eq!(config.hostname(), &Some("custom.example.com".to_string()));
124 }
125
126 #[test]
127 fn test_java_ping_config_builder_custom_timeout() {
128 let config = JavaPingConfigBuilder::new()
129 .set_timeout(Duration::from_secs(5))
130 .build();
131 assert_eq!(config.common().timeout(), Duration::from_secs(5));
132 }
133
134 #[test]
135 fn test_java_ping_config_builder_deny_non_public_ips() {
136 let config = JavaPingConfigBuilder::new().deny_non_public_ips().build();
137 assert!(config.common().ip_filter().is_some());
138 }
139
140 #[test]
141 fn test_java_ping_config_builder_from_ping_config_builder() {
142 let base = PingConfigBuilder::new()
143 .set_timeout(Duration::from_secs(10))
144 .deny_non_public_ips();
145 let config = JavaPingConfigBuilder::from(&base).build();
146 assert_eq!(config.common().timeout(), Duration::from_secs(10));
147 assert!(config.common().ip_filter().is_some());
148 }
149
150 #[test]
151 fn test_java_ping_config_builder_chaining() {
152 let config = JavaPingConfigBuilder::new()
153 .set_protocol_version(766)
154 .set_hostname(Some("test.com".to_string()))
155 .set_timeout(Duration::from_millis(500))
156 .deny_non_public_ips()
157 .build();
158 assert_eq!(config.protocol_version(), 766);
159 assert_eq!(config.hostname(), &Some("test.com".to_string()));
160 assert_eq!(config.common().timeout(), Duration::from_millis(500));
161 assert!(config.common().ip_filter().is_some());
162 }
163
164 #[test]
165 fn test_java_ping_config_default() {
166 let config = JavaPingConfig::default();
167 assert_eq!(config.protocol_version(), DEFAULT_PROTOCOL_VERSION);
168 assert_eq!(config.common().timeout(), Duration::from_secs(1));
169 }
170}