From 174a7b516369aa23dd2630732feed3fdc19947e3 Mon Sep 17 00:00:00 2001 From: Brian Bowman Date: Sat, 9 Jul 2022 07:10:40 -0500 Subject: [PATCH] Fix clippy lints --- src-tauri/src/downloader.rs | 14 +++++++------- src-tauri/src/file_helpers.rs | 12 ++++++------ src-tauri/src/lang.rs | 18 ++++++++---------- src-tauri/src/main.rs | 30 +++++++++++------------------- src-tauri/src/system_helpers.rs | 7 ++----- src-tauri/src/unzip.rs | 2 -- src-tauri/src/web.rs | 4 ++-- 7 files changed, 36 insertions(+), 51 deletions(-) diff --git a/src-tauri/src/downloader.rs b/src-tauri/src/downloader.rs index 4b779af..1b567ce 100644 --- a/src-tauri/src/downloader.rs +++ b/src-tauri/src/downloader.rs @@ -54,17 +54,17 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu let chunk = match item { Ok(itm) => itm, Err(e) => { - emit_download_err(window, format!("Error while downloading file"), path); + emit_download_err(window, "Error while downloading file".to_string(), path); return Err(format!("Error while downloading file: {}", e)); } }; let vect = &chunk.to_vec()[..]; // Write bytes - match file.write_all(&vect) { + match file.write_all(vect) { Ok(x) => x, Err(e) => { - emit_download_err(window, format!("Error while writing file"), path); + emit_download_err(window, "Error while writing file".to_string(), path); return Err(format!("Error while writing file: {}", e)); } } @@ -73,7 +73,7 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu let new = min(downloaded + (chunk.len() as u64), total_size); downloaded = new; - total_downloaded = total_downloaded + chunk.len() as u64; + total_downloaded += chunk.len() as u64; let mut res_hash = std::collections::HashMap::new(); @@ -105,15 +105,15 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu window.emit("download_finished", &path).unwrap(); // We are done - return Ok(()); + Ok(()) } -pub fn emit_download_err(window: tauri::Window, msg: std::string::String, path: &str) { +pub fn emit_download_err(window: tauri::Window, msg: String, path: &str) { let mut res_hash = std::collections::HashMap::new(); res_hash.insert( "error".to_string(), - msg.to_string(), + msg, ); res_hash.insert( diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs index c47f63b..72c5bf8 100644 --- a/src-tauri/src/file_helpers.rs +++ b/src-tauri/src/file_helpers.rs @@ -5,28 +5,28 @@ pub fn rename(path: String, new_name: String) { let mut new_path = path.clone(); // Check if file/folder to replace exists - if !fs::metadata(&path).is_ok() { + if fs::metadata(&path).is_err() { return; } // Check if path uses forward or back slashes - if new_path.contains("\\") { - new_path = path.replace("\\", "/"); + if new_path.contains('\\') { + new_path = path.replace('\\', "/"); } - let path_replaced = &path.replace(&new_path.split("/").last().unwrap(), &new_name); + let path_replaced = &path.replace(&new_path.split('/').last().unwrap(), &new_name); fs::rename(path, &path_replaced).unwrap(); } #[tauri::command] pub fn dir_exists(path: &str) -> bool { - return fs::metadata(&path).is_ok(); + fs::metadata(&path).is_ok() } #[tauri::command] pub fn dir_is_empty(path: &str) -> bool { - return fs::read_dir(&path).unwrap().count() == 0; + fs::read_dir(&path).unwrap().count() == 0 } #[tauri::command] diff --git a/src-tauri/src/lang.rs b/src-tauri/src/lang.rs index cff655f..e797996 100644 --- a/src-tauri/src/lang.rs +++ b/src-tauri/src/lang.rs @@ -7,15 +7,13 @@ pub async fn get_lang(window: tauri::Window, lang: String) -> String { // Send contents of language file back let lang_path: PathBuf = [&install_location(), "lang", &format!("{}.json", lang)].iter().collect(); - let contents = match std::fs::read_to_string(&lang_path) { + match std::fs::read_to_string(&lang_path) { Ok(x) => x, Err(e) => { emit_lang_err(window, format!("Failed to read language file: {}", e)); - return "".to_string(); + "".to_string() } - }; - - return contents; + } } #[tauri::command] @@ -23,9 +21,9 @@ pub async fn get_languages() -> std::collections::HashMap { // for each lang file, set the key as the filename and the value as the lang_name contained in the file let mut languages = std::collections::HashMap::new(); - let mut lang_files = std::fs::read_dir(Path::new(&install_location()).join("lang")).unwrap(); + let lang_files = std::fs::read_dir(Path::new(&install_location()).join("lang")).unwrap(); - while let Some(entry) = lang_files.next() { + for entry in lang_files { let entry = entry.unwrap(); let path = entry.path(); let filename = path.file_name().unwrap().to_str().unwrap(); @@ -41,15 +39,15 @@ pub async fn get_languages() -> std::collections::HashMap { languages.insert(filename.to_string(), content); } - return languages; + languages } -pub fn emit_lang_err(window: tauri::Window, msg: std::string::String) { +pub fn emit_lang_err(window: tauri::Window, msg: String) { let mut res_hash = std::collections::HashMap::new(); res_hash.insert( "error".to_string(), - msg.to_string(), + msg, ); window.emit("lang_error", &res_hash).unwrap(); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e9e72da..6d7ac7c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -79,14 +79,9 @@ fn process_watcher() { // Grab the game process name let proc = WATCH_GAME_PROCESS.lock().unwrap().to_string(); - if !&proc.is_empty() { - let proc_with_name = system.processes_by_exact_name(&proc); - let mut exists = false; - - for _p in proc_with_name { - exists = true; - break; - } + if !proc.is_empty() { + let mut proc_with_name = system.processes_by_exact_name(&proc); + let exists = proc_with_name.next().is_some(); // If the game process closes, disable the proxy. if !exists { @@ -104,7 +99,7 @@ fn is_game_running() -> bool { // Grab the game process name let proc = WATCH_GAME_PROCESS.lock().unwrap().to_string(); - return !proc.is_empty(); + !proc.is_empty() } #[tauri::command] @@ -135,11 +130,8 @@ fn disconnect() { #[tauri::command] async fn req_get(url: String) -> String { - // Send a GET request to the specified URL. - let response = web::query(&url.to_string()).await; - - // Send the response body back to the client. - return response; + // Send a GET request to the specified URL and send the response body back to the client. + web::query(&url.to_string()).await } #[tauri::command] @@ -175,7 +167,7 @@ async fn get_theme_list(data_dir: String) -> Vec> { } } - return themes; + themes } #[tauri::command] @@ -199,7 +191,7 @@ async fn get_bg_file(bg_path: String, appdata: String) -> String { } // Now we check if the bg folder, which is one directory above the game_path, exists. - let bg_img_path = format!("{}\\{}", bg_path.clone().to_string(), file_name.as_str()); + let bg_img_path = format!("{}\\{}", &bg_path, &file_name); // If it doesn't, then we do not have backgrounds to grab. if !file_helpers::dir_exists(&bg_path) { @@ -215,15 +207,15 @@ async fn get_bg_file(bg_path: String, appdata: String) -> String { // The image exists, lets copy it to our local '\bg' folder. let bg_img_path_local = format!("{}\\bg\\{}", copy_loc, file_name.as_str()); - return match std::fs::copy(bg_img_path, bg_img_path_local) { + match std::fs::copy(bg_img_path, bg_img_path_local) { Ok(_) => { // Copy was successful, lets return true. - format!("{}\\{}", copy_loc, response_data.bg_file.as_str()) + format!("{}\\{}", copy_loc, response_data.bg_file) } Err(e) => { // Copy failed, lets return false println!("Failed to copy background image: {}", e); "".to_string() } - }; + } } diff --git a/src-tauri/src/system_helpers.rs b/src-tauri/src/system_helpers.rs index d74794b..e36d00e 100644 --- a/src-tauri/src/system_helpers.rs +++ b/src-tauri/src/system_helpers.rs @@ -1,7 +1,4 @@ - use std::thread; -use tauri; -use open; use duct::cmd; use crate::file_helpers; @@ -31,7 +28,7 @@ pub fn run_jar(path: String, execute_in: String, java_path: String) { }; // Open the program from the specified path. - match open::with(format!("/k cd /D \"{}\" & {}", &execute_in, &command).to_string(), "C:\\Windows\\System32\\cmd.exe") { + match open::with(format!("/k cd /D \"{}\" & {}", &execute_in, &command), "C:\\Windows\\System32\\cmd.exe") { Ok(_) => (), Err(e) => println!("Failed to open jar ({} from {}): {}", &path, &execute_in, e), }; @@ -48,7 +45,7 @@ pub fn open_in_browser(url: String) { #[tauri::command] pub fn copy_file(path: String, new_path: String) -> bool { - let filename = &path.split("/").last().unwrap(); + let filename = &path.split('/').last().unwrap(); let mut new_path_buf = std::path::PathBuf::from(&new_path); // If the new path doesn't exist, create it. diff --git a/src-tauri/src/unzip.rs b/src-tauri/src/unzip.rs index 16735d9..dcfe084 100644 --- a/src-tauri/src/unzip.rs +++ b/src-tauri/src/unzip.rs @@ -1,5 +1,3 @@ -use zip_extract; -use zip; use std::fs::File; use std::path; use std::thread; diff --git a/src-tauri/src/web.rs b/src-tauri/src/web.rs index c155f86..af5864d 100644 --- a/src-tauri/src/web.rs +++ b/src-tauri/src/web.rs @@ -4,7 +4,7 @@ pub(crate) async fn query(site: &str) -> String { let client = reqwest::Client::new(); let response = client.get(site).header(USER_AGENT, "cultivation").send().await.unwrap(); - return response.text().await.unwrap(); + response.text().await.unwrap() } #[tauri::command] @@ -14,5 +14,5 @@ pub(crate) async fn valid_url(url: String) -> bool { let response = client.get(url).header(USER_AGENT, "cultivation").send().await.unwrap(); - return response.status().as_str() == "200"; + response.status().as_str() == "200" } \ No newline at end of file