]> git.seodisparate.com - mpd_info_screen/commitdiff
Refactor reconnect code
authorStephen Seo <seo.disparate@gmail.com>
Wed, 19 Mar 2025 07:58:56 +0000 (16:58 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 19 Mar 2025 07:59:20 +0000 (16:59 +0900)
The reconnection "impl" function will only be used by one other
function. To prevent future misuse, the "impl" function was moved into
a closure inside the function that uses it.

src/mpd_handler.rs

index 0704bcfce6f68c1cb7b26ad38f4642c8409571a4..7599e5498b94291643d916c057709442db4c8ca4 100644 (file)
@@ -241,32 +241,31 @@ fn read_line(
     Err((String::from("Newline not reached"), result))
 }
 
-fn restart_stream_impl(
-    state_handle: &mut RwLockWriteGuard<'_, MPDHandlerState>,
-) -> Result<(), String> {
-    let peer = state_handle
-        .stream
-        .peer_addr()
-        .map_err(|_| String::from("Failed to get TCP stream peer addr/port"))?;
-    state_handle
-        .stream
-        .shutdown(std::net::Shutdown::Both)
-        .map_err(|_| String::from("Failed to cleanup TCP stream"))?;
-    state_handle.stream = TcpStream::connect_timeout(&peer, CONNECT_TIMEOUT)
-        .map_err(|_| String::from("Failed to reconnect"))?;
-    state_handle
-        .stream
-        .set_nonblocking(true)
-        .map_err(|_| String::from("Failed to set non-blocking on restarted TCP stream"))?;
-    Ok(())
-}
-
 fn restart_stream(
     state_handle: &mut RwLockWriteGuard<'_, MPDHandlerState>,
     log_level: LogLevel,
 ) -> Result<(), String> {
     thread::sleep(PRE_RESTART_WAIT);
-    let result = restart_stream_impl(state_handle);
+
+    let fn_impl = |state_handle: &mut RwLockWriteGuard<'_, MPDHandlerState>| -> Result<(), String> {
+        let peer = state_handle
+            .stream
+            .peer_addr()
+            .map_err(|_| String::from("Failed to get TCP stream peer addr/port"))?;
+        state_handle
+            .stream
+            .shutdown(std::net::Shutdown::Both)
+            .map_err(|_| String::from("Failed to cleanup TCP stream"))?;
+        state_handle.stream = TcpStream::connect_timeout(&peer, CONNECT_TIMEOUT)
+            .map_err(|_| String::from("Failed to reconnect"))?;
+        state_handle
+            .stream
+            .set_nonblocking(true)
+            .map_err(|_| String::from("Failed to set non-blocking on restarted TCP stream"))?;
+        Ok(())
+    };
+
+    let result = fn_impl(state_handle);
     if let Err(e) = result {
         log("Failed to reconnect.", LogState::Error, LogLevel::Error);
         state_handle.stop_flag.store(true, Ordering::Release);