From 6b013bb91969d7e269aa5a08da729a8370a31900 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Wed, 1 May 2019 12:55:50 -0400 Subject: add ConfigStructure trait - make JoshutoKeymap just an alias for HashMap - change keymap.toml to use ascii values rather than strings that needs reparsing --- src/config/config.rs | 6 ++-- src/config/keymap.rs | 91 ++++++++++++++++++++++---------------------------- src/config/mimetype.rs | 14 ++++---- src/config/mod.rs | 4 +++ src/config/preview.rs | 19 ++++++----- src/config/theme.rs | 6 ++-- 6 files changed, 68 insertions(+), 72 deletions(-) (limited to 'src/config') diff --git a/src/config/config.rs b/src/config/config.rs index ab1e56f..d28f837 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,6 +1,6 @@ use serde_derive::Deserialize; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::sort; use crate::CONFIG_FILE; @@ -93,8 +93,8 @@ pub struct JoshutoConfig { pub column_ratio: (usize, usize, usize), } -impl JoshutoConfig { - pub fn get_config() -> JoshutoConfig { +impl ConfigStructure for JoshutoConfig { + fn get_config() -> Self { parse_config_file::(CONFIG_FILE) .unwrap_or_else(JoshutoConfig::default) } diff --git a/src/config/keymap.rs b/src/config/keymap.rs index e38bd4c..1dc84c0 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -3,7 +3,7 @@ use std::collections::{hash_map::Entry, HashMap}; use std::process::exit; use crate::commands::{self, CommandKeybind, JoshutoCommand}; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::KEYMAP_FILE; pub const BACKSPACE: i32 = 0x7F; @@ -15,7 +15,7 @@ pub const ESCAPE: i32 = 0x1B; #[derive(Debug, Deserialize)] struct JoshutoMapCommand { - pub keys: Vec, + pub keys: Vec, pub command: String, pub args: Option>, } @@ -27,76 +27,63 @@ struct JoshutoRawKeymap { impl Flattenable for JoshutoRawKeymap { fn flatten(self) -> JoshutoKeymap { - let mut keymaps: HashMap = HashMap::new(); - if let Some(maps) = self.mapcommand { - for mapcommand in maps { - match commands::from_args(mapcommand.command.as_str(), mapcommand.args.as_ref()) { - Ok(command) => insert_keycommand(&mut keymaps, command, &mapcommand.keys[..]), - Err(e) => eprintln!("{}", e), - } + match self.mapcommand { + None => JoshutoKeymap::new(), + Some(maps) => { + let mut keymaps = JoshutoKeymap::new(); + maps.iter().for_each(|m| { + match commands::from_args(m.command.as_str(), m.args.as_ref()) { + Ok(command) => insert_keycommand(&mut keymaps, command, &m.keys[..]), + Err(e) => eprintln!("{}", e), + } + }); + keymaps } } - JoshutoKeymap { keymaps } } } -#[derive(Debug)] -pub struct JoshutoKeymap { - pub keymaps: HashMap, -} +pub type JoshutoKeymap = HashMap; -impl JoshutoKeymap { - pub fn get_config() -> JoshutoKeymap { +impl ConfigStructure for JoshutoKeymap { + fn get_config() -> Self { parse_config_file::(KEYMAP_FILE) .unwrap_or_else(JoshutoKeymap::default) } } -impl std::default::Default for JoshutoKeymap { - fn default() -> Self { - let keymaps = HashMap::new(); - JoshutoKeymap { keymaps } - } -} - fn insert_keycommand( - map: &mut HashMap, + map: &mut JoshutoKeymap, keycommand: Box, - keys: &[String], + keys: &[i32], ) { match keys.len() { 0 => {} - 1 => match key_to_i32(&keys[0]) { - Some(s) => match map.entry(s) { - Entry::Occupied(_) => { + 1 => match map.entry(keys[0]) { + Entry::Occupied(_) => { + eprintln!("Error: Keybindings ambiguous"); + exit(1); + } + Entry::Vacant(entry) => { + entry.insert(CommandKeybind::SimpleKeybind(keycommand)); + } + }, + _ => match map.entry(keys[0]) { + Entry::Occupied(mut entry) => match entry.get_mut() { + CommandKeybind::CompositeKeybind(ref mut m) => { + insert_keycommand(m, keycommand, &keys[1..]) + } + _ => { eprintln!("Error: Keybindings ambiguous"); exit(1); } - Entry::Vacant(entry) => { - entry.insert(CommandKeybind::SimpleKeybind(keycommand)); - } - }, - None => eprintln!("Error: Failed to parse keycode: {}", keys[0]), - }, - _ => match key_to_i32(&keys[0]) { - Some(s) => match map.entry(s) { - Entry::Occupied(mut entry) => match entry.get_mut() { - CommandKeybind::CompositeKeybind(ref mut m) => { - insert_keycommand(m, keycommand, &keys[1..]) - } - _ => { - eprintln!("Error: Keybindings ambiguous"); - exit(1); - } - }, - Entry::Vacant(entry) => { - let mut new_map = HashMap::new(); - insert_keycommand(&mut new_map, keycommand, &keys[1..]); - let composite_command = CommandKeybind::CompositeKeybind(new_map); - entry.insert(composite_command); - } }, - None => eprintln!("Error: Failed to parse keycode: {}", keys[0]), + Entry::Vacant(entry) => { + let mut new_map = HashMap::new(); + insert_keycommand(&mut new_map, keycommand, &keys[1..]); + let composite_command = CommandKeybind::CompositeKeybind(new_map); + entry.insert(composite_command); + } }, } } diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index 15560db..40d64dc 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -2,7 +2,7 @@ use serde_derive::Deserialize; use std::collections::HashMap; use std::fmt; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::MIMETYPE_FILE; #[derive(Debug, Deserialize)] @@ -68,11 +68,6 @@ pub struct JoshutoMimetype { } impl JoshutoMimetype { - pub fn get_config() -> JoshutoMimetype { - parse_config_file::(MIMETYPE_FILE) - .unwrap_or_else(JoshutoMimetype::default) - } - pub fn get_entries_for_ext(&self, extension: &str) -> Vec<&JoshutoMimetypeEntry> { let mut vec = Vec::new(); if let Some(entry_ids) = self.extension.get(extension) { @@ -97,6 +92,13 @@ impl JoshutoMimetype { } } +impl ConfigStructure for JoshutoMimetype { + fn get_config() -> Self { + parse_config_file::(MIMETYPE_FILE) + .unwrap_or_else(JoshutoMimetype::default) + } +} + impl std::default::Default for JoshutoMimetype { fn default() -> Self { JoshutoMimetype { diff --git a/src/config/mod.rs b/src/config/mod.rs index ba6017f..255878e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -16,6 +16,10 @@ use std::path::{Path, PathBuf}; use crate::CONFIG_HIERARCHY; +pub trait ConfigStructure { + fn get_config() -> Self; +} + // implemented by config file implementations to turn a RawConfig into a Config trait Flattenable { fn flatten(self) -> T; diff --git a/src/config/preview.rs b/src/config/preview.rs index 24175b8..dd6665d 100644 --- a/src/config/preview.rs +++ b/src/config/preview.rs @@ -1,7 +1,7 @@ use serde_derive::Deserialize; use std::collections::HashMap; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; use crate::PREVIEW_FILE; #[derive(Debug, Deserialize)] @@ -43,16 +43,19 @@ pub struct JoshutoPreview { pub mimetype: HashMap, } -impl JoshutoPreview { - pub fn new() -> Self { +impl ConfigStructure for JoshutoPreview { + fn get_config() -> Self { + parse_config_file::(PREVIEW_FILE) + .unwrap_or_else(JoshutoPreview::default) + } +} + +impl std::default::Default for JoshutoPreview { + fn default() -> Self { JoshutoPreview { extension: HashMap::new(), mimetype: HashMap::new(), } } - - pub fn get_config() -> JoshutoPreview { - parse_config_file::(PREVIEW_FILE) - .unwrap_or_else(JoshutoPreview::new) - } } + diff --git a/src/config/theme.rs b/src/config/theme.rs index 77cae37..217f6c1 100644 --- a/src/config/theme.rs +++ b/src/config/theme.rs @@ -1,7 +1,7 @@ use serde_derive::Deserialize; use std::collections::HashMap; -use crate::config::{parse_config_file, Flattenable}; +use super::{parse_config_file, ConfigStructure, Flattenable}; #[derive(Debug, Deserialize, Clone)] pub struct JoshutoColorPair { @@ -164,8 +164,8 @@ pub struct JoshutoTheme { pub ext: HashMap, } -impl JoshutoTheme { - pub fn get_config() -> JoshutoTheme { +impl ConfigStructure for JoshutoTheme { + fn get_config() -> Self { parse_config_file::(crate::THEME_FILE) .unwrap_or_else(JoshutoTheme::default) } -- cgit v1.2.3