From 0cd20107808e37175b07dd1f673fb4ea09b9cb7a Mon Sep 17 00:00:00 2001 From: Patryk Laurent Date: Thu, 12 Jan 2023 21:12:56 -0800 Subject: [PATCH] use closure/return to avoid deep indents --- services/shellchat/src/cmds/pddb_cmd.rs | 44 ++++++++++++------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 63319f5e2..c1f3ee401 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -158,30 +158,30 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } } "copy" => { - if let Some(srcdescriptor) = tokens.next() { - if let Some(dstdescriptor) = tokens.next() { - if let Some((_srcdict, _srckeyname)) = srcdescriptor.split_once(std::path::MAIN_SEPARATOR) { - if let Some((_dstdict, _dstkeyname)) = dstdescriptor.split_once(std::path::MAIN_SEPARATOR) { - match std::fs::copy(srcdescriptor, dstdescriptor) { - Ok(_result) => { - write!(ret, "Copy from {} to {} succeeded", srcdescriptor, dstdescriptor).ok(); - } - Err(e) => { - write!(ret, "Error copying from {} to {}: {:?}", srcdescriptor, dstdescriptor, e).ok(); - } - } - } else { - write!(ret, "Destination {} is not of required form 'dict{}key'", dstdescriptor, std::path::MAIN_SEPARATOR).unwrap(); - } - } else { - write!(ret, "Source {} is not of required form 'dict{}key'", srcdescriptor, std::path::MAIN_SEPARATOR).unwrap(); - } - } else { + (|| { + let Some(srcdescriptor) = tokens.next() else { write!(ret, "Usage is copy 'dict:key' 'dict:key' (missing destination)").unwrap(); + return; + }; + let Some(dstdescriptor) = tokens.next() else { + write!(ret, "Usage is copy 'dict:key' 'dict:key' (missing source)").unwrap(); + return; + }; + if srcdescriptor.split_once(':').is_none() { + write!(ret, "Source {} is not of required form 'dict:key'", srcdescriptor).unwrap(); + return; } - } else { - write!(ret, "Usage is copy 'dict:key' 'dict:key' (missing source)").unwrap(); - } + if dstdescriptor.split_once(':').is_none() { + write!(ret, "Destination {} is not of required form 'dict:key'", dstdescriptor).unwrap(); + return; + } + if let Err(e) = std::fs::copy(srcdescriptor, dstdescriptor) { + write!(ret, "Error copying from {} to {}: {:?}", srcdescriptor, dstdescriptor, e).unwrap(); + return; + } + + write!(ret, "Copy from {} to {} succeeded", srcdescriptor, dstdescriptor).unwrap(); + })() } "write" => { if let Some(descriptor) = tokens.next() {