rsolace 0.3.9

Solace bindings for the Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Session properties for Solace client connections.
//!
//! This module provides the `SessionProps` struct for configuring Solace session connections,
//! including SSL/TLS and OAuth2 authentication options.

use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr::null;

use crate::utils::ConvertToCString;

/// Session properties for configuring a Solace client connection.
///
/// Use the builder pattern to configure the session:
/// ```ignore
/// let props = SessionProps::default()
///     .host("tcp://localhost:55555")
///     .vpn("default")
///     .username("user")
///     .password("pass");
/// ```
#[derive(Debug)]
pub struct SessionProps {
    // === Required fields ===
    username: CString,
    password: CString,
    host: CString,
    vpn: CString,
    client_name: CString,
    connect_timeout_ms: CString,
    tcp_nodelay: CString,
    keep_alive_int_ms: CString,
    keep_alive_limit: CString,
    compression_level: CString,
    generate_rcv_timestamps: CString,
    generate_send_timestamps: CString,
    generate_sender_id: CString,
    generate_sequence_number: CString,
    connect_retries: CString,
    reconnect_retries: CString,
    reconnect_retry_wait_ms: CString,
    reapply_subscriptions: CString,

    // === SSL optional fields ===
    ssl_trust_store_dir: Option<CString>,
    ssl_validate_certificate: Option<CString>,
    ssl_validate_certificate_date: Option<CString>,
    ssl_validate_certificate_host: Option<CString>,
    ssl_cipher_suites: Option<CString>,
    ssl_excluded_protocols: Option<CString>,
    ssl_client_certificate_file: Option<CString>,
    ssl_client_private_key_file: Option<CString>,
    ssl_client_private_key_file_password: Option<CString>,
    ssl_trusted_common_name_list: Option<CString>,

    // === OAuth2/Authentication optional fields ===
    authentication_scheme: Option<CString>,
    oauth2_access_token: Option<CString>,
    oauth2_issuer_identifier: Option<CString>,
    oidc_id_token: Option<CString>,
}

