From 6e297455b9e2b6d64694e00b7cc9c866c4c8634a Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sun, 23 Oct 2022 00:20:05 +0200 Subject: [PATCH] Thanks clippy! --- src/config.rs | 14 ++++++++------ src/db.rs | 25 +++++++++++-------------- src/routes.rs | 2 +- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8dd04df..2de60eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,12 +21,14 @@ pub struct Config { impl Config { pub fn new() -> Self { let config_path = Path::new("config.json"); - let config_file = File::open(config_path).expect(&format!( - "Can not open the config file at the path {}!", - config_path - .to_str() - .expect("Can not convert the config file path into a string") - )); + let config_file = File::open(config_path).unwrap_or_else(|_| { + panic!( + "Can not open the config file at the path {}!", + config_path + .to_str() + .expect("Can not convert the config file path into a string") + ) + }); let config_reader = BufReader::new(config_file); let config: Self = serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!"); diff --git a/src/db.rs b/src/db.rs index a53f501..8ef5f1c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -23,7 +23,7 @@ fn get_conn( pool: &DBPool, ) -> Result>, String> { pool.get() - .or(Err("Could not get database pool!".to_string())) + .map_err(|_| "Could not get database pool!".to_string()) } pub fn add_hook_log(pool: &DBPool, hook: &Hook) -> Result { @@ -41,7 +41,7 @@ pub fn add_hook_log(pool: &DBPool, hook: &Hook) -> Result { diesel::insert_into(hooklog::table) .values(&new_hook_log) .get_result::(conn) - .or_else(|e| Err(e.to_string())) + .map_err(|e| e.to_string()) } pub fn fill_hook_log( @@ -62,7 +62,7 @@ pub fn fill_hook_log( match diesel::update(hooklog::dsl::hooklog.find(hook_log_id)) .set(( hooklog::dsl::stdout.eq(Some( - std::str::from_utf8(&stdout).unwrap_or("Could not convert stdout to str!"), + std::str::from_utf8(stdout).unwrap_or("Could not convert stdout to str!"), )), hooklog::dsl::stderr.eq(Some( std::str::from_utf8(stderr).unwrap_or("Could not convert stderr to str!"), @@ -71,10 +71,9 @@ pub fn fill_hook_log( )) .execute(conn) { - Ok(_) => return, + Ok(_) => (), Err(e) => { - println!("Could not update hook log: {}", e.to_string()); - return; + println!("Could not update hook log: {e}"); } }; } @@ -84,16 +83,14 @@ pub fn get_hook_log(pool: &DBPool, id: i32) -> Result { let conn = &mut get_conn(pool)?; - let hl: Result; - - if id > 0 { - hl = hooklog::dsl::hooklog.find(id).first(conn); + let hl = if id > 0 { + hooklog::dsl::hooklog.find(id).first(conn) } else { - hl = hooklog::dsl::hooklog + hooklog::dsl::hooklog .order(hooklog::dsl::id.desc()) .offset((-id - 1).into()) - .first(conn); - } + .first(conn) + }; - hl.or_else(|e| Err(e.to_string())) + hl.map_err(|e| e.to_string()) } diff --git a/src/routes.rs b/src/routes.rs index c72ca40..6dbe598 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -82,7 +82,7 @@ pub fn trigger( } Err(e) => { stdout = Vec::new(); - stderr = format!("Error while running the hook command: {}", e.to_string()) + stderr = format!("Error while running the hook command: {e}") .as_bytes() .to_vec(); status_code = Some(1);