Skip to content

Commit

Permalink
refactor: extract some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
radlinskii committed Oct 17, 2023
1 parent 0dfd672 commit c9f5f61
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 188 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"0.2","language":"en","flagWords":[],"words":["crossterm","ratatui","donkeytype","tempfile","mockall","automock","foobarbaaz","foobarbazqux","withf","rngs","Szewnia","chrono","datetime","Datelike"]}
{"version":"0.2","flagWords":[],"words":["crossterm","ratatui","donkeytype","tempfile","mockall","automock","foobarbaaz","foobarbazqux","withf","rngs","Szewnia","chrono","datetime","Datelike","Timelike"],"language":"en"}
18 changes: 14 additions & 4 deletions src/expected_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!
//! Dictionary file should be a text file in format of single words per line.

use anyhow::Context;
use anyhow::{Context, Result};
use mockall::automock;
use rand::{seq::SliceRandom, thread_rng, Rng};
use std::io::Read;
Expand Down Expand Up @@ -50,7 +50,8 @@ impl ExpectedInput {
}

if config.uppercase == true {
create_uppercase_words(&mut string_vec, words_start_pos, config.uppercase_ratio);
create_uppercase_words(&mut string_vec, words_start_pos, config.uppercase_ratio)
.context("Unable to create uppercase words")?;
str_vec = string_vec.iter().map(|s| s.as_str()).collect();
}

Expand Down Expand Up @@ -91,16 +92,25 @@ fn replace_words_with_numbers(
return change_to_num_threshold - 1;
}

fn create_uppercase_words(string_vec: &mut Vec<String>, pos: usize, uppercase_ratio: f64) {
fn create_uppercase_words(
string_vec: &mut Vec<String>,
pos: usize,
uppercase_ratio: f64,
) -> Result<()> {
let num_uppercase_words = (uppercase_ratio * string_vec[pos..].len() as f64).round() as usize;
for i in pos..pos + num_uppercase_words {
if string_vec[i] != "" {
let mut v: Vec<char> = string_vec[i].chars().collect();
v[0] = v[0].to_uppercase().nth(0).unwrap();
v[0] = v[0]
.to_uppercase()
.nth(0)
.context("Unable to get first character of a word")?;
let s: String = v.into_iter().collect();
string_vec[i] = s;
}
}

Ok(())
}

/// extracted to trait to create mock with `mockall` crate
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn main() -> anyhow::Result<()> {
match res {
Ok(test_results) => {
if test_results.completed {
if let Err(err) = test_results.render_chart(&mut terminal) {
if let Err(err) = test_results.render_results(&mut terminal) {
eprintln!("{:?}", err);

restore_terminal(terminal).context("Unable to restore terminal")?;
Expand Down
Loading

0 comments on commit c9f5f61

Please sign in to comment.