From 4f712db7cb348f343b09bd4efbd085f1213a9eb0 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 7 Sep 2019 13:55:31 +0200 Subject: [PATCH] Add check to load auxiliary config files --- src/config.rs | 25 ++++++++++++++++++++++++- src/main.rs | 2 -- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index a0046f7..0aaf161 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,7 @@ use std::fs::{File, create_dir_all}; use std::io::Read; use serde::{Serialize, Deserialize}; use crate::keyboard::KeyModifier; +use std::collections::HashSet; // TODO: add documentation link const DEFAULT_CONFIG_FILE_CONTENT : &str = include_str!("res/config.yaml"); @@ -15,6 +16,7 @@ const DEFAULT_CONFIG_FILE_NAME : &str = "default.yaml"; // Default values for primitives +fn default_name() -> String{ "default".to_owned() } fn default_toggle_interval() -> u32 { 230 } @@ -24,6 +26,9 @@ fn default_backspace_limit() -> i32 { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Configs { + #[serde(default = "default_name")] + pub name: String, + #[serde(default)] pub toggle_key: KeyModifier, @@ -73,7 +78,7 @@ pub struct ConfigSet { specific: Vec, } -impl ConfigSet { +impl ConfigSet { // TODO: tests pub fn load(dir_path: &Path) -> ConfigSet { if !dir_path.is_dir() { panic!("Invalid config directory"); @@ -84,6 +89,9 @@ impl ConfigSet { let mut specific = Vec::new(); + // Used to make sure no duplicates are present + let mut name_set = HashSet::new(); + for entry in fs::read_dir(dir_path) .expect("Cannot read espanso config directory!") { @@ -91,7 +99,22 @@ impl ConfigSet { if let Ok(entry) = entry { let path = entry.path(); + // Skip the default one, already loaded + if path.file_name().unwrap_or("".as_ref()) == "default.yaml" { + continue; + } + let config = Configs::load_config(path.as_path()); + + if config.name == "default" { + panic!(format!("Error while parsing {} : please a name", path.to_str().unwrap())) + } + + if name_set.contains(&config.name) { + panic!(format!("Error while parsing {} : the specified name is already used, please specify another one", path.to_str().unwrap())) + } + + name_set.insert(config.name.clone()); specific.push(config); } } diff --git a/src/main.rs b/src/main.rs index 380153c..4c0128e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,8 +60,6 @@ fn espanso_main(config_set: ConfigSet) { ui_manager.notify("Hello guys"); let system_manager = system::get_manager(); - println!("{}", system_manager.get_current_window_title().unwrap()); - println!("{}", system_manager.get_current_window_class().unwrap()); let clipboard_manager = clipboard::get_manager(); let clipboard_manager_arc = Arc::new(clipboard_manager);