Simplify command line argument verification code

This commit is contained in:
Tobie Morgan Hitchcock 2022-08-29 13:02:20 +01:00
parent 65d91b122e
commit cdf244f0f5

View file

@ -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(());
}
Err(String::from(
match v {
v if !v.is_empty() => Ok(()),
_ => Err(String::from(
"\
Provide a valid path to a SQL file\
",
))
)),
}
}
fn path_valid(v: &str) -> Result<(), String> {
if v == "memory" {
return Ok(());
}
if v.starts_with("file:") {
return Ok(());
}
if v.starts_with("tikv:") {
return Ok(());
}
if v.starts_with("fdb:") {
return Ok(());
}
Err(String::from(
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\
",
))
)),
}
}
fn conn_valid(v: &str) -> Result<(), String> {
if v.starts_with("https://") {
return Ok(());
}
if v.starts_with("http://") {
return Ok(());
}
Err(String::from(
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\
",
))
)),
}
}
fn from_valid(v: &str) -> Result<(), String> {
if v.starts_with("https://") {
return Ok(());
}
if v.starts_with("http://") {
return Ok(());
}
if v.ends_with(".db") {
return Ok(());
}
Err(String::from(
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\
",
))
)),
}
}
fn into_valid(v: &str) -> Result<(), String> {
if v.starts_with("https://") {
return Ok(());
}
if v.starts_with("http://") {
return Ok(());
}
if v.ends_with(".db") {
return Ok(());
}
Err(String::from(
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\
",
))
)),
}
}
fn key_valid(v: &str) -> Result<(), String> {