From cdf244f0f58c59b25c3e9d8ffc8e6f9dd2a8c2ea Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 29 Aug 2022 13:02:20 +0100 Subject: [PATCH] Simplify command line argument verification code --- src/cli/mod.rs | 107 +++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 61 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index e7555c5b..73b4dd70 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -28,84 +28,69 @@ We would love it if you could star the repository (https://github.com/surrealdb/ "; fn file_valid(v: &str) -> Result<(), String> { - if !v.is_empty() { - return Ok(()); + match v { + v if !v.is_empty() => Ok(()), + _ => Err(String::from( + "\ + Provide a valid path to a SQL file\ + ", + )), } - Err(String::from( - "\ - Provide a valid path to a SQL file\ - ", - )) } fn path_valid(v: &str) -> Result<(), String> { - if v == "memory" { - return Ok(()); + match v { + "memory" => Ok(()), + v if v.starts_with("file:") => Ok(()), + v if v.starts_with("rocksdb:") => Ok(()), + v if v.starts_with("tikv:") => Ok(()), + v if v.starts_with("fdb:") => Ok(()), + _ => Err(String::from( + "\ + Provide a valid database path parameter\ + ", + )), } - if v.starts_with("file:") { - return Ok(()); - } - if v.starts_with("tikv:") { - return Ok(()); - } - if v.starts_with("fdb:") { - return Ok(()); - } - Err(String::from( - "\ - Provide a valid database path parameter\ - ", - )) } fn conn_valid(v: &str) -> Result<(), String> { - if v.starts_with("https://") { - return Ok(()); + match v { + v if v.starts_with("http://") => Ok(()), + v if v.starts_with("https://") => Ok(()), + _ => Err(String::from( + "\ + Provide a valid database connection string\ + ", + )), } - if v.starts_with("http://") { - return Ok(()); - } - Err(String::from( - "\ - Provide a valid database connection string\ - ", - )) } fn from_valid(v: &str) -> Result<(), String> { - if v.starts_with("https://") { - return Ok(()); + match v { + v if v.ends_with(".db") => Ok(()), + v if v.starts_with("http://") => Ok(()), + v if v.starts_with("https://") => Ok(()), + _ => Err(String::from( + "\ + Provide a valid database connection string, \ + or specify the path to a database file\ + ", + )), } - if v.starts_with("http://") { - return Ok(()); - } - if v.ends_with(".db") { - return Ok(()); - } - Err(String::from( - "\ - Provide a valid database connection string, \ - or specify the path to a database file\ - ", - )) } fn into_valid(v: &str) -> Result<(), String> { - if v.starts_with("https://") { - return Ok(()); + match v { + v if v.ends_with(".db") => Ok(()), + v if v.starts_with("http://") => Ok(()), + v if v.starts_with("https://") => Ok(()), + _ => Err(String::from( + "\ + Provide a valid database connection string, \ + or specify the path to a database file\ + ", + )), } - if v.starts_with("http://") { - return Ok(()); - } - if v.ends_with(".db") { - return Ok(()); - } - Err(String::from( - "\ - Provide a valid database connection string, \ - or specify the path to a database file\ - ", - )) } fn key_valid(v: &str) -> Result<(), String> {