Skip to content

Commit

Permalink
chore(clafrica): code review
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed Sep 15, 2023
1 parent 77ff7c4 commit 2d272b5
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 82 deletions.
96 changes: 53 additions & 43 deletions clafrica/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ impl Frontend for Console {
.chain(self.predicates.iter().enumerate())
.skip(self.current_predicate_id)
.take(page_size)
.map(|(i, (_code, remaining_code, text))| format!(
.map(|(id, (_code, remaining_code, text))| format!(
"{}{}. {} ~{}\t ",
if i == self.current_predicate_id {
if id == self.current_predicate_id {
"*"
} else {
""
},
i + 1,
id + 1,
text,
remaining_code
))
Expand Down Expand Up @@ -99,44 +99,54 @@ impl Frontend for Console {
}
}

#[test]
fn test_console() {
let mut none = None;
none.set_input("hello");
none.update_screen((64, 64));
none.update_position((64.0, 64.0));
none.set_input("input");
none.set_page_size(10);
none.add_predicate("hey", "y", "hello");
none.display();
none.clear_predicates();
none.previous_predicate();
none.next_predicate();
none.get_selected_predicate();

let mut console = Console::default();
console.set_page_size(10);
console.update_screen((0, 0));
console.update_position((0.0, 0.0));
console.set_input("he");

console.add_predicate("hell", "llo", "hello");
console.add_predicate("helip", "lip", "helicopter");
console.add_predicate("heal", "al", "health");
console.display();
console.previous_predicate();
assert_eq!(
console.get_selected_predicate(),
Some(&("heal".to_owned(), "al".to_owned(), "health".to_owned()))
);
console.next_predicate();
assert_eq!(
console.get_selected_predicate(),
Some(&("hell".to_owned(), "llo".to_owned(), "hello".to_owned()))
);

console.clear_predicates();
console.previous_predicate();
console.next_predicate();
assert!(console.get_selected_predicate().is_none());
#[cfg(test)]
mod tests {
#[test]
fn test_none() {
use crate::api::{Frontend, None};

let mut none = None;
none.set_input("hello");
none.update_screen((64, 64));
none.update_position((64.0, 64.0));
none.set_input("input");
none.set_page_size(10);
none.add_predicate("hey", "y", "hello");
none.display();
none.clear_predicates();
none.previous_predicate();
none.next_predicate();
none.get_selected_predicate();
}

#[test]
fn test_console() {
use crate::api::{Console, Frontend};

let mut console = Console::default();
console.set_page_size(10);
console.update_screen((0, 0));
console.update_position((0.0, 0.0));
console.set_input("he");

console.add_predicate("hell", "llo", "hello");
console.add_predicate("helip", "lip", "helicopter");
console.add_predicate("heal", "al", "health");
console.display();
console.previous_predicate();
assert_eq!(
console.get_selected_predicate(),
Some(&("heal".to_owned(), "al".to_owned(), "health".to_owned()))
);
console.next_predicate();
assert_eq!(
console.get_selected_predicate(),
Some(&("hell".to_owned(), "llo".to_owned(), "hello".to_owned()))
);

console.clear_predicates();
console.previous_predicate();
console.next_predicate();
assert!(console.get_selected_predicate().is_none());
}
}
53 changes: 25 additions & 28 deletions clafrica/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Config {
#[derive(Deserialize, Debug, Clone)]
pub struct CoreConfig {
pub buffer_size: Option<usize>,
pub auto_capitalize: Option<bool>,
auto_capitalize: Option<bool>,
pub page_size: Option<usize>,
pub auto_commit: Option<bool>,
}
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Config {
let auto_capitalize = config
.core
.as_ref()
.map(|c| c.auto_capitalize.unwrap_or(true))
.and_then(|c| c.auto_capitalize)
.unwrap_or(true);

// Data
Expand Down Expand Up @@ -110,8 +110,8 @@ impl Config {
let conf = Config::from_file(&filepath)?;
translators.extend(conf.translators.unwrap_or_default());
}
Data::Simple(v) => {
let filepath = config_path.join(v.clone()).to_str().unwrap().to_string();
Data::Simple(value) => {
let filepath = config_path.join(value.clone()).to_str().unwrap().to_string();
translators.insert(key.to_owned(), Data::Simple(filepath));
}
_ => Err(format!("Invalid script file `{filepath:?}`.\nCaused by:\n\t{value:?} not allowed in the translator table."))?,
Expand Down Expand Up @@ -141,8 +141,8 @@ impl Config {
});
}
Data::MoreDetailed(MoreDetailedData { values, alias }) => {
alias.iter().chain([key.to_owned()].iter()).for_each(|e| {
translation.insert(e.to_owned(), Data::Multi(values.clone()));
alias.iter().chain([key.to_owned()].iter()).for_each(|key| {
translation.insert(key.to_owned(), Data::Multi(values.clone()));
});
}
};
Expand All @@ -162,12 +162,12 @@ impl Config {
.as_ref()
.unwrap_or(&empty)
.iter()
.filter_map(|(k, v)| {
let v = match v {
.filter_map(|(key, value)| {
let value = match value {
Data::Simple(value) => Some(value),
_ => None,
};
v.map(|v| (k.to_owned(), v.to_owned()))
value.map(|value| (key.to_owned(), value.to_owned()))
})
.collect()
}
Expand Down Expand Up @@ -211,26 +211,26 @@ impl Config {
.as_ref()
.unwrap_or(&empty)
.iter()
.filter_map(|(k, v)| {
let v = match v {
Data::Simple(v) => Some(vec![v.to_owned()]),
Data::Multi(v) => Some(v.to_owned()),
.filter_map(|(key, value)| {
let value = match value {
Data::Simple(value) => Some(vec![value.to_owned()]),
Data::Multi(value) => Some(value.to_owned()),
_ => None,
};

v.map(|v| (k.to_owned(), v))
value.map(|value| (key.to_owned(), value))
})
.collect()
}
}

#[cfg(test)]
mod tests {
use crate::config::Config;
use std::path::Path;

#[test]
fn from_file() {
use crate::config::Config;
use std::path::Path;

let conf = Config::from_file(Path::new("./data/config_sample.toml")).unwrap();

assert_eq!(
Expand All @@ -247,19 +247,22 @@ mod tests {
let data = conf.extract_data();
assert_eq!(data.keys().len(), 23);

// data and core not provided
let conf = Config::from_file(Path::new("./data/blank_sample.toml")).unwrap();
let data = conf.extract_data();
assert_eq!(data.keys().len(), 0);

// parsing error
let conf = Config::from_file(Path::new("./data/invalid_file.toml"));
assert!(conf.is_err());

// config file not found
let conf = Config::from_file(Path::new("./data/not_found"));
assert!(conf.is_err());
}

// data and and core not provided
let conf = Config::from_file(Path::new("./data/blank_sample.toml")).unwrap();
let data = conf.extract_data();
assert_eq!(data.keys().len(), 0);

#[test]
fn from_invalid_file() {
// invalid data
let conf = Config::from_file(Path::new("./data/invalid_data.toml"));
assert!(conf.is_err());
Expand All @@ -271,9 +274,6 @@ mod tests {

#[test]
fn from_file_with_translators() {
use crate::config::Config;
use std::path::Path;

let conf = Config::from_file(Path::new("./data/config_sample.toml")).unwrap();
let translators = conf.extract_translators().unwrap();
assert_eq!(translators.keys().len(), 2);
Expand All @@ -294,9 +294,6 @@ mod tests {

#[test]
fn from_file_with_translation() {
use crate::config::Config;
use std::path::Path;

let conf = Config::from_file(Path::new("./data/config_sample.toml")).unwrap();
let translation = conf.extract_translation();
assert_eq!(translation.keys().len(), 4);
Expand Down
5 changes: 3 additions & 2 deletions clafrica/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn run(
config
.extract_data()
.iter()
.map(|(k, v)| [k.as_str(), v.as_str()])
.map(|(key, value)| [key.as_str(), value.as_str()])
.collect(),
);
let (buffer_size, auto_commit, page_size) = config
Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn run(
}
EventType::KeyRelease(E_Key::ControlRight) if is_special_pressed => {
rdev::simulate(&EventType::KeyRelease(E_Key::ControlLeft))
.expect("We couldn't cancel the special function");
.expect("We couldn't cancel the special function key");
is_special_pressed = false;

if let Some(predicate) = frontend.get_selected_predicate() {
Expand Down Expand Up @@ -191,6 +191,7 @@ mod tests {
fn start_sandbox() -> rstk::TkText {
let root = rstk::trace_with("wish").unwrap();
root.title("Clafrica Test Environment");

let input_field = rstk::make_text(&root);
input_field.width(50);
input_field.height(12);
Expand Down
3 changes: 1 addition & 2 deletions clafrica/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl Processor {
self.pause();
self.keyboard.key_up(Key::Backspace);

let i = out.chars().count();
(1..i).for_each(|_| self.keyboard.key_click(Key::Backspace));
(1..out.chars().count()).for_each(|_| self.keyboard.key_click(Key::Backspace));

// Clear the remaining code
while let (None, 1.., ..) = self.cursor.state() {
Expand Down
19 changes: 12 additions & 7 deletions clafrica/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ impl Translator {

self.dictionary
.iter()
.filter_map(|(k, v)| {
if k == input {
Some((k.to_owned(), "".to_owned(), v.to_owned(), self.auto_commit))
} else if input.len() > 1 && k.starts_with(input) {
.filter_map(|(key, value)| {
if key == input {
Some((
k.to_owned(),
k.chars().skip(input.len()).collect(),
v.to_owned(),
key.to_owned(),
"".to_owned(),
value.to_owned(),
self.auto_commit,
))
} else if input.len() > 1 && key.starts_with(input) {
Some((
key.to_owned(),
key.chars().skip(input.len()).collect(),
value.to_owned(),
false,
))
} else {
Expand Down

0 comments on commit 2d272b5

Please sign in to comment.