From 563938e51ab9ec26379a920d3eab80458506c4e7 Mon Sep 17 00:00:00 2001 From: Ian Wijma Date: Tue, 21 Nov 2023 00:31:37 +1100 Subject: [PATCH] Refactor spawn_process fn to handle spawn error This commit modifies the spawn_process function to explicitly return a Result type, handling the error of the spawn() method. This change improves the readability of the error handling and makes it more idiomatic in Rust. As a secondary correction, it fixes a typo in a print statement. --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 05dc5b1..762e930 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,7 +84,7 @@ fn check_history(retry: &Retry) -> bool { return retry.history.len().lt(&usize::from(retry.max_retries)) } -fn spawn_process(retry: &Retry) -> std::io::Result { +fn spawn_process(retry: &Retry) -> Result { let mut command_parts: Vec = retry .command .split_whitespace() @@ -95,7 +95,10 @@ fn spawn_process(retry: &Retry) -> std::io::Result { command_parts.remove(0); command.args(command_parts); - return command.spawn(); + match command.spawn() { + Ok(child) => Ok(child), + Err(err) => Err(err), + } } fn run_command(retry: &mut Retry) { @@ -120,7 +123,7 @@ fn run_command(retry: &mut Retry) { println!("Restarting..."); restart(retry); } else { - print!("The process has crashed more then {} times in the past {}, stop restarting\n", retry.max_retries, retry.restart_name); + println!("The process has crashed more than {} times in the past {}, stop restarting\n", retry.max_retries, retry.restart_name); } } }