Update config module to manage a directory instead of a file

This commit is contained in:
Federico Terzi 2019-09-07 11:27:08 +02:00
parent 9c680445f0
commit 5276dc262d

View File

@ -3,7 +3,7 @@ extern crate dirs;
use std::path::Path; use std::path::Path;
use std::fs; use std::fs;
use crate::matcher::Match; use crate::matcher::Match;
use std::fs::File; use std::fs::{File, create_dir_all};
use std::io::Read; use std::io::Read;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::keyboard::KeyModifier; use crate::keyboard::KeyModifier;
@ -11,12 +11,13 @@ use crate::keyboard::KeyModifier;
// TODO: add documentation link // TODO: add documentation link
const DEFAULT_CONFIG_FILE_CONTENT : &str = include_str!("res/config.yaml"); const DEFAULT_CONFIG_FILE_CONTENT : &str = include_str!("res/config.yaml");
// Default values for primitives const DEFAULT_CONFIG_FILE_NAME : &str = "default.yaml";
// Default values for primitives
fn default_toggle_interval() -> u32 { fn default_toggle_interval() -> u32 {
230 230
} }
fn default_backspace_limit() -> i32 { fn default_backspace_limit() -> i32 {
3 3
} }
@ -50,7 +51,7 @@ impl Default for BackendType {
} }
impl Configs { impl Configs {
pub fn load(path: &Path) -> Configs { fn load_config(path: &Path) -> Configs {
let file_res = File::open(path); let file_res = File::open(path);
if let Ok(mut file) = file_res { if let Ok(mut file) = file_res {
let mut contents = String::new(); let mut contents = String::new();
@ -64,11 +65,48 @@ impl Configs {
panic!("Config file not found...") panic!("Config file not found...")
} }
} }
}
pub fn load_default() -> Configs { pub struct ConfigSet {
default: Configs,
specific: Vec<Configs>,
}
impl ConfigSet {
pub fn load(dir_path: &Path) -> ConfigSet {
if !dir_path.is_dir() {
panic!("Invalid config directory");
}
let default_file = espanso_dir.join(DEFAULT_CONFIG_FILE_NAME);
let default = Configs::load_config(default_file);
let mut specific = Vec::new();
for entry in fs::read_dir(dir_path)? {
let entry = entry?;
let path = entry.path();
let config = Configs::load_config(path.as_path());
specific.push(config);
}
ConfigSet {
default,
specific
}
}
pub fn load_default() -> ConfigSet {
let res = dirs::home_dir(); let res = dirs::home_dir();
if let Some(home_dir) = res { if let Some(home_dir) = res {
let default_file = home_dir.join(".espanso"); let espanso_dir = home_dir.join(".espanso");
// Create the espanso dir if id doesn't exist
let res = create_dir_all(espanso_dir);
if let Ok(_) = res {
let default_file = espanso_dir.join(DEFAULT_CONFIG_FILE_NAME);
// If config file does not exist, create one from template // If config file does not exist, create one from template
if !default_file.exists() { if !default_file.exists() {
@ -76,9 +114,10 @@ impl Configs {
.expect("Unable to write default config file"); .expect("Unable to write default config file");
} }
Configs::load(default_file.as_path()) return ConfigSet::load(espanso_dir.as_path())
}else{ }
}
panic!("Could not generate default position for config file"); panic!("Could not generate default position for config file");
} }
}
} }