From 8250584af6daf5865e3484aa563e0cb774c8dac2 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sat, 7 Oct 2023 11:10:17 -0300 Subject: [PATCH] refactor: Run code formatter and enable clippy some clippy warns --- Cargo.toml | 1 + src/cli.rs | 61 +++++++++++++++++++++++++++-------------------------- src/main.rs | 13 ++++++------ src/web.rs | 4 ++-- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 84c8bb0..061081c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" license = "MIT" keywords = ["lyrics", "cli", "rust"] +categories = ["command-line-utilities"] readme = "README.md" authors = ["Ishan Joshi "] description = "A simple CLI to get lyrics of a song from Genius using a public availed API" diff --git a/src/cli.rs b/src/cli.rs index 9d7d207..7d27a90 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,53 +1,54 @@ use inquire::Text; -pub fn get_input(msg: &str, help: Option<&str>, default: Option<&str>)->String{ - let input = Text::new(msg) - .with_help_message( - help.unwrap_or(""), - ) - .with_default( - default.unwrap_or(""), - ) - .prompt(); - let input = input.unwrap(); - input +pub fn get_input(msg: &str, help: Option<&str>, default: Option<&str>) -> String { + Text::new(msg) + .with_help_message(help.unwrap_or("")) + .with_default(default.unwrap_or("")) + .prompt() + .expect("Failed to get input") } pub fn parse_args() -> (String, String) { let args: Vec = std::env::args().collect(); let mut input = String::new(); if args.len() > 1 { - for i in 1..args.len() { - input.push_str(&args[i]); - input.push_str(" "); + for arg in args.iter().skip(1) { + input.push_str(arg); + input.push(' '); } (input, args[1].clone()) - } - else{ - let output = get_input("Get any song's lyrics: ".into(), "Enter the song name: ".into(), "Never Gonna Give You Up".into()); + } else { + let output = get_input( + "Get any song's lyrics: ", + "Enter the song name: ".into(), + "Never Gonna Give You Up".into(), + ); (output, "lyrics".into()) } } -pub fn print_help(){ - bunt::println!("Please provide a song name, no need to put {$underline}quotes{/$} around it ;)"); +pub fn print_help() { + bunt::println!( + "Please provide a song name, no need to put {$underline}quotes{/$} around it ;)" + ); bunt::println!("{$blue}{$underline}Usage:{/$}{/$} "); bunt::println!("lyrics {$yellow}{/$}"); bunt::println!("lyrics --help / -h"); bunt::println!("lyrics --version / -v"); } -pub fn print_version(){ +pub fn print_version() { bunt::println!("lyrics {$yellow}v0.1.0{/$}"); } -pub fn splash_screen(){ -bunt::println!("{$green} _ _ {/$}"); -bunt::println!("{$red}| | (_) {/$}"); -bunt::println!("{$blue}| | _ _ _ __ _ ___ ___ {/$}"); -bunt::println!("{$yellow}| | | | | | '__| |/ __/ __|{/$}"); -bunt::println!("{$yellow}| |___| |_| | | | | (__\\__ \\{/$}"); -bunt::println!("{$blue}\\_____/\\__, |_| |_|\\___|___/{/$}"); -bunt::println!("{$red} __/ | {/$}"); -bunt::println!("{$green} |___/ {/$}Finder {$underline}v.0.1{/$}"); -} \ No newline at end of file +#[allow(clippy::cognitive_complexity)] +pub fn splash_screen() { + bunt::println!("{$green} _ _ {/$}"); + bunt::println!("{$red}| | (_) {/$}"); + bunt::println!("{$blue}| | _ _ _ __ _ ___ ___ {/$}"); + bunt::println!("{$yellow}| | | | | | '__| |/ __/ __|{/$}"); + bunt::println!("{$yellow}| |___| |_| | | | | (__\\__ \\{/$}"); + bunt::println!("{$blue}\\_____/\\__, |_| |_|\\___|___/{/$}"); + bunt::println!("{$red} __/ | {/$}"); + bunt::println!("{$green} |___/ {/$}Finder {$underline}v.0.1{/$}"); +} diff --git a/src/main.rs b/src/main.rs index cdfd2a2..b274f0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)] +#![allow(clippy::multiple_crate_versions)] + use human_panic::setup_panic; mod cli; @@ -10,16 +13,14 @@ async fn main() { let (input, cmd) = cli::parse_args(); if cmd == "-h" || cmd == "--help" { cli::print_help(); - } - else if cmd == "-v" || cmd == "--version" { + } else if cmd == "-v" || cmd == "--version" { cli::print_version(); - } - else { + } else { let (url, lyrics) = web::get_lyrics(input.as_str()).await; bunt::println!("\n--------------------------"); bunt::println!("{$yellow}{}{/$}", input); bunt::println!("--------------------------"); - println!("{}", lyrics); + println!("{lyrics}"); bunt::println!("\n{$green}Lyrics from: {/$}{}", url); } -} \ No newline at end of file +} diff --git a/src/web.rs b/src/web.rs index 59bd84b..859cc8f 100644 --- a/src/web.rs +++ b/src/web.rs @@ -7,9 +7,9 @@ pub struct Response { } pub async fn get_lyrics(query: &str) -> (String, String) { - let url = format!("https://lyrics.astrid.sh/api/search?q={}", query); + let url = format!("https://lyrics.astrid.sh/api/search?q={query}"); let resp = reqwest::get(&url).await.unwrap(); //use serde_json to parse the response let json: Response = serde_json::from_str(&resp.text().await.unwrap()).unwrap(); (json.url, json.lyrics) -} \ No newline at end of file +}