From 874506a5dee8e0e05f51cd314f114fbcd7cf26bd Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Thu, 8 May 2025 01:50:35 +0100 Subject: [PATCH] refactor: Use Result::or to simplify setup() control flow --- src/main.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10a40fe..4830ca2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,18 +42,15 @@ enum Action { } fn setup(args: SetupArgs) -> Result<(), std::io::Error> { - if let Ok(config_template) = std::fs::read_to_string(CONFIG_TEMPLATE_PATH) { - let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap(); - let path = shellexpand::tilde(BOT_CONFIG_PATH).into_owned(); - std::fs::write(&path, filled_in_config).unwrap(); - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?; - } - Ok(()) - } else { - Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Unable to find {}; did you execute the script from the same directory?", CONFIG_TEMPLATE_PATH))) + let config_template = std::fs::read_to_string(CONFIG_TEMPLATE_PATH).or(Err(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Unable to find {}; did you execute the script from the same directory?", CONFIG_TEMPLATE_PATH))))?; + let filled_in_config = formatx!(config_template, args.api_url, args.rest_url, args.username, args.botpassword, args.oauth2_token).unwrap(); + let path = shellexpand::tilde(BOT_CONFIG_PATH).into_owned(); + std::fs::write(&path, filled_in_config).unwrap(); + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?; } + Ok(()) } #[tokio::main]