From 1568bcb0945502e3ae9e17cee33166e13f420d55 Mon Sep 17 00:00:00 2001 From: Vomitblood Date: Mon, 22 Apr 2024 10:16:04 +0800 Subject: [PATCH] resolved clippy warnings --- src/constants.rs | 4 ++-- src/fetch.rs | 34 +++++++++++++++++----------------- src/list.rs | 4 ++-- src/main.rs | 2 +- src/print.rs | 36 ++++++++++++++++++------------------ src/validation.rs | 15 ++++----------- 6 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index f0a4330..85a479b 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,14 +1,14 @@ pub const TARGET_URL: &str = "https://github.com/Vomitblood/pokesprite/archive/refs/heads/master.zip"; -pub const DATA_DIRECTORY: once_cell::sync::Lazy = +pub static DATA_DIRECTORY: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { dirs::data_dir() .map(|dir| dir.join("rustmon")) .expect("Data directory not found") }); -pub const CACHE_DIRECTORY: once_cell::sync::Lazy = +pub static CACHE_DIRECTORY: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { dirs::cache_dir() .map(|dir| dir.join("rustmon")) diff --git a/src/fetch.rs b/src/fetch.rs index ae966ed..960fb12 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -96,7 +96,7 @@ fn create_working_directory() -> std::io::Result<()> { // create intermediate directories also std::fs::create_dir(&*crate::constants::CACHE_DIRECTORY)?; println!("Created working directory"); - return Ok(()); + Ok(()) } fn create_output_directory(output_directory_path: &std::path::Path) -> std::io::Result<()> { @@ -106,7 +106,7 @@ fn create_output_directory(output_directory_path: &std::path::Path) -> std::io:: ); std::fs::create_dir_all(output_directory_path)?; println!("Created output directory"); - return Ok(()); + Ok(()) } fn fetch_pokemon_json() -> Result<(), Box> { @@ -193,7 +193,7 @@ fn read_pokemon_file( // deserialize the into pokemoncollection let collection = serde_json::from_reader(reader)?; - return Ok(collection); + Ok(collection) } fn transform_pokemon_data( @@ -240,7 +240,7 @@ fn transform_pokemon_data( .cmp(&b.pokedex.parse::().unwrap_or(0)) }); - return processed_pokemons; + processed_pokemons } fn fetch_colorscripts_archive(target_url: &str) -> Result<(), Box> { @@ -333,10 +333,10 @@ fn extract_colorscripts_archive() -> zip::result::ZipResult<()> { if !file.name().ends_with('/') { if let Some(p) = outpath.parent() { if !p.exists() { - std::fs::create_dir_all(&p)?; + std::fs::create_dir_all(p)?; } } - let mut outfile = std::fs::File::create(&outpath)?; + let mut outfile = std::fs::File::create(outpath)?; std::io::copy(&mut file, &mut outfile)?; }; }; @@ -344,7 +344,7 @@ fn extract_colorscripts_archive() -> zip::result::ZipResult<()> { println!("Extracted colorscripts archive"); - return Ok(()); + Ok(()) } fn crop_all_images_in_directory() -> std::io::Result<()> { @@ -368,9 +368,9 @@ fn crop_all_images_in_directory() -> std::io::Result<()> { .join("cropped_images") .join(subdirectory); - std::fs::create_dir_all(&output_subdirectory_path)?; + std::fs::create_dir_all(output_subdirectory_path)?; - for entry in std::fs::read_dir(&input_subdirectory_path)? { + for entry in std::fs::read_dir(input_subdirectory_path)? { let entry = entry?; let path = entry.path(); @@ -381,7 +381,7 @@ fn crop_all_images_in_directory() -> std::io::Result<()> { println!("Cropped images"); - return Ok(()); + Ok(()) } fn crop_to_content( @@ -436,7 +436,7 @@ fn crop_to_content( // write the cropped image cropped_img.save(output_path)?; - return Ok(cropped_img); + Ok(cropped_img) } fn convert_images_to_ascii( @@ -458,7 +458,7 @@ fn convert_images_to_ascii( std::fs::create_dir_all(&output_subdirectory_path)?; - for entry in std::fs::read_dir(&input_subdirectory_path)? { + for entry in std::fs::read_dir(input_subdirectory_path)? { let entry = entry?; let path = entry.path(); @@ -471,7 +471,7 @@ fn convert_images_to_ascii( }; // print for fun - if verbose == true { + if verbose { println!("{}", ascii_art); }; @@ -485,7 +485,7 @@ fn convert_images_to_ascii( println!("Converted images to ASCII"); - return Ok(()); + Ok(()) } fn convert_image_to_unicode_small(img: &image::DynamicImage) -> String { @@ -520,7 +520,7 @@ fn convert_image_to_unicode_small(img: &image::DynamicImage) -> String { unicode_sprite.push('\n'); // New line for each row, plus reset might be added here too if colors extend beyond. } - return unicode_sprite; + unicode_sprite } fn convert_image_to_unicode_big(img: &image::DynamicImage) -> String { @@ -541,7 +541,7 @@ fn convert_image_to_unicode_big(img: &image::DynamicImage) -> String { unicode_sprite.push('\n'); } - return unicode_sprite; + unicode_sprite } fn get_color_escape_code(pixel: image::Rgba, background: bool) -> String { @@ -569,5 +569,5 @@ fn cleanup() -> std::io::Result<()> { println!("Cleaned up"); - return Ok(()); + Ok(()) } diff --git a/src/list.rs b/src/list.rs index 48aff51..ba20bb1 100644 --- a/src/list.rs +++ b/src/list.rs @@ -14,7 +14,7 @@ pub fn print_pokemon_list() -> Result<(), serde_json::Error> { println!("\nHint: Having trouble finding a Pokemon? Pass in --help to see tips!"); - return Ok(()); + Ok(()) } pub fn print_pokemon_forms(pokemon_name: &str) -> std::io::Result<()> { @@ -47,5 +47,5 @@ pub fn print_pokemon_forms(pokemon_name: &str) -> std::io::Result<()> { println!("\nHint: Pass in `--form` when using subcommand `print` to see the specific form of a Pokemon!"); } - return Ok(()); + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 48540d5..63221da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,7 +95,7 @@ fn argument_parser() -> clap::ArgMatches { .help("eXtract the colorscripts archive to a custom location") .short('x') .long("extract-destination") - .default_value(&*rustmon::constants::DATA_DIRECTORY.to_str().unwrap()), + .default_value(rustmon::constants::DATA_DIRECTORY.to_str().unwrap()), ) // fetch/verbose .arg( diff --git a/src/print.rs b/src/print.rs index faae857..313b466 100644 --- a/src/print.rs +++ b/src/print.rs @@ -14,10 +14,10 @@ pub fn print( spacing: u8, ) { // decide which function to call - if big == false + if !big // uber fast random && forms.len() == 1 - && hide_name == false + && !hide_name && (names.len() == 1 && pokedexes.len() == 1) && shiny_rate == 0.0 && forms[0] == "regular" @@ -67,7 +67,7 @@ fn random_lite() -> std::io::Result<()> { let path = crate::constants::DATA_DIRECTORY.join("colorscripts/small/regular/"); let mut files: Vec = Vec::new(); - for entry in std::fs::read_dir(&path)? { + for entry in std::fs::read_dir(path)? { let dir_entry = entry?; files.push(dir_entry.path()); } @@ -117,13 +117,13 @@ fn get_pokemon_data(pokedex_number: u16) -> crate::structs::Pokemon { pokemons.get(pokedex_number as usize - 1).unwrap().clone(); // return the data - return pokemon_data; + pokemon_data } fn find_pokedex_by_pokemon(pokemon_name: &str) -> Result> { // handle random if pokemon_name == "random" { - return Ok("0".to_string()); + Ok("0".to_string()) } else { // read the file let mut file = std::fs::File::open(crate::constants::DATA_DIRECTORY.join("pokemon.json"))?; @@ -142,7 +142,7 @@ fn find_pokedex_by_pokemon(pokemon_name: &str) -> Result bool { let random_number = rand::random::(); // if the random number is less than the shiny rate then return true - return random_number < shiny_rate; + random_number < shiny_rate } fn process_pokedexes_list(pokedexes: Vec) -> Vec { @@ -164,10 +164,10 @@ fn process_pokedexes_list(pokedexes: Vec) -> Vec { } } - return pokedexes_processed; + pokedexes_processed } -fn process_forms_list(pokedexes: &Vec, forms: Vec<&String>) -> Vec { +fn process_forms_list(pokedexes: &[u16], forms: Vec<&String>) -> Vec { let mut forms_processed: Vec = forms.iter().map(|s| s.to_string()).collect(); // ensure forms_processed has the same length as pokedexes @@ -188,7 +188,7 @@ fn process_forms_list(pokedexes: &Vec, forms: Vec<&String>) -> Vec } } - return forms_processed; + forms_processed } fn slug_generator(big: bool, form: String, name: String, shiny_rate: f32) -> std::path::PathBuf { @@ -221,17 +221,17 @@ fn slug_generator(big: bool, form: String, name: String, shiny_rate: f32) -> std // construct the path using PathBuf let mut path = std::path::PathBuf::new(); path.push(crate::constants::DATA_DIRECTORY.join("colorscripts")); - path.push(format!("{}", big)); + path.push(&big); path.push(shiny_directory); path.push(format!("{}{}", name, form)); - return path; + path } fn generate_slug_list( big: bool, forms: Vec, - pokedexes: &Vec, + pokedexes: &[u16], shiny_rate: f32, ) -> Vec { let mut slugs: Vec = Vec::new(); @@ -245,10 +245,10 @@ fn generate_slug_list( slugs.push(slug); } - return slugs; + slugs } -fn print_name(paths: &Vec) { +fn print_name(paths: &[std::path::PathBuf]) { let last_parts: Vec<&str> = paths .iter() .filter_map(|path| path.file_name()) @@ -259,13 +259,13 @@ fn print_name(paths: &Vec) { println!("{}", output); } -fn print_colorscripts(paths: &Vec, spacing: u8) -> std::io::Result<()> { +fn print_colorscripts(paths: &[std::path::PathBuf], spacing: u8) -> std::io::Result<()> { // open all files and create BufReaders let mut readers: Vec<_> = paths .iter() - .map(|path| std::fs::File::open(path)) + .map(std::fs::File::open) .filter_map(|result| result.ok()) - .map(|file| std::io::BufReader::new(file)) + .map(std::io::BufReader::new) .collect(); // create a string for spacing diff --git a/src/validation.rs b/src/validation.rs index 92cde4d..fe78055 100644 --- a/src/validation.rs +++ b/src/validation.rs @@ -36,13 +36,8 @@ fn validate_pokemon_json() -> Result<(), Box> { serde_json::from_reader(reader); match pokemon_data { - Ok(_) => return Ok(()), - Err(_) => { - return Err(format!( - "JSON structure is not correct. Please run the `fetch` subcommand." - ) - .into()) - } + Ok(_) => Ok(()), + Err(_) => Err("JSON structure is not correct. Please run the `fetch` subcommand.".into()), } } @@ -54,11 +49,9 @@ fn validate_colorscripts_directory() -> Result<(), String> { for subdirectory in subdirectories.iter() { let path = base_path.join(subdirectory); if !path.exists() { - return Err(format!( - "Directory does not exist. Please run the `fetch` subcommand." - )); + return Err("Directory does not exist. Please run the `fetch` subcommand.".into()); } } - return Ok(()); + Ok(()) }