spetz commented on code in PR #1803:
URL: https://github.com/apache/iggy/pull/1803#discussion_r2104271140


##########
core/common/src/types/configuration/auth_config/connection_string.rs:
##########
@@ -112,89 +100,51 @@ impl ConnectionString {
             options: connection_string_options,
         })
     }
+}
 
-    fn parse_options(options: &str) -> Result<ConnectionStringOptions, 
IggyError> {
-        let options = options.split('&').collect::<Vec<&str>>();
-        let mut tls_enabled = false;
-        let mut tls_domain = "localhost".to_string();
-        let mut tls_ca_file = None;
-        let mut reconnection_retries = "unlimited".to_owned();
-        let mut reconnection_interval = "1s".to_owned();
-        let mut reestablish_after = "5s".to_owned();
-        let mut heartbeat_interval = "5s".to_owned();
-        let mut nodelay = false;
-
-        for option in options {
-            let option_parts = option.split('=').collect::<Vec<&str>>();
-            if option_parts.len() != 2 {
-                return Err(IggyError::InvalidConnectionString);
-            }
-            match option_parts[0] {
-                "tls" => {
-                    tls_enabled = option_parts[1] == "true";
-                }
-                "tls_domain" => {
-                    tls_domain = option_parts[1].to_string();
-                }
-                "tls_ca_file" => {
-                    tls_ca_file = Some(option_parts[1].to_string());
-                }
-                "reconnection_retries" => {
-                    reconnection_retries = option_parts[1].to_string();
-                }
-                "reconnection_interval" => {
-                    reconnection_interval = option_parts[1].to_string();
-                }
-                "reestablish_after" => {
-                    reestablish_after = option_parts[1].to_string();
-                }
-                "heartbeat_interval" => {
-                    heartbeat_interval = option_parts[1].to_string();
-                }
-                "nodelay" => {
-                    nodelay = option_parts[1] == "true";
-                }
-                _ => {
-                    return Err(IggyError::InvalidConnectionString);
-                }
-            }
-        }
+impl<T: ConnectionStringOptions + Default> FromStr for ConnectionString<T> {
+    type Err = IggyError;
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        ConnectionString::<T>::new(s)
+    }
+}
 
-        let reconnection = TcpClientReconnectionConfig {
-            enabled: true,
-            max_retries: match reconnection_retries.as_str() {
-                "unlimited" => None,
-                _ => Some(
-                    reconnection_retries
-                        .parse()
-                        .map_err(|_| IggyError::InvalidNumberValue)?,
-                ),
-            },
-            interval: IggyDuration::from_str(reconnection_interval.as_str())
-                .map_err(|_| IggyError::InvalidConnectionString)?,
-            reestablish_after: 
IggyDuration::from_str(reestablish_after.as_str())
-                .map_err(|_| IggyError::InvalidConnectionString)?,
-        };
+/// ConnectionStringUtils is a utility struct for connection strings.
+pub struct ConnectionStringUtils;
 
-        let heartbeat_interval = 
IggyDuration::from_str(heartbeat_interval.as_str())
-            .map_err(|_| IggyError::InvalidConnectionString)?;
+impl ConnectionStringUtils {
+    pub fn parse_protocol(connection_string: &str) -> Result<String, 
IggyError> {
+        if connection_string.is_empty() {
+            return Err(IggyError::InvalidConnectionString);
+        }
 
-        let connection_string_options = ConnectionStringOptions::new(
-            tls_enabled,
-            tls_domain,
-            tls_ca_file,
-            reconnection,
-            heartbeat_interval,
-            nodelay,
-        );
+        if connection_string.starts_with(DEFAULT_CONNECTION_STRING_PREFIX) {
+            return Ok("TCP".to_string());
+        }
 
-        Ok(connection_string_options)
-    }
-}
+        if !connection_string.starts_with(CONNECTION_STRING_PREFIX) {
+            return Err(IggyError::InvalidConnectionString);
+        }
 
-impl FromStr for ConnectionString {
-    type Err = IggyError;
-    fn from_str(s: &str) -> Result<Self, Self::Err> {
-        ConnectionString::new(s)
+        let connection_string = 
connection_string.replace(CONNECTION_STRING_PREFIX, "");
+        let protocol = 
connection_string.split("://").collect::<Vec<&str>>()[0];
+
+        match protocol {
+            "" => {
+                return Ok("TCP".to_string());

Review Comment:
   Please use the custom enum for this. You can extend it with e.g. `strum`, 
for example:
   
   ```rust
   use strum::{Display, EnumString, IntoStaticStr};
   
   #[derive(Clone, Copy, Debug, Default, Display, PartialEq, EnumString, 
IntoStaticStr)]
   #[strum(serialize_all = "snake_case")]
   pub enum TransportProtocol {
       #[default]
       #[strum(to_string = "tcp")]
       Tcp,
       #[strum(to_string = "http")]
       Http,
       #[strum(to_string = "quic")]
       Quic,
   }
   
   impl TransportProtocol {
       pub fn as_str(&self) -> &'static str {
           self.into()
       }
   }
   
   impl ConnectionStringUtils {
       pub fn parse_protocol(connection_string: &str) -> 
Result<TransportProtocol, IggyError> {
           if connection_string.is_empty() {
               return Err(IggyError::InvalidConnectionString);
           }
   
           if connection_string.starts_with(DEFAULT_CONNECTION_STRING_PREFIX) {
               return Ok(TransportProtocol::Tcp);
           }
   
           if !connection_string.starts_with(CONNECTION_STRING_PREFIX) {
               return Err(IggyError::InvalidConnectionString);
           }
   
           let connection_string = 
connection_string.replace(CONNECTION_STRING_PREFIX, "");
           
TransportProtocol::from_str(connection_string.split("://").collect::<Vec<&str>>()[0])
               .map_err(|_| IggyError::InvalidConnectionString)
       }
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to