tsubakuro_rust_core/session/
option.rs1use std::{path::Path, time::Duration};
2
3use crate::{
4 error::TgError,
5 prelude::{
6 r#type::large_object::{LargeObjectRecvPathMapping, LargeObjectSendPathMapping},
7 Credential,
8 },
9};
10
11use super::endpoint::Endpoint;
12
13const DEFAULT_VALIDITY_PERIOD_SECONDS: u64 = 300;
15
16#[derive(Debug, Clone)]
41pub struct ConnectionOption {
42 endpoint: Option<Endpoint>,
43 credential: Credential,
44 validity_period: Duration,
45 application_name: Option<String>,
46 session_label: Option<String>,
47 keep_alive: Duration,
48 lob_send_path_mapping: LargeObjectSendPathMapping,
49 lob_recv_path_mapping: LargeObjectRecvPathMapping,
50 default_timeout: Duration,
51 send_timeout: Duration,
52 recv_timeout: Duration,
53}
54
55impl Default for ConnectionOption {
56 fn default() -> Self {
57 Self::new()
58 }
59}
60
61impl ConnectionOption {
62 pub fn new() -> ConnectionOption {
64 ConnectionOption {
65 endpoint: None,
66 credential: Credential::Null,
67 validity_period: Duration::from_secs(DEFAULT_VALIDITY_PERIOD_SECONDS),
68 application_name: None,
69 session_label: None,
70 keep_alive: Duration::from_secs(60),
71 lob_send_path_mapping: LargeObjectSendPathMapping::new(),
72 lob_recv_path_mapping: LargeObjectRecvPathMapping::new(),
73 default_timeout: Duration::ZERO,
74 send_timeout: Duration::ZERO,
75 recv_timeout: Duration::ZERO,
76 }
77 }
78
79 pub fn set_endpoint(&mut self, endpoint: Endpoint) {
81 self.endpoint = Some(endpoint);
82 }
83
84 pub fn set_endpoint_url(&mut self, endpoint: &str) -> Result<(), TgError> {
89 let endpoint = Endpoint::parse(endpoint)?;
90 self.set_endpoint(endpoint);
91 Ok(())
92 }
93
94 pub fn endpoint(&self) -> Option<&Endpoint> {
96 self.endpoint.as_ref()
97 }
98
99 pub fn set_credential(&mut self, credential: Credential) {
103 self.credential = credential;
104 }
105
106 pub fn credential(&self) -> &Credential {
110 &self.credential
111 }
112
113 pub fn set_validity_period(&mut self, duration: Duration) {
119 self.validity_period = duration;
120 }
121
122 pub fn validity_period(&self) -> Duration {
128 self.validity_period
129 }
130
131 pub fn set_application_name(&mut self, name: &str) {
133 self.application_name = Some(name.to_string());
134 }
135
136 pub fn application_name(&self) -> Option<&String> {
138 self.application_name.as_ref()
139 }
140
141 pub fn set_session_label(&mut self, label: &str) {
143 self.session_label = Some(label.to_string());
144 }
145
146 pub fn session_label(&self) -> Option<&String> {
148 self.session_label.as_ref()
149 }
150
151 pub fn set_keep_alive(&mut self, keep_alive: Duration) {
155 self.keep_alive = keep_alive;
156 }
157
158 pub fn keep_alive(&self) -> Duration {
160 self.keep_alive
161 }
162
163 pub fn add_large_object_path_mapping<T: AsRef<Path>>(
167 &mut self,
168 client_path: T,
169 server_path: &str,
170 ) {
171 self.add_large_object_path_mapping_on_send(&client_path, server_path);
172 self.add_large_object_path_mapping_on_recv(server_path, &client_path);
173 }
174
175 pub fn add_large_object_path_mapping_on_send<T: AsRef<Path>>(
179 &mut self,
180 client_path: T,
181 server_path: &str,
182 ) {
183 self.lob_send_path_mapping.add(client_path, server_path);
184 }
185
186 pub fn add_large_object_path_mapping_on_recv<T: AsRef<Path>>(
190 &mut self,
191 server_path: &str,
192 client_path: T,
193 ) {
194 self.lob_recv_path_mapping.add(server_path, client_path);
195 }
196
197 pub(crate) fn large_object_path_mapping_on_send(&self) -> &LargeObjectSendPathMapping {
198 &self.lob_send_path_mapping
199 }
200
201 pub(crate) fn large_object_path_mapping_on_recv(&self) -> &LargeObjectRecvPathMapping {
202 &self.lob_recv_path_mapping
203 }
204
205 pub fn set_default_timeout(&mut self, timeout: Duration) {
207 self.default_timeout = timeout;
208 }
209
210 pub fn default_timeout(&self) -> Duration {
212 self.default_timeout
213 }
214
215 pub fn set_send_timeout(&mut self, timeout: Duration) {
217 self.send_timeout = timeout;
218 }
219
220 pub fn send_timeout(&self) -> Duration {
222 self.send_timeout
223 }
224
225 pub fn set_recv_timeout(&mut self, timeout: Duration) {
227 self.recv_timeout = timeout;
228 }
229
230 pub fn recv_timeout(&self) -> Duration {
232 self.recv_timeout
233 }
234}
235
236#[cfg(test)]
237mod test {
238 use super::*;
239
240 #[test]
241 fn endpoint() {
242 let mut option = ConnectionOption::new();
243
244 let endpoint: Endpoint = Endpoint::parse("tcp://localhost:12345").unwrap();
245 option.set_endpoint(endpoint);
246
247 assert_eq!(
248 Some(&Endpoint::Tcp("localhost".to_string(), 12345)),
249 option.endpoint()
250 );
251 }
252
253 #[test]
254 fn endpoint_url_str() {
255 let mut option = ConnectionOption::new();
256
257 let endpoint: &str = "tcp://localhost:12345";
258 option.set_endpoint_url(endpoint).unwrap();
259
260 assert_eq!(
261 Some(&Endpoint::Tcp("localhost".to_string(), 12345)),
262 option.endpoint()
263 );
264 }
265
266 #[test]
267 fn endpoint_url_string() {
268 let mut option = ConnectionOption::new();
269
270 let endpoint: String = "tcp://localhost:12345".to_string();
271 option.set_endpoint_url(&endpoint).unwrap();
272
273 assert_eq!(
274 Some(&Endpoint::Tcp("localhost".to_string(), 12345)),
275 option.endpoint()
276 );
277 }
278
279 #[test]
280 fn application_name_str() {
281 let mut option = ConnectionOption::new();
282
283 let name: &str = "appname-test";
284 option.set_application_name(name);
285
286 assert_eq!(Some(&name.to_string()), option.application_name());
287 }
288
289 #[test]
290 fn application_name_string() {
291 let mut option = ConnectionOption::new();
292
293 let name: String = "appname-test".to_string();
294 option.set_application_name(&name);
295
296 assert_eq!(Some(&name), option.application_name());
297 }
298
299 #[test]
300 fn label_str() {
301 let mut option = ConnectionOption::new();
302
303 let label: &str = "label-test";
304 option.set_session_label(label);
305
306 assert_eq!(Some(&label.to_string()), option.session_label());
307 }
308
309 #[test]
310 fn label_string() {
311 let mut option = ConnectionOption::new();
312
313 let label: String = "label-test".to_string();
314 option.set_session_label(&label);
315
316 assert_eq!(Some(&label), option.session_label());
317 }
318
319 #[test]
320 fn default_timeout() {
321 let mut option = ConnectionOption::new();
322 assert_eq!(Duration::ZERO, option.default_timeout());
323
324 option.set_default_timeout(Duration::from_secs(123));
325 assert_eq!(Duration::from_secs(123), option.default_timeout());
326 }
327
328 #[test]
329 fn send_timeout() {
330 let mut option = ConnectionOption::new();
331 assert_eq!(Duration::ZERO, option.send_timeout());
332
333 option.set_send_timeout(Duration::from_secs(123));
334 assert_eq!(Duration::from_secs(123), option.send_timeout());
335 }
336
337 #[test]
338 fn recv_timeout() {
339 let mut option = ConnectionOption::new();
340 assert_eq!(Duration::ZERO, option.recv_timeout());
341
342 option.set_recv_timeout(Duration::from_secs(123));
343 assert_eq!(Duration::from_secs(123), option.recv_timeout());
344 }
345}