diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-14 16:29:36 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-14 22:39:36 +0100 |
| commit | 9bc834bcb6e694a0ce065fc8c638c699b62688c5 (patch) | |
| tree | cdf6148862c5b0bf17d483d88f4fb6ff7ffd0bc0 /crates/cli | |
| parent | 79b6db7603b727950fb5a208ab15303d31599cf3 (diff) | |
| download | mihi-9bc834bcb6e694a0ce065fc8c638c699b62688c5.tar.gz mihi-9bc834bcb6e694a0ce065fc8c638c699b62688c5.zip | |
run: do not account errors in the exit code
It's confusing with the error case, and it also gets in the way for
endless mode.
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'crates/cli')
| -rw-r--r-- | crates/cli/src/run.rs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 8af93ee..744fe6f 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -30,9 +30,7 @@ fn help(msg: Option<&str>) { // Run the quiz for all the given `words` while expecting answers to be // delivered in the given `locale`. -fn run_words(words: Vec<Word>, locale: &Locale) -> i32 { - let mut errors = 0; - +fn run_words(words: Vec<Word>, locale: &Locale) -> bool { for word in words { // If the translation cannot be found, skip this word. let Some(translation) = word.translation.get(locale.to_code()) else { @@ -42,7 +40,7 @@ fn run_words(words: Vec<Word>, locale: &Locale) -> i32 { println!("Word: {}", word.enunciated); let Ok(raw) = Text::new(format!("Translation ({locale}):").as_str()).prompt() else { - return 1; + return false; }; let answer = raw.trim(); @@ -61,11 +59,10 @@ fn run_words(words: Vec<Word>, locale: &Locale) -> i32 { let _ = update_success(&word, word.succeeded - 1, 0); } println!("\x1b[91m❌{tr}\x1b[0m"); - errors += 1; } } - errors + true } // Returns a vector of words which contain a randomized set of words from @@ -164,14 +161,12 @@ fn accepted_diff(given: String, expected: String) -> bool { } // Run the quiz for all the given `exercises`. -fn run_exercises(exercises: Vec<Exercise>) -> i32 { +fn run_exercises(exercises: Vec<Exercise>) -> bool { if exercises.is_empty() { println!("practice: no exercises!"); - return 0; + return true; } - let mut errors = 0; - for exercise in exercises { let Ok(solution) = Editor::new( format!("Exercise '{}' (kind: {}):", exercise.title, exercise.kind).as_str(), @@ -185,20 +180,24 @@ fn run_exercises(exercises: Vec<Exercise>) -> i32 { ) .with_file_extension(".md") .prompt() else { - return 1; + return false; }; - let solution = remove_exercise_enunciate(solution); + + let mut solution = remove_exercise_enunciate(solution); + if solution.is_empty() { + solution = String::from("<no solution given>"); + } println!( "Enunciate for '{}':\n\n{}\n\nGiven:\n", exercise.title, exercise.enunciate ); if !accepted_diff(solution, exercise.solution) { - errors += 1; + // TODO: update_{success,failure} } } - errors + true } pub fn run(args: Vec<String>) { @@ -322,17 +321,21 @@ pub fn run(args: Vec<String>) { } }; - let mut code = 0; - match words { + let code = match words { Ok(list) => { - if !exercises_only { - code += run_words(list, &locale); + if !exercises_only + && !run_words(list, &locale) { + std::process::exit(1); + } + if !run_exercises(exercises) { + std::process::exit(1); } - code += run_exercises(exercises); + + 0 } Err(e) => { println!("error: practice: {e}"); - code = 1; + 1 } }; |
