From e1c0e90581b7ce97f87ddf43267ceb32e28447e3 Mon Sep 17 00:00:00 2001 From: Daniel López Azaña Date: Thu, 28 Mar 2024 19:19:46 +0100 Subject: ✨ (client_listener.rs): Implement Debug trait for ClientListener for better logging ♻️ (connection_details.rs): Refactor ConnectionDetails to use Option for optional fields ♻️ (connection_details.rs): Change new constructor to accept &str and convert to String ✨ (connection_details.rs): Implement Debug trait for ConnectionDetails ♻️ (connection_options.rs): Implement Debug trait for ConnectionOptions ♻️ (lightstreamer_client.rs): Refactor LightstreamerClient to use Option for server_address and adapter_set ✨ (lightstreamer_client.rs): Implement Debug trait for LightstreamerClient ♻️ (main.rs): Update subscribe_to_channel function to use new control.txt URL and parameters ♻️ (main.rs): Update main function to use Option<&str> when creating LightstreamerClient ✨ (proxy.rs): Add Proxy struct and ProxyType enum to handle proxy configurations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lightstreamer_client.rs | 66 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 8 deletions(-) (limited to 'src/lightstreamer_client.rs') diff --git a/src/lightstreamer_client.rs b/src/lightstreamer_client.rs index f91e843..214a722 100644 --- a/src/lightstreamer_client.rs +++ b/src/lightstreamer_client.rs @@ -6,6 +6,7 @@ use crate::subscription::Subscription; use crate::IllegalStateException; use cookie::Cookie; +use std::fmt::{self, Debug, Formatter}; /// Facade class for the management of the communication to Lightstreamer Server. Used to provide /// configuration settings, event handlers, operations for the control of the connection lifecycle, @@ -45,8 +46,8 @@ use cookie::Cookie; /// * `IllegalArgumentException`: if a not valid address is passed. See `ConnectionDetails.setServerAddress()` /// for details. pub struct LightstreamerClient { - server_address: String, - adapter_set: String, + server_address: Option, + adapter_set: Option, /// Data object that contains the details needed to open a connection to a Lightstreamer Server. /// This instance is set up by the `LightstreamerClient` object at its own creation. Properties /// of this object can be overwritten by values received from a Lightstreamer Server. @@ -59,6 +60,19 @@ pub struct LightstreamerClient { subscriptions: Vec, } +impl Debug for LightstreamerClient { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("LightstreamerClient") + .field("server_address", &self.server_address) + .field("adapter_set", &self.adapter_set) + .field("connection_details", &self.connection_details) + .field("connection_options", &self.connection_options) + .field("listeners", &self.listeners) + .field("subscriptions", &self.subscriptions) + .finish() + } +} + impl LightstreamerClient { /// A constant string representing the name of the library. pub const LIB_NAME: &'static str = "rust_client"; @@ -232,13 +246,49 @@ impl LightstreamerClient { &self.subscriptions } - pub fn new (server_address: &str, adapter_set: &str) -> Result { - let connection_details = ConnectionDetails::new( - server_address.to_string(), - adapter_set.to_string(), - ); + /// Creates a new instance of `LightstreamerClient`. + /// + /// The constructor initializes the client with the server address and adapter set, if provided. + /// It sets up the connection details and options for the client. If no server address or + /// adapter set is specified, those properties on the client will be `None`. This allows + /// for late configuration of these details before connecting to the Lightstreamer server. + /// + /// # Arguments + /// * `server_address` - An optional reference to a string slice that represents the server + /// address to connect to. If `None`, the server address must be set later. + /// * `adapter_set` - An optional reference to a string slice that specifies the adapter set name. + /// If `None`, the adapter set must be set later. + /// + /// # Returns + /// A result containing the new `LightstreamerClient` instance if successful, or an + /// `IllegalStateException` if the initialization fails due to invalid state conditions. + /// + /// # Panics + /// Does not panic under normal circumstances. However, unexpected internal errors during + /// the creation of internal components could cause panics, which should be considered when + /// using this function in production code. + /// + /// # Example + /// ``` + /// // Example usage of `new` to create a LightstreamerClient with specified server address and + /// // adapter set. + /// let server_address = Some("http://myserver.com"); + /// let adapter_set = Some("MY_ADAPTER_SET"); + /// let ls_client = LightstreamerClient::new(server_address, adapter_set); + /// + /// assert!(ls_client.is_ok()); + /// if let Ok(client) = ls_client { + /// assert_eq!(client.server_address.unwrap(), "http://myserver.com".to_string()); + /// assert_eq!(client.adapter_set.unwrap(), "MY_ADAPTER_SET".to_string()); + /// } + /// ``` + pub fn new( + server_address: Option<&str>, + adapter_set: Option<&str>, + ) -> Result { + let connection_details = ConnectionDetails::new(server_address, adapter_set); let connection_options = ConnectionOptions::new(); - + Ok(LightstreamerClient { server_address: server_address.map(|s| s.to_string()), adapter_set: adapter_set.map(|s| s.to_string()), -- cgit v1.2.3