From 09a761999534e955426144ac2bd25377e9543a67 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 22 Jan 2023 21:21:11 -0500 Subject: [PATCH] Clean up err returns --- src/lib/globals/net.rs | 42 ++++++++++++++++---------------------- src/lib/globals/process.rs | 37 ++++++++++++++++++--------------- src/lib/globals/task.rs | 18 ++++++++-------- 3 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/lib/globals/net.rs b/src/lib/globals/net.rs index 238b435..ce23c6b 100644 --- a/src/lib/globals/net.rs +++ b/src/lib/globals/net.rs @@ -34,18 +34,16 @@ async fn net_request<'lua>(lua: &'lua Lua, config: Value<'lua>) -> Result { let url = s.to_string_lossy().to_string(); let method = "GET".to_string(); - (url, method, HashMap::new(), None) + Ok((url, method, HashMap::new(), None)) } Value::Table(tab) => { // Extract url let url = match tab.raw_get::<&str, mlua::String>("url") { - Ok(config_url) => config_url.to_string_lossy().to_string(), - Err(_) => { - return Err(Error::RuntimeError( - "Missing 'url' in request config".to_string(), - )) - } - }; + Ok(config_url) => Ok(config_url.to_string_lossy().to_string()), + Err(_) => Err(Error::RuntimeError( + "Missing 'url' in request config".to_string(), + )), + }?; // Extract method let method = match tab.raw_get::<&str, mlua::String>("method") { Ok(config_method) => config_method.to_string_lossy().trim().to_ascii_uppercase(), @@ -68,26 +66,22 @@ async fn net_request<'lua>(lua: &'lua Lua, config: Value<'lua>) -> Result Some(config_body.as_bytes().to_owned()), Err(_) => None, }; - (url, method, headers, body) + Ok((url, method, headers, body)) } - value => { - return Err(Error::RuntimeError(format!( - "Invalid request config - expected string or table, got {}", - value.type_name() - ))) - } - }; + value => Err(Error::RuntimeError(format!( + "Invalid request config - expected string or table, got {}", + value.type_name() + ))), + }?; // Convert method string into proper enum let method = method.trim().to_ascii_uppercase(); let method = match method.as_ref() { - "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => &method, - _ => { - return Err(Error::RuntimeError(format!( - "Invalid request config method '{}'", - &method - ))) - } - }; + "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => Ok(&method), + _ => Err(Error::RuntimeError(format!( + "Invalid request config method '{}'", + &method + ))), + }?; // Create and send the request let mut request = ureq::request(method, &url); for (header, value) in headers { diff --git a/src/lib/globals/process.rs b/src/lib/globals/process.rs index d691ed6..223b720 100644 --- a/src/lib/globals/process.rs +++ b/src/lib/globals/process.rs @@ -49,29 +49,34 @@ fn process_env_get<'lua>(lua: &'lua Lua, (_, key): (Value<'lua>, String)) -> Res fn process_env_set(_: &Lua, (_, key, value): (Value, String, Option)) -> Result<()> { // Make sure key is valid, otherwise set_var will panic if key.is_empty() { - return Err(Error::RuntimeError("Key must not be empty".to_string())); + Err(Error::RuntimeError("Key must not be empty".to_string())) } else if key.contains('=') { - return Err(Error::RuntimeError( + Err(Error::RuntimeError( "Key must not contain the equals character '='".to_string(), - )); + )) } else if key.contains('\0') { - return Err(Error::RuntimeError( + Err(Error::RuntimeError( "Key must not contain the NUL character".to_string(), - )); - } - match value { - Some(value) => { - // Make sure value is valid, otherwise set_var will panic - if value.contains('\0') { - return Err(Error::RuntimeError( - "Value must not contain the NUL character".to_string(), - )); + )) + } else { + match value { + Some(value) => { + // Make sure value is valid, otherwise set_var will panic + if value.contains('\0') { + Err(Error::RuntimeError( + "Value must not contain the NUL character".to_string(), + )) + } else { + env::set_var(&key, &value); + Ok(()) + } + } + None => { + env::remove_var(&key); + Ok(()) } - env::set_var(&key, &value); } - None => env::remove_var(&key), } - Ok(()) } fn process_env_iter<'lua>(lua: &'lua Lua, (_, _): (Value<'lua>, ())) -> Result> { diff --git a/src/lib/globals/task.rs b/src/lib/globals/task.rs index 2bc51b1..5ca34d9 100644 --- a/src/lib/globals/task.rs +++ b/src/lib/globals/task.rs @@ -21,16 +21,14 @@ pub async fn create(lua: &Lua) -> Result<()> { } fn get_or_create_thread_from_arg<'a>(lua: &'a Lua, arg: Value<'a>) -> Result> { - Ok(match arg { - Value::Thread(thread) => thread, - Value::Function(func) => lua.create_thread(func)?, - val => { - return Err(Error::RuntimeError(format!( - "Expected type thread or function, got {}", - val.type_name() - ))) - } - }) + match arg { + Value::Thread(thread) => Ok(thread), + Value::Function(func) => Ok(lua.create_thread(func)?), + val => Err(Error::RuntimeError(format!( + "Expected type thread or function, got {}", + val.type_name() + ))), + } } async fn resume_thread(lua: &Lua, thread: Thread<'_>, args: Vararg<'_>) -> Result<()> {