diff options
Diffstat (limited to 'crates/cli')
| -rw-r--r-- | crates/cli/src/exercises.rs | 17 | ||||
| -rw-r--r-- | crates/cli/src/inflection.rs | 13 | ||||
| -rw-r--r-- | crates/cli/src/init.rs | 2 | ||||
| -rw-r--r-- | crates/cli/src/nuke.rs | 2 | ||||
| -rw-r--r-- | crates/cli/src/run.rs | 33 | ||||
| -rw-r--r-- | crates/cli/src/tags.rs | 2 | ||||
| -rw-r--r-- | crates/cli/src/words.rs | 71 |
7 files changed, 71 insertions, 69 deletions
diff --git a/crates/cli/src/exercises.rs b/crates/cli/src/exercises.rs index 39af6c3..ef89a0c 100644 --- a/crates/cli/src/exercises.rs +++ b/crates/cli/src/exercises.rs @@ -1,5 +1,8 @@ use inquire::{Confirm, Editor, Select, Text}; -use mihi::{Exercise, ExerciseKind}; +use mihi::exercise::{ + create_exercise, delete_exercise, find_exercise_by_title, select_by_title, update_exercise, + Exercise, ExerciseKind, +}; use std::vec::IntoIter; // Show the help message. @@ -107,7 +110,7 @@ fn create(args: IntoIter<String>) -> i32 { }; let title = exercise.title.clone(); - match mihi::create_exercise(exercise) { + match create_exercise(exercise) { Ok(_) => { println!("Exercise '{title}' has been successfully created!"); 0 @@ -120,7 +123,7 @@ fn create(args: IntoIter<String>) -> i32 { } fn select_single_exercise(search: Option<String>) -> Result<Exercise, String> { - let exercises = mihi::select_by_title(search)?; + let exercises = select_by_title(search)?; let title = match exercises.len() { 0 => return Err("not found".to_string()), @@ -134,7 +137,7 @@ fn select_single_exercise(search: Option<String>) -> Result<Exercise, String> { }, }; - mihi::find_exercise_by_title(title.as_str()) + find_exercise_by_title(title.as_str()) } fn edit(mut args: IntoIter<String>) -> i32 { @@ -160,7 +163,7 @@ fn edit(mut args: IntoIter<String>) -> i32 { }; let title = exercise.title.clone(); - match mihi::update_exercise(exercise) { + match update_exercise(exercise) { Ok(_) => { println!("Exercise '{title}' has been successfully updated!"); 0 @@ -178,7 +181,7 @@ fn ls(mut args: IntoIter<String>) -> i32 { return 1; } - let exercises = mihi::select_by_title(args.next()).unwrap_or(vec![]); + let exercises = select_by_title(args.next()).unwrap_or(vec![]); for exe in exercises { println!("- '{}'", exe); } @@ -208,7 +211,7 @@ fn rm(mut args: IntoIter<String>) -> i32 { .prompt(); match ans { - Ok(true) => match mihi::delete_exercise(selection) { + Ok(true) => match delete_exercise(selection) { Ok(_) => println!("Removed '{selection}' from the database!"), Err(e) => { println!("error: words: {e}"); diff --git a/crates/cli/src/inflection.rs b/crates/cli/src/inflection.rs index 8799e9b..d3a23dd 100644 --- a/crates/cli/src/inflection.rs +++ b/crates/cli/src/inflection.rs @@ -1,6 +1,6 @@ -use mihi::{ - configuration, get_adjective_table, get_inflected_from, get_noun_table, Category, Word, -}; +use mihi::cfg::configuration; +use mihi::inflection::{get_adjective_table, get_inflected_from, get_noun_table}; +use mihi::word::{Category, Word}; fn print_noun_inflection(word: &Word) -> Result<(), String> { let table = get_noun_table(word)?; @@ -119,14 +119,15 @@ pub fn print_full_inflection_for(word: Word) -> Result<(), String> { #[cfg(test)] mod tests { use super::*; - use mihi::DeclensionTable; + use mihi::inflection::DeclensionTable; + use mihi::word::{find_by, select_enunciated}; fn get_word(enunciated: &str) -> Word { - let words = mihi::select_enunciated(Some(enunciated.to_string()), &[]).unwrap(); + let words = select_enunciated(Some(enunciated.to_string()), &[]).unwrap(); assert_eq!(words.len(), 1); - mihi::find_by(words.first().unwrap().as_str()).unwrap() + find_by(words.first().unwrap().as_str()).unwrap() } fn stringify_with(word: &Word, table: &DeclensionTable) -> String { diff --git a/crates/cli/src/init.rs b/crates/cli/src/init.rs index 50808cc..7636a58 100644 --- a/crates/cli/src/init.rs +++ b/crates/cli/src/init.rs @@ -50,5 +50,5 @@ pub fn run(args: Vec<String>) { } fn init(language: String) -> Result<(), String> { - mihi::add_language(language) + mihi::cfg::add_language(language) } diff --git a/crates/cli/src/nuke.rs b/crates/cli/src/nuke.rs index 6e6624c..f249f46 100644 --- a/crates/cli/src/nuke.rs +++ b/crates/cli/src/nuke.rs @@ -20,7 +20,7 @@ pub fn run(args: Vec<String>) { } } - match mihi::get_config_path() { + match mihi::cfg::get_config_path() { Ok(path) => match std::fs::remove_dir_all(path) { Ok(_) => {} Err(e) => { diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 97d687b..86136aa 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -1,9 +1,13 @@ extern crate rand; use inquire::{Confirm, Editor, Text}; -use mihi::{configuration, get_adjective_table, get_inflected_from, select_related_words}; -use mihi::{ - get_noun_table, select_relevant_words, touch_exercise, update_success, Category, - DeclensionTable, Exercise, ExerciseKind, RelationKind, Word, +use mihi::cfg::configuration; +use mihi::exercise::{select_relevant_exercises, touch_exercise, Exercise, ExerciseKind}; +use mihi::inflection::{get_adjective_table, get_inflected_from, get_noun_table, DeclensionTable}; +use mihi::tag::{select_tag_names, update_success}; +use mihi::word::{ + adverb, comparative, is_valid_word_flag, joint_related_words, select_related_words, + select_relevant_words, select_words_except, superlative, Category, RelationKind, Word, + BOOLEAN_FLAGS, }; use rand::prelude::*; use std::env; @@ -218,7 +222,7 @@ fn ask_for_alternatives(related: &[Vec<Word>; 5]) -> bool { else { return false; }; - let expected = mihi::joint_related_words(alternatives); + let expected = joint_related_words(alternatives); if !same_answer(&raw, &expected) { return false; } @@ -230,7 +234,7 @@ fn ask_for_alternatives(related: &[Vec<Word>; 5]) -> bool { else { return false; }; - let expected = mihi::joint_related_words(gendered); + let expected = joint_related_words(gendered); if !same_answer(&raw, &expected) { return false; } @@ -246,7 +250,7 @@ fn ask_for_alternatives(related: &[Vec<Word>; 5]) -> bool { fn ask_for_others(word: &Word, related: &[Vec<Word>; 5]) -> bool { assert!(matches!(word.category, Category::Adjective)); - let comparative = mihi::comparative(word, &related[RelationKind::Comparative as usize - 1]); + let comparative = comparative(word, &related[RelationKind::Comparative as usize - 1]); let Ok(raw) = Text::new("Comparative:").prompt() else { return false; }; @@ -254,7 +258,7 @@ fn ask_for_others(word: &Word, related: &[Vec<Word>; 5]) -> bool { return false; } - let superlative = mihi::superlative(word, &related[RelationKind::Superlative as usize - 1]); + let superlative = superlative(word, &related[RelationKind::Superlative as usize - 1]); let Ok(raw) = Text::new("Superlative:").prompt() else { return false; }; @@ -262,7 +266,7 @@ fn ask_for_others(word: &Word, related: &[Vec<Word>; 5]) -> bool { return false; } - let adverbial = mihi::adverb(word, &related[RelationKind::Adverb as usize - 1]); + let adverbial = adverb(word, &related[RelationKind::Adverb as usize - 1]); let Ok(raw) = Text::new("Adverb:").prompt() else { return false; }; @@ -593,7 +597,7 @@ pub fn run(args: Vec<String>) { } "-f" | "--flag" => match it.next() { Some(flag) => { - if mihi::is_valid_word_flag(flag.as_str()) { + if is_valid_word_flag(flag.as_str()) { if flags.iter().any(|s| s.as_str() == flag) { println!( "warning: practice: flag '{flag}' was provided multiple times" @@ -602,7 +606,7 @@ pub fn run(args: Vec<String>) { flags.push(flag); } } else { - let supported = mihi::BOOLEAN_FLAGS.join(", "); + let supported = BOOLEAN_FLAGS.join(", "); help(Some( format!( "error: practice: unknown flag value '{flag}'. You have to pick between: {supported}" @@ -642,7 +646,7 @@ pub fn run(args: Vec<String>) { "-t" | "--tag" => match it.next() { Some(t) => { let name = t.trim().to_string(); - if let Ok(results) = mihi::select_tag_names(&Some(name.clone())) { + if let Ok(results) = select_tag_names(&Some(name.clone())) { if results.is_empty() { println!("warning: practice: the tag '{}' does not exist.", name); } else { @@ -693,8 +697,7 @@ pub fn run(args: Vec<String>) { Category::Pronoun, ], }; - if let Ok(words_to_inflect) = mihi::select_words_except(&list, &cats, &flags, &tags) - { + if let Ok(words_to_inflect) = select_words_except(&list, &cats, &flags, &tags) { if !run_inflect_words(&words_to_inflect, &locale) { break; } @@ -704,7 +707,7 @@ pub fn run(args: Vec<String>) { if !inflection_only { if let Ok(exercises) = - mihi::select_relevant_exercises(kind, if exercises_only { 5 } else { 1 }) + select_relevant_exercises(kind, if exercises_only { 5 } else { 1 }) { if !run_exercises(exercises) { break; diff --git a/crates/cli/src/tags.rs b/crates/cli/src/tags.rs index 82a14ce..bb5e907 100644 --- a/crates/cli/src/tags.rs +++ b/crates/cli/src/tags.rs @@ -1,5 +1,5 @@ use inquire::{Confirm, Select}; -use mihi::{create_tag, delete_tag, select_tag_names}; +use mihi::tag::{create_tag, delete_tag, select_tag_names}; use std::vec::IntoIter; // Show the help message. diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs index cd0f1a0..3febce8 100644 --- a/crates/cli/src/words.rs +++ b/crates/cli/src/words.rs @@ -2,10 +2,9 @@ use crate::inflection::print_full_inflection_for; use crate::locale::current_locale; use inquire::{Confirm, Editor, MultiSelect, Select, Text}; -use mihi::{ - adverb, comparative, joint_related_words, select_related_words, select_tags_for, superlative, - Category, Conjugation, Declension, Gender, Language, RelationKind, Word, -}; +use mihi::cfg::Language; +use mihi::tag::{attach_tag_to_word, dettach_tags_from_word, select_tag_names, select_tags_for}; +use mihi::word::*; use std::vec::IntoIter; static NEW_MESSAGE: &str = "New word"; @@ -508,10 +507,10 @@ fn do_create(enunciated: String) -> Result<(), String> { return Err("abort!".to_string()); }; - match mihi::create_word(word) { + match create_word(word) { Ok(word_id) => { for tag in selected_tags { - if let Err(e) = mihi::attach_tag_to_word(tag.id as i64, word_id) { + if let Err(e) = attach_tag_to_word(tag.id as i64, word_id) { println!("warning: words: {e}"); } } @@ -541,7 +540,7 @@ fn create(args: IntoIter<String>) -> i32 { // Now we try to fetch whether the word already existed, by doing a // general search on the database. - let mut words = match mihi::select_enunciated(Some(enunciated.clone()), &[]) { + let mut words = match select_enunciated(Some(enunciated.clone()), &[]) { Ok(words) => words, Err(e) => { println!("error: words: {e}"); @@ -586,7 +585,7 @@ fn ls(mut args: IntoIter<String>, tags: &[String]) -> i32 { return 1; } - let words = match mihi::select_enunciated(args.next(), tags) { + let words = match select_enunciated(args.next(), tags) { Ok(words) => words, Err(e) => { println!("error: words: {e}"); @@ -605,7 +604,7 @@ fn ls(mut args: IntoIter<String>, tags: &[String]) -> i32 { // multiple words match the same search parameter, then the user is asked to // select one from a list of candidates. fn select_single_word(search: Option<String>) -> Result<String, String> { - let words = mihi::select_enunciated(search, &[])?; + let words = select_enunciated(search, &[])?; match words.len() { 0 => Err("not found".to_string()), @@ -638,7 +637,7 @@ fn dup(mut args: IntoIter<String>) -> i32 { }; // Fetch the word object for it which will serve as the initial values. - let word = match mihi::find_by(enunciated.as_str()) { + let word = match find_by(enunciated.as_str()) { Ok(word) => word, Err(e) => { println!("error: words: {e}"); @@ -662,14 +661,14 @@ fn dup(mut args: IntoIter<String>) -> i32 { } // Select the tags for the current word. - let tags = match mihi::select_tags_for(Some(word.id)) { + let tags = match select_tags_for(Some(word.id)) { Ok(tags) => tags, Err(e) => { println!("error: words: {e}"); return 1; } }; - let all_tags = match mihi::select_tags_for(None) { + let all_tags = match select_tags_for(None) { Ok(tags) => tags, Err(e) => { println!("error: words: {e}"); @@ -707,26 +706,22 @@ fn dup(mut args: IntoIter<String>) -> i32 { }; // Create the word. If successful, then we move into relationships and tags. - match mihi::create_word(updated) { + match create_word(updated) { Ok(word_id) => { // Set it as an alternative. This goes both ways, so two // relationships have to be inserted with both directions. - if let Err(e) = - mihi::add_word_relationship(source_id, word_id, RelationKind::Alternative) - { + if let Err(e) = add_word_relationship(source_id, word_id, RelationKind::Alternative) { println!("errors: words: {e}."); return 1; } - if let Err(e) = - mihi::add_word_relationship(word_id, source_id, RelationKind::Alternative) - { + if let Err(e) = add_word_relationship(word_id, source_id, RelationKind::Alternative) { println!("errors: words: {e}."); return 1; } // Attach tags. for tag in selected_tags { - if let Err(e) = mihi::attach_tag_to_word(tag.id as i64, word_id) { + if let Err(e) = attach_tag_to_word(tag.id as i64, word_id) { println!("warning: words: {e}."); } } @@ -758,7 +753,7 @@ fn edit(mut args: IntoIter<String>) -> i32 { }; // Fetch the word object for it which will serve as the initial values. - let word = match mihi::find_by(enunciated.as_str()) { + let word = match find_by(enunciated.as_str()) { Ok(word) => word, Err(e) => { println!("error: words: {e}"); @@ -782,14 +777,14 @@ fn edit(mut args: IntoIter<String>) -> i32 { } // Select the tags for the current word. - let tags = match mihi::select_tags_for(Some(word.id)) { + let tags = match select_tags_for(Some(word.id)) { Ok(tags) => tags, Err(e) => { println!("error: words: {e}"); return 1; } }; - let all_tags = match mihi::select_tags_for(None) { + let all_tags = match select_tags_for(None) { Ok(tags) => tags, Err(e) => { println!("error: words: {e}"); @@ -844,17 +839,17 @@ fn edit(mut args: IntoIter<String>) -> i32 { } } - match mihi::update_word(updated) { + match update_word(updated) { Ok(_) => { // Add missing tags. for tag in tags_to_add { - if let Err(e) = mihi::attach_tag_to_word(tag as i64, word_id) { + if let Err(e) = attach_tag_to_word(tag as i64, word_id) { println!("warning: words: {e}"); } } // Drop tags which are no longer needed. - if let Err(e) = mihi::dettach_tags_from_word(&tags_to_remove, word_id) { + if let Err(e) = dettach_tags_from_word(&tags_to_remove, word_id) { println!("warning: words: {e}"); } @@ -1066,7 +1061,7 @@ fn poke(mut args: IntoIter<String>) -> i32 { } }; - if mihi::update_timestamp(enunciated.as_str()).is_ok() { + if update_timestamp(enunciated.as_str()).is_ok() { 0 } else { 1 @@ -1089,7 +1084,7 @@ fn rel(args: IntoIter<String>) -> i32 { return 1; } }; - let source = match mihi::find_by(source_enunciate.as_str()) { + let source = match find_by(source_enunciate.as_str()) { Ok(word) => word, Err(e) => { println!("error: words: {e}"); @@ -1116,7 +1111,7 @@ fn rel(args: IntoIter<String>) -> i32 { return 1; } }; - let dest = match mihi::find_by(dest_enunciate.as_str()) { + let dest = match find_by(dest_enunciate.as_str()) { Ok(word) => word, Err(e) => { println!("error: words: {e}"); @@ -1124,14 +1119,14 @@ fn rel(args: IntoIter<String>) -> i32 { } }; - match mihi::add_word_relationship(source.id as i64, dest.id as i64, relation.clone()) { + match add_word_relationship(source.id as i64, dest.id as i64, relation.clone()) { Ok(_) => { // If the relation was actually an alternative word, then the // relationship goes both ways. Add the same relationship but with // the direction changed. if matches!(relation, RelationKind::Alternative | RelationKind::Gendered) { if let Err(e) = - mihi::add_word_relationship(dest.id as i64, source.id as i64, relation.clone()) + add_word_relationship(dest.id as i64, source.id as i64, relation.clone()) { println!("errors: words: {e}"); return 1; @@ -1166,7 +1161,7 @@ fn show(mut args: IntoIter<String>) -> i32 { } }; - let word = match mihi::find_by(enunciated.as_str()) { + let word = match find_by(enunciated.as_str()) { Ok(word) => word, Err(e) => { println!("error: words: {e}."); @@ -1197,7 +1192,7 @@ fn rm(mut args: IntoIter<String>) -> i32 { }; // Fetch the word object for it which will serve as the initial values. - let word = match mihi::find_by(selection.as_str()) { + let word = match find_by(selection.as_str()) { Ok(word) => word, Err(e) => { println!("error: words: {e}"); @@ -1212,7 +1207,7 @@ fn rm(mut args: IntoIter<String>) -> i32 { .prompt(); match ans { - Ok(true) => match mihi::delete_word(&word) { + Ok(true) => match delete_word(&word) { Ok(_) => println!("Removed '{selection}' from the database!"), Err(e) => { println!("error: words: {e}"); @@ -1249,7 +1244,7 @@ pub fn run(args: Vec<String>) { "-t" | "--tag" => match it.next() { Some(t) => { let name = t.trim().to_string(); - if let Ok(results) = mihi::select_tag_names(&Some(name.clone())) { + if let Ok(results) = select_tag_names(&Some(name.clone())) { if results.is_empty() { println!("warning: words: the tag '{}' does not exist.", name); } else { @@ -1319,7 +1314,7 @@ mod tests { // Returns a string with the format "{comparative form}-{superlative // form}-{adverbial form}-{alternatives}-{gendered alternatives}". fn related_for(enunciated: &str) -> String { - let word = mihi::find_by(enunciated).unwrap(); + let word = find_by(enunciated).unwrap(); let related = select_related_words(&word).unwrap(); let alternatives = &related[RelationKind::Alternative as usize - 1]; let gendered = &related[RelationKind::Gendered as usize - 1]; @@ -1338,8 +1333,8 @@ mod tests { format!( "{}-{}-{}", first, - mihi::joint_related_words(alternatives), - mihi::joint_related_words(gendered) + joint_related_words(alternatives), + joint_related_words(gendered) ) } |