impl SessionProps {
    /// Convert session properties to a C-compatible array of property pointers.
    ///
    /// Returns a `Vec` of property key-value pairs terminated by a null pointer,
    /// suitable for passing to Solace C API functions.
    pub fn to_c(&self) -> Vec<*const c_char> {
        let mut props = Vec::with_capacity(60);

        macro_rules! add_prop {
            ($key:expr, $val:expr) => {
                props.push($key.as_ptr() as *const c_char);
                props.push($val.as_ptr() as *const c_char);
            };
        }

        macro_rules! add_optional {
            ($key:expr, $val:expr) => {
                if let Some(ref v) = $val {
                    props.push($key.as_ptr() as *const c_char);
                    props.push(v.as_ptr() as *const c_char);
                }
            };
        }

        // Required fields
        add_prop!(rsolace_sys::SOLCLIENT_SESSION_PROP_HOST, self.host);
        add_prop!(rsolace_sys::SOLCLIENT_SESSION_PROP_VPN_NAME, self.vpn);
        add_prop!(rsolace_sys::SOLCLIENT_SESSION_PROP_USERNAME, self.username);
        add_prop!(rsolace_sys::SOLCLIENT_SESSION_PROP_PASSWORD, self.password);
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_COMPRESSION_LEVEL,
            self.compression_level
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_CLIENT_NAME,
            self.client_name
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_CONNECT_TIMEOUT_MS,
            self.connect_timeout_ms
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_TCP_NODELAY,
            self.tcp_nodelay
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_GENERATE_RCV_TIMESTAMPS,
            self.generate_rcv_timestamps
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_GENERATE_SEND_TIMESTAMPS,
            self.generate_send_timestamps
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_GENERATE_SENDER_ID,
            self.generate_sender_id
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_GENERATE_SEQUENCE_NUMBER,
            self.generate_sequence_number
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_CONNECT_RETRIES,
            self.connect_retries
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_RECONNECT_RETRIES,
            self.reconnect_retries
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_RECONNECT_RETRY_WAIT_MS,
            self.reconnect_retry_wait_ms
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_REAPPLY_SUBSCRIPTIONS,
            self.reapply_subscriptions
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_KEEP_ALIVE_INT_MS,
            self.keep_alive_int_ms
        );
        add_prop!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_KEEP_ALIVE_LIMIT,
            self.keep_alive_limit
        );

        // SSL optional fields
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_TRUST_STORE_DIR,
            self.ssl_trust_store_dir
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_VALIDATE_CERTIFICATE,
            self.ssl_validate_certificate
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_VALIDATE_CERTIFICATE_DATE,
            self.ssl_validate_certificate_date
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_VALIDATE_CERTIFICATE_HOST,
            self.ssl_validate_certificate_host
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_CIPHER_SUITES,
            self.ssl_cipher_suites
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_EXCLUDED_PROTOCOLS,
            self.ssl_excluded_protocols
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_CLIENT_CERTIFICATE_FILE,
            self.ssl_client_certificate_file
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_CLIENT_PRIVATE_KEY_FILE,
            self.ssl_client_private_key_file
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_CLIENT_PRIVATE_KEY_FILE_PASSWORD,
            self.ssl_client_private_key_file_password
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_SSL_TRUSTED_COMMON_NAME_LIST,
            self.ssl_trusted_common_name_list
        );

        // OAuth2/Authentication optional fields
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_AUTHENTICATION_SCHEME,
            self.authentication_scheme
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_OAUTH2_ACCESS_TOKEN,
            self.oauth2_access_token
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_OAUTH2_ISSUER_IDENTIFIER,
            self.oauth2_issuer_identifier
        );
        add_optional!(
            rsolace_sys::SOLCLIENT_SESSION_PROP_OIDC_ID_TOKEN,
            self.oidc_id_token
        );

        // Null terminator
        props.push(null());
        props
    }

    // === Required field builder methods ===

    pub fn username(mut self, username: &str) -> Self {
        self.username = username.to_cstring();
        self
    }

    pub fn password(mut self, password: &str) -> Self {
        self.password = password.to_cstring();
        self
    }

    pub fn host(mut self, host: &str) -> Self {
        self.host = host.to_cstring();
        self
    }

    pub fn vpn(mut self, vpn: &str) -> Self {
        self.vpn = vpn.to_cstring();
        self
    }

    pub fn compression_level(mut self, compression_level: u32) -> Self {
        assert!(compression_level < 10);
        self.compression_level = compression_level.to_cstring();
        self
    }

    pub fn connect_timeout_ms(mut self, timeout: u32) -> Self {
        self.connect_timeout_ms = timeout.to_cstring();
        self
    }

    pub fn tcp_nodelay(mut self, enable: bool) -> Self {
        self.tcp_nodelay = enable.to_cstring();
        self
    }

    pub fn client_name(mut self, client_name: &str) -> Self {
        self.client_name = client_name.to_cstring();
        self
    }

    pub fn keep_alive_int_ms(mut self, keep_alive_int_ms: u32) -> Self {
        self.keep_alive_int_ms = keep_alive_int_ms.to_cstring();
        self
    }

    pub fn keep_alive_limit(mut self, keep_alive_limit: u32) -> Self {
        self.keep_alive_limit = keep_alive_limit.to_cstring();
        self
    }

    pub fn generate_rcv_timestamps(mut self, generate_rcv_timestamps: bool) -> Self {
        self.generate_rcv_timestamps = generate_rcv_timestamps.to_cstring();
        self
    }

    pub fn generate_send_timestamps(mut self, generate_send_timestamps: bool) -> Self {
        self.generate_send_timestamps = generate_send_timestamps.to_cstring();
        self
    }

    pub fn generate_sender_id(mut self, generate_sender_id: bool) -> Self {
        self.generate_sender_id = generate_sender_id.to_cstring();
        self
    }

    pub fn generate_sequence_number(mut self, generate_sequence_number: bool) -> Self {
        self.generate_sequence_number = generate_sequence_number.to_cstring();
        self
    }

    /// Set number of connection retries.
    /// - `0`: no retries (try once and give up)
    /// - `-1`: retry forever
    /// - positive: retry N times
    pub fn connect_retries(mut self, connect_retries: i32) -> Self {
        self.connect_retries = connect_retries.to_cstring();
        self
    }

    /// Set number of reconnection retries after connection goes down.
    /// - `0`: no automatic reconnection
    /// - `-1`: reconnect forever
    /// - positive: retry N times
    pub fn reconnect_retries(mut self, reconnect_retries: i32) -> Self {
        self.reconnect_retries = reconnect_retries.to_cstring();
        self
    }

    pub fn reconnect_retry_wait_ms(mut self, reconnect_retry_wait_ms: u32) -> Self {
        self.reconnect_retry_wait_ms = reconnect_retry_wait_ms.to_cstring();
        self
    }

    pub fn reapply_subscriptions(mut self, reapply_subscriptions: bool) -> Self {
        self.reapply_subscriptions = reapply_subscriptions.to_cstring();
        self
    }

    // === SSL builder methods ===

    /// Set the SSL trust store directory path.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_trust_store_dir(mut self, dir: Option<&str>) -> Self {
        self.ssl_trust_store_dir = dir.map(|v| v.to_cstring());
        self
    }

    /// Set whether to validate the server certificate.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value
    pub fn ssl_validate_certificate(mut self, validate: Option<bool>) -> Self {
        self.ssl_validate_certificate = validate.map(|v| v.to_cstring());
        self
    }

    /// Set whether to validate the certificate date.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value
    pub fn ssl_validate_certificate_date(mut self, validate: Option<bool>) -> Self {
        self.ssl_validate_certificate_date = validate.map(|v| v.to_cstring());
        self
    }

    /// Set whether to validate the certificate host name.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value
    pub fn ssl_validate_certificate_host(mut self, validate: Option<bool>) -> Self {
        self.ssl_validate_certificate_host = validate.map(|v| v.to_cstring());
        self
    }

    /// Set the SSL cipher suites to use.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_cipher_suites(mut self, suites: Option<&str>) -> Self {
        self.ssl_cipher_suites = suites.map(|v| v.to_cstring());
        self
    }

    /// Set the SSL protocols to exclude.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_excluded_protocols(mut self, protocols: Option<&str>) -> Self {
        self.ssl_excluded_protocols = protocols.map(|v| v.to_cstring());
        self
    }

    /// Set the path to the client certificate file for mutual TLS.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_client_certificate_file(mut self, path: Option<&str>) -> Self {
        self.ssl_client_certificate_file = path.map(|v| v.to_cstring());
        self
    }

    /// Set the path to the client private key file for mutual TLS.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_client_private_key_file(mut self, path: Option<&str>) -> Self {
        self.ssl_client_private_key_file = path.map(|v| v.to_cstring());
        self
    }

    /// Set the password for the client private key file.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_client_private_key_file_password(mut self, password: Option<&str>) -> Self {
        self.ssl_client_private_key_file_password = password.map(|v| v.to_cstring());
        self
    }

    /// Set the list of trusted common names for certificate validation.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn ssl_trusted_common_name_list(mut self, names: Option<&str>) -> Self {
        self.ssl_trusted_common_name_list = names.map(|v| v.to_cstring());
        self
    }

    // === OAuth2/Authentication builder methods ===

    /// Set the authentication scheme (BASIC, OAUTH2, CLIENT_CERTIFICATE, GSS_KRB).
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn authentication_scheme(mut self, scheme: Option<&str>) -> Self {
        self.authentication_scheme = scheme.map(|v| v.to_cstring());
        self
    }

    /// Set the OAuth2 access token.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn oauth2_access_token(mut self, token: Option<&str>) -> Self {
        self.oauth2_access_token = token.map(|v| v.to_cstring());
        self
    }

    /// Set the OAuth2 issuer identifier.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn oauth2_issuer_identifier(mut self, issuer: Option<&str>) -> Self {
        self.oauth2_issuer_identifier = issuer.map(|v| v.to_cstring());
        self
    }

    /// Set the OIDC ID token.
    /// - `None` → property not sent to Solace
    /// - `Some(value)` → property sent with the value (including empty string)
    pub fn oidc_id_token(mut self, token: Option<&str>) -> Self {
        self.oidc_id_token = token.map(|v| v.to_cstring());
        self
    }
}

impl Default for SessionProps {
    fn default() -> Self {
        Self {
            username: "".to_cstring(),
            password: "".to_cstring(),
            host: "".to_cstring(),
            vpn: "".to_cstring(),
            client_name: "".to_cstring(),
            connect_timeout_ms: 30000.to_cstring(),
            tcp_nodelay: true.to_cstring(),
            keep_alive_int_ms: 3000.to_cstring(),
            keep_alive_limit: 3.to_cstring(),
            compression_level: 0.to_cstring(),
            generate_rcv_timestamps: false.to_cstring(),
            generate_send_timestamps: false.to_cstring(),
            generate_sender_id: false.to_cstring(),
            generate_sequence_number: false.to_cstring(),
            connect_retries: 0.to_cstring(),
            reconnect_retries: 0.to_cstring(),
            reconnect_retry_wait_ms: 3000.to_cstring(),
            reapply_subscriptions: false.to_cstring(),
            // SSL optional fields - all None by default
            ssl_trust_store_dir: None,
            ssl_validate_certificate: None,
            ssl_validate_certificate_date: None,
            ssl_validate_certificate_host: None,
            ssl_cipher_suites: None,
            ssl_excluded_protocols: None,
            ssl_client_certificate_file: None,
            ssl_client_private_key_file: None,
            ssl_client_private_key_file_password: None,
            ssl_trusted_common_name_list: None,
            // OAuth2/Authentication optional fields - all None by default
            authentication_scheme: None,
            oauth2_access_token: None,
            oauth2_issuer_identifier: None,
            oidc_id_token: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_session_props() {
        let props = SessionProps::default();
        let c_props = props.to_c();
        // Should have required props (18 pairs = 36 entries) + null terminator
        assert!(c_props.len() >= 37);
        // Last element should be null
        assert!(c_props.last().unwrap().is_null());
    }

    #[test]
    fn test_ssl_trust_store_dir_none() {
        let props = SessionProps::default().ssl_trust_store_dir(None);
        assert!(props.ssl_trust_store_dir.is_none());
    }

    #[test]
    fn test_ssl_trust_store_dir_empty_string() {
        // Empty string should be set (not ignored)
        let props = SessionProps::default().ssl_trust_store_dir(Some(""));
        assert!(props.ssl_trust_store_dir.is_some());
    }

    #[test]
    fn test_ssl_trust_store_dir_with_path() {
        let props = SessionProps::default().ssl_trust_store_dir(Some("/path/to/certs"));
        assert!(props.ssl_trust_store_dir.is_some());
    }

    #[test]
    fn test_ssl_validate_certificate() {
        let props = SessionProps::default().ssl_validate_certificate(Some(false));
        assert!(props.ssl_validate_certificate.is_some());
    }

    #[test]
    fn test_ssl_validate_certificate_none() {
        let props = SessionProps::default().ssl_validate_certificate(None);
        assert!(props.ssl_validate_certificate.is_none());
    }

    #[test]
    fn test_builder_chain() {
        let props = SessionProps::default()
            .host("tcps://localhost:55443")
            .vpn("default")
            .username("user")
            .password("pass")
            .ssl_trust_store_dir(Some("/certs"))
            .ssl_validate_certificate(Some(true));

        let c_props = props.to_c();
        // Should have more entries than default due to SSL props
        assert!(c_props.len() > 37);
    }
}