From 6ea4d1f33e158407a1a0b51f210461d698513300 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Fri, 25 Jul 2025 15:58:10 +0200 Subject: Apply suggestions from clippy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- crates/cli/src/init.rs | 2 +- crates/cli/src/main.rs | 87 ++++++++++++++++++++++--------------------------- crates/cli/src/nuke.rs | 6 ++-- crates/cli/src/run.rs | 12 +++---- crates/cli/src/words.rs | 77 ++++++++++++++++++++++++++++--------------- 5 files changed, 99 insertions(+), 85 deletions(-) (limited to 'crates/cli/src') diff --git a/crates/cli/src/init.rs b/crates/cli/src/init.rs index 7de2e8c..6fa904c 100644 --- a/crates/cli/src/init.rs +++ b/crates/cli/src/init.rs @@ -43,7 +43,7 @@ pub fn run(args: Vec) { match init(language) { Ok(_) => {} Err(e) => { - println!("error: init: {}", e); + println!("error: init: {e}"); std::process::exit(1); } } diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 78d094d..239891e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -23,59 +23,50 @@ fn help() { fn main() { let mut args = std::env::args(); - let nargs = std::env::args().count(); - let mut count = 1; + let nargs = args.len(); // Skip command name. args.next(); - // And iterate over the arguments. - while let Some(arg) = args.next() { - count += 1; - - match arg.as_str() { - "-h" | "--help" => { - if nargs > count { - println!( - "warning: arguments passed the '{}' flag will be ignored.", - arg.as_str() - ); - } - help(); - std::process::exit(0); - } - "-v" | "--version" => { - if nargs > count { - println!( - "warning: arguments passed the '{}' flag will be ignored.", - arg.as_str() - ); + match args.next() { + Some(command_flag) => { + match command_flag.as_str() { + "-h" | "--help" => { + if nargs > 2 { + println!("warning: arguments passed the 'help' flag will be ignored.\n"); + } + help(); + std::process::exit(0); + }, + "-v" | "--version" => { + if nargs > 2 { + println!("warning: arguments passed the 'version' flag will be ignored.\n"); + } + println!("mihi {VERSION}"); + std::process::exit(0); + }, + "init" => { + let rest: Vec = args.collect(); + init::run(rest); + }, + "nuke" => { + let rest: Vec = args.collect(); + nuke::run(rest); + }, + "words" => { + let rest: Vec = args.collect(); + words::run(rest); + }, + "run" => { + let rest: Vec = args.collect(); + run::run(rest); + }, + _ => { + println!("error: unknown flag or command: '{command_flag}'"); + std::process::exit(1); } - println!("mihi {}", VERSION); - std::process::exit(0); - } - "init" => { - let rest: Vec = args.collect(); - return init::run(rest); - } - "nuke" => { - let rest: Vec = args.collect(); - return nuke::run(rest); } - "words" => { - let rest: Vec = args.collect(); - return words::run(rest); - } - "run" => { - let rest: Vec = args.collect(); - return run::run(rest); - } - _ => { - println!("error: unknown flag or command: '{}'", arg.as_str()); - std::process::exit(1); - } - } + }, + None => run::run(Vec::new()) } - - run::run(Vec::new()); } diff --git a/crates/cli/src/nuke.rs b/crates/cli/src/nuke.rs index 1ea6532..6e6624c 100644 --- a/crates/cli/src/nuke.rs +++ b/crates/cli/src/nuke.rs @@ -7,7 +7,7 @@ fn help() { } pub fn run(args: Vec) { - for arg in args { + if let Some(arg) = args.into_iter().next() { match arg.as_str() { "-h" | "--help" => { help(); @@ -24,12 +24,12 @@ pub fn run(args: Vec) { Ok(path) => match std::fs::remove_dir_all(path) { Ok(_) => {} Err(e) => { - println!("error: nuke: {}", e); + println!("error: nuke: {e}"); std::process::exit(1); } }, Err(e) => { - println!("error: nuke: {}", e); + println!("error: nuke: {e}"); std::process::exit(1); } } diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 0cce6d6..31c521d 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -51,13 +51,13 @@ fn run_words(words: Vec, locale: Locale) -> i32 { println!("Word: {}", word.enunciated); - let Ok(raw) = Text::new(format!("Translation ({}):", locale).as_str()).prompt() else { + let Ok(raw) = Text::new(format!("Translation ({locale}):").as_str()).prompt() else { return 1; }; let answer = raw.trim(); let tr = translation.as_str().unwrap_or(""); - let found = !answer.is_empty() && tr.split(',').any(|tr| tr.trim().contains(&answer)); + let found = !answer.is_empty() && tr.split(',').any(|tr| tr.trim().contains(answer)); if found { if word.steps == MAX_STEPS - 1 { @@ -65,12 +65,12 @@ fn run_words(words: Vec, locale: Locale) -> i32 { } else { let _ = update_success(&word, word.succeeded, word.steps + 1); } - println!("\x1b[92m✓ {}\x1b[0m", tr); + println!("\x1b[92m✓ {tr}\x1b[0m"); } else { if word.succeeded > 0 { let _ = update_success(&word, word.succeeded - 1, 0); } - println!("\x1b[91m❌{}\x1b[0m", tr); + println!("\x1b[91m❌{tr}\x1b[0m"); errors += 1; } } @@ -122,7 +122,7 @@ pub fn run(args: Vec) { } _ => { help(Some( - format!("error: run: unknown flag or command '{}'", first).as_str(), + format!("error: run: unknown flag or command '{first}'").as_str(), )); std::process::exit(1); } @@ -144,7 +144,7 @@ pub fn run(args: Vec) { match words { Ok(list) => std::process::exit(run_words(list, locale)), Err(e) => { - println!("error: run: {}", e); + println!("error: run: {e}"); std::process::exit(1); } }; diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs index 4e6e4b6..7ba8dc5 100644 --- a/crates/cli/src/words.rs +++ b/crates/cli/src/words.rs @@ -1,4 +1,4 @@ -use inquire::{Confirm, Select, Text, Editor}; +use inquire::{Confirm, Editor, Select, Text}; use std::vec::IntoIter; use mihi::{create_word, delete_word, select_enunciated, Category, Gender, Language, Word}; @@ -230,7 +230,7 @@ fn do_create(enunciated: String) -> Result<(), String> { return Err("abort!".to_string()); }; let Ok(inflection_id) = inflection.parse::() else { - return Err(format!("bad value for inflection ID '{}'", inflection)); + return Err(format!("bad value for inflection ID '{inflection}'")); }; let Ok(kind) = Text::new("Kind:").with_initial_value(&guess.kind).prompt() else { @@ -244,7 +244,10 @@ fn do_create(enunciated: String) -> Result<(), String> { return Err("abort!".to_string()); }; - let Ok(flags) = Editor::new("Flags:").with_predefined_text(FLAGS_TEXT).prompt() else { + let Ok(flags) = Editor::new("Flags:") + .with_predefined_text(FLAGS_TEXT) + .prompt() + else { return Err("abort!".to_string()); }; let trimmed_flags = trim_flags(flags); @@ -256,20 +259,36 @@ fn do_create(enunciated: String) -> Result<(), String> { return Err("abort!".to_string()); }; - let word = Word{ + let word = Word { id: 0, enunciated: enunciated.clone(), particle, language: Language::Latin, - declension_id: if matches!(category, Category::Verb) { None } else { Some(inflection_id) }, - conjugation_id: if matches!(category, Category::Verb) { Some(inflection_id) } else { None }, + declension_id: if matches!(category, Category::Verb) { + None + } else { + Some(inflection_id) + }, + conjugation_id: if matches!(category, Category::Verb) { + Some(inflection_id) + } else { + None + }, kind, category, regular, locative, gender, suffix: None, - translation: serde_json::from_str(format!("{{\"en\":\"{}\", \"ca\":\"{}\"}}", translation_en.trim(), translation_ca.trim()).as_str()).unwrap(), + translation: serde_json::from_str( + format!( + "{{\"en\":\"{}\", \"ca\":\"{}\"}}", + translation_en.trim(), + translation_ca.trim() + ) + .as_str(), + ) + .unwrap(), flags: serde_json::from_str(&trimmed_flags).unwrap(), succeeded: 0, steps: 0, @@ -277,9 +296,9 @@ fn do_create(enunciated: String) -> Result<(), String> { match create_word(word) { Ok(_) => { - println!("Word '{}' has been successfully created!", enunciated); + println!("Word '{enunciated}' has been successfully created!"); Ok(()) - }, + } Err(e) => Err(e), } } @@ -306,7 +325,7 @@ fn create(args: IntoIter) -> i32 { let mut words = match select_enunciated(Some(enunciated.clone())) { Ok(words) => words, Err(e) => { - println!("error: words: {}", e); + println!("error: words: {e}"); return 1; } }; @@ -321,7 +340,7 @@ fn create(args: IntoIter) -> i32 { // into creating the word. 3 => { if let Err(e) = do_create(enunciated) { - println!("error: words: {}", e); + println!("error: words: {e}"); return 1; } } @@ -331,7 +350,7 @@ fn create(args: IntoIter) -> i32 { return 0; } else if choice == NEW_MESSAGE { if let Err(e) = do_create(enunciated) { - println!("error: words: {}", e); + println!("error: words: {e}"); return 1; } } @@ -351,17 +370,17 @@ fn ls(mut args: IntoIter) -> i32 { let words = match select_enunciated(args.next()) { Ok(words) => words, Err(e) => { - println!("error: words: {}", e); + println!("error: words: {e}"); return 1; } }; // TODO: not just the enunciated, but being able to edit for enunciated in words { - println!("{}", enunciated); + println!("{enunciated}"); } - return 0; + 0 } fn rm(mut args: IntoIter) -> i32 { @@ -373,7 +392,7 @@ fn rm(mut args: IntoIter) -> i32 { let words = match select_enunciated(args.next()) { Ok(words) => words, Err(e) => { - println!("error: words: {}", e); + println!("error: words: {e}"); return 1; } }; @@ -391,20 +410,16 @@ fn rm(mut args: IntoIter) -> i32 { }; let ans = Confirm::new( - format!( - "Do you really want to remove '{}' from the database?", - selection - ) - .as_str(), + format!("Do you really want to remove '{selection}' from the database?").as_str(), ) .with_default(true) .prompt(); match ans { Ok(true) => match delete_word(&selection) { - Ok(_) => println!("Removed '{}' from the database!", selection), + Ok(_) => println!("Removed '{selection}' from the database!"), Err(e) => { - println!("error: words: {}", e); + println!("error: words: {e}"); return 1; } }, @@ -414,7 +429,7 @@ fn rm(mut args: IntoIter) -> i32 { Err(_) => return 1, } - return 0; + 0 } pub fn run(args: Vec) { @@ -427,8 +442,8 @@ pub fn run(args: Vec) { let mut it = args.into_iter(); - while let Some(first) = it.next() { - match first.as_str() { + match it.next() { + Some(first) => match first.as_str() { "-h" | "--help" => { help(None); std::process::exit(0); @@ -444,10 +459,18 @@ pub fn run(args: Vec) { } _ => { help(Some( - format!("error: words: unknown flag or command '{}'", first).as_str(), + format!("error: words: unknown flag or command '{first}'").as_str(), )); std::process::exit(1); } + }, + None => { + help(Some( + "error: words: you need to provide a command" + .to_string() + .as_str(), + )); + std::process::exit(1); } } } -- cgit v1.2.3