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 ++++++++++++++++++++++++++++--------------- lib/mihi/src/lib.rs | 44 ++++++++++--------------- 6 files changed, 116 insertions(+), 112 deletions(-) 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); } } } diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs index 8325af8..54efcf9 100644 --- a/lib/mihi/src/lib.rs +++ b/lib/mihi/src/lib.rs @@ -9,7 +9,9 @@ use rusqlite::{params, Connection}; mod migrate; #[derive(Debug)] +#[derive(Default)] pub enum Category { + #[default] Unknown = 0, Noun, Adjective, @@ -39,11 +41,6 @@ impl std::fmt::Display for Category { } } -impl std::default::Default for Category { - fn default() -> Self { - Category::Unknown - } -} impl TryFrom for Category { type Error = &'static str; @@ -66,11 +63,13 @@ impl TryFrom for Category { } #[derive(Debug)] +#[derive(Default)] pub enum Gender { Masculine = 0, Feminine, MasculineOrFeminine, Neuter, + #[default] None, } @@ -101,14 +100,11 @@ impl std::fmt::Display for Gender { } } -impl std::default::Default for Gender { - fn default() -> Self { - Gender::None - } -} #[derive(Debug)] +#[derive(Default)] pub enum Language { + #[default] Unknown = 0, Latin, } @@ -134,11 +130,6 @@ impl std::fmt::Display for Language { } } -impl std::default::Default for Language { - fn default() -> Self { - Language::Unknown - } -} /// Returns the configuration path for the application, and it even creates it /// if it doesn't exist already. @@ -179,11 +170,11 @@ pub fn add_language(language: String) -> Result<(), String> { let mut file = match File::create(cfg) { Ok(f) => f, - Err(e) => return Err(format!("could not create file: {}", e)), + Err(e) => return Err(format!("could not create file: {e}")), }; match file.write_all(language.as_bytes()) { Ok(_) => Ok(()), - Err(e) => Err(format!("could not save language '{}': {}", language, e)), + Err(e) => Err(format!("could not save language '{language}': {e}")), } } @@ -194,15 +185,14 @@ pub fn init_database() -> Result<(), String> { Ok(handle) => handle, Err(e) => { return Err(format!( - "could not initialize the database: {}", - e.to_string() + "could not initialize the database: {e}" )) } }; match migrate::init(conn) { Ok(_) => Ok(()), - Err(e) => Err(format!("bad database schema file: {}", e.to_string())), + Err(e) => Err(format!("bad database schema file: {e}")), } } @@ -248,10 +238,10 @@ pub fn create_word(word: Word) -> Result<(), String> { match word.declension_id { Some(id @ 1..7) => { if !DECLENSIONS_WITH_KINDS[id - 1].contains(&word.kind.as_str()) { - return Err(format!("bad kind for declension '{}'", id)); + return Err(format!("bad kind for declension '{id}'")); } } - Some(val) => return Err(format!("the declension ID '{}' is not valid for nouns", val)), + Some(val) => return Err(format!("the declension ID '{val}' is not valid for nouns")), None => return Err(String::from("you have to provide the declension ID for this noun")), } }, @@ -259,10 +249,10 @@ pub fn create_word(word: Word) -> Result<(), String> { match word.declension_id { Some(id @ (1 | 3)) => { if !ADJECTIVE_KINDS[id - 1].contains(&word.kind.as_str()) { - return Err(format!("bad kind for declension '{}'", id)); + return Err(format!("bad kind for declension '{id}'")); } } - Some(val) => return Err(format!("the declension ID '{}' is not valid for adjectives", val)), + Some(val) => return Err(format!("the declension ID '{val}' is not valid for adjectives")), None => return Err(String::from("you have to provide the declension ID for this adjective")), } }, @@ -360,7 +350,7 @@ pub fn update_success(word: &Word, success: usize, steps: usize) -> Result<(), S params![success, steps, word.id], ) { Ok(_) => Ok(()), - Err(e) => return Err(format!("could not update '{}': {}", word.enunciated, e)), + Err(e) => Err(format!("could not update '{}': {}", word.enunciated, e)), } } @@ -372,7 +362,7 @@ pub fn delete_word(enunciated: &String) -> Result<(), String> { params![enunciated.as_str()], ) { Ok(_) => Ok(()), - Err(e) => return Err(format!("could not remove '{}': {}", enunciated, e)), + Err(e) => Err(format!("could not remove '{enunciated}': {e}")), } } @@ -381,7 +371,7 @@ fn get_connection() -> Result { match Connection::open(path) { Ok(handle) => Ok(handle), Err(_) => { - return Err( + Err( "could not fetch the database. Ensure that you have called 'init' first" .to_string(), ) -- cgit v1.2.3