Skip to content

Commit

Permalink
Merge b318031 into fa0b686
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed Aug 20, 2023
2 parents fa0b686 + b318031 commit e87ede6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions clafrica/data/invalid.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# parsing error
1a = à
19 changes: 14 additions & 5 deletions clafrica/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ struct DetailedData {

impl Config {
pub fn from_file(filepath: &Path) -> Result<Self, Box<dyn error::Error>> {
let content = fs::read_to_string(filepath)?;
let mut config: Self = toml::from_str(&content)?;
let content = fs::read_to_string(filepath)
.map_err(|err| format!("Couldn't open file `{filepath:?}`.\nCaused by:\n\t{err}."))?;
let mut config: Self = toml::from_str(&content).map_err(|err| {
format!("Failed to parse configuration file `{filepath:?}`.\nCaused by:\n\t{err}")
})?;

let config_path = filepath.parent().unwrap();

Expand Down Expand Up @@ -115,15 +118,21 @@ mod tests {
use std::path::Path;

let conf = Config::from_file(Path::new("./data/config_sample.toml")).unwrap();
assert_eq!(conf.core.clone().unwrap().buffer_size, 12);
assert!(!conf.core.clone().unwrap().auto_capitalize);
assert_eq!(conf.core.as_ref().unwrap().buffer_size, 12);
assert!(!conf.core.as_ref().unwrap().auto_capitalize);

let data = conf.extract_data();
assert_eq!(data.keys().len(), 19);

let conf = Config::from_file(Path::new("./not_found"));
// parsing error
let conf = Config::from_file(Path::new("./data/invalid.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);
Expand Down
7 changes: 5 additions & 2 deletions clafrica/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use crate::api::Frontend;
use clafrica_lib::{text_buffer, utils};
use enigo::{Enigo, Key, KeyboardControllable};
use rdev::{self, EventType, Key as E_Key};
use std::{io, sync::mpsc, thread};
use std::{error, sync::mpsc, thread};

pub mod prelude {
pub use crate::config::Config;
}

pub fn run(config: config::Config, mut frontend: impl Frontend) -> Result<(), io::Error> {
pub fn run(
config: config::Config,
mut frontend: impl Frontend,
) -> Result<(), Box<dyn error::Error>> {
let map = utils::build_map(
config
.extract_data()
Expand Down

0 comments on commit e87ede6

Please sign in to comment.