From beb66224f1d670a37293e3672ea181e7c5f9b9dc Mon Sep 17 00:00:00 2001 From: LeChatP Date: Tue, 15 Oct 2024 09:26:31 +0200 Subject: [PATCH] refactor: Refactor parse_conf_command function and improve code readability --- rar-common/src/util.rs | 89 +++++++++++++++++++---------------------- src/chsr/cli/process.rs | 60 +++++++++++++-------------- 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/rar-common/src/util.rs b/rar-common/src/util.rs index 15b264d..485505f 100644 --- a/rar-common/src/util.rs +++ b/rar-common/src/util.rs @@ -190,47 +190,48 @@ fn remove_outer_quotes(input: &str) -> String { pub fn parse_conf_command(command: &SCommand) -> Result, Box> { match command { - SCommand::Simple(command) => Ok(shell_words::split(command)?), - SCommand::Complex(command) => { - if let Some(array) = command.as_array() { - let mut result = Vec::new(); - if !array.iter().all(|item| { - // if it is a string - item.is_string() && { - //add to result - result.push(item.as_str().unwrap().to_string()); - true // continue - } - }) { - // if any of the items is not a string - return Err("Invalid command".into()); - } - Ok(result) - } else { - // call PluginManager - #[cfg(feature = "finder")] - { - let res = PluginManager::notify_complex_command_parser(command); - debug!("Parsed command {:?}", res); - res - } - #[cfg(not(feature = "finder"))] - { - Err("Invalid command".into()) - } - } - } + SCommand::Simple(command) => parse_simple_command(command), + SCommand::Complex(command) => parse_complex_command(command), } } -pub fn find_from_envpath

(exe_name: &P) -> Option -where - P: AsRef, -{ +fn parse_simple_command(command: &str) -> Result, Box> { + shell_words::split(command).map_err(Into::into) +} + +fn parse_complex_command(command: &serde_json::Value) -> Result, Box> { + if let Some(array) = command.as_array() { + let result: Result, _> = array + .iter() + .map(|item| { + item.as_str() + .map(|s| s.to_string()) + .ok_or_else(|| "Invalid command".into()) + }) + .collect(); + result + } else { + parse_complex_command_with_finder(command) + } +} + +#[cfg(feature = "finder")] +fn parse_complex_command_with_finder(command: &serde_json::Value) -> Result, Box> { + let res = PluginManager::notify_complex_command_parser(command); + debug!("Parsed command {:?}", res); + res +} + +#[cfg(not(feature = "finder"))] +fn parse_complex_command_with_finder(_command: &serde_json::Value) -> Result, Box> { + Err("Invalid command".into()) +} + +pub fn find_from_envpath>(exe_name: P) -> Option { env::var_os("PATH").and_then(|paths| { env::split_paths(&paths) .filter_map(|dir| { - let full_path = dir.join(exe_name); + let full_path = dir.join(&exe_name); if full_path.is_file() { Some(full_path) } else { @@ -241,20 +242,14 @@ where }) } -pub fn final_path(path: &String) -> PathBuf { - let result; - if let Some(env_path) = find_from_envpath(&path) { - result = env_path - } else if let Ok(cannon_path) = std::fs::canonicalize(path) { - result = cannon_path; +pub fn final_path(path: &str) -> PathBuf { + if let Some(env_path) = find_from_envpath(path) { + env_path + } else if let Ok(canon_path) = std::fs::canonicalize(path) { + canon_path } else { - result = path.parse().expect("The path is not valid"); + PathBuf::from(path) } - result - .to_str() - .expect("The path is not valid") - .parse() - .expect("The path is not valid") } #[cfg(debug_assertions)] diff --git a/src/chsr/cli/process.rs b/src/chsr/cli/process.rs index d681291..395dec6 100644 --- a/src/chsr/cli/process.rs +++ b/src/chsr/cli/process.rs @@ -392,41 +392,43 @@ pub fn process_input(storage: &Storage, inputs: Inputs) -> Result Err("Unknown Input".into()), } } - pub fn perform_on_target_opt( rconfig: &Rc>, role_id: Option, task_id: Option, exec_on_opt: impl Fn(Rc>) -> Result<(), Box>, ) -> Result<(), Box> { - let config = rconfig.as_ref().borrow_mut(); - if let Some(role_id) = role_id { - if let Some(role) = config.role(&role_id) { - if let Some(task_id) = task_id { - if let Some(task) = role.as_ref().borrow().task(&task_id) { - if let Some(options) = &task.as_ref().borrow_mut().options { - exec_on_opt(options.clone()) - } else { - let options = Rc::new(RefCell::new(Opt::default())); - let ret = exec_on_opt(options.clone()); - task.as_ref().borrow_mut().options = Some(options); - ret - } - } else { - Err("Task not found".into()) - } - } else if let Some(options) = &role.as_ref().borrow_mut().options { - exec_on_opt(options.clone()) - } else { - let options = Rc::new(RefCell::new(Opt::default())); - let ret = exec_on_opt(options.clone()); - role.as_ref().borrow_mut().options = Some(options); - ret - } + let mut config = rconfig.as_ref().borrow_mut(); + + // Helper function to execute on option or create a new one + fn execute_or_create_option( + exec_on_opt: impl Fn(Rc>) -> Result<(), Box>, + options: &mut Option>>, + ) -> Result<(), Box> { + if let Some(opt) = options { + exec_on_opt(opt.clone()) } else { - Err("Role not found".into()) + let new_opt = Rc::new(RefCell::new(Opt::default())); + let result = exec_on_opt(new_opt.clone()); + *options = Some(new_opt); + result } - } else { - return exec_on_opt(config.options.as_ref().unwrap().clone()); } -} + + // If role_id is provided, find the role + if let Some(role_id) = role_id { + let role = config.role(&role_id).ok_or("Role not found")?; + let mut role_borrowed = role.as_ref().borrow_mut(); + + // If task_id is provided, find the task + if let Some(task_id) = task_id { + let task = role_borrowed.task(&task_id).ok_or("Task not found")?; + return execute_or_create_option(exec_on_opt, &mut task.as_ref().borrow_mut().options); + } + // No task_id, use role options + return execute_or_create_option(exec_on_opt, &mut role_borrowed.options); + } + + // No role_id, use global config options + execute_or_create_option(exec_on_opt, &mut config.options) +} \ No newline at end of file