diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-20 15:52:41 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-10-20 15:52:41 -0400 |
commit | 6e2164aeaaf02bf61099e773a0341a38652c5da4 (patch) | |
tree | 69c90c589119b1058097993f3a0221aba6af4100 /src/config/keymap/keymapping.rs | |
parent | d151d64809f918c59b4cd5c18ce71d05b165cd84 (diff) |
rework config structure
Diffstat (limited to 'src/config/keymap/keymapping.rs')
-rw-r--r-- | src/config/keymap/keymapping.rs | 85 |
1 files changed, 43 insertions, 42 deletions
diff --git a/src/config/keymap/keymapping.rs b/src/config/keymap/keymapping.rs index 0f42e94..3c3f68c 100644 --- a/src/config/keymap/keymapping.rs +++ b/src/config/keymap/keymapping.rs @@ -1,13 +1,14 @@ use serde_derive::Deserialize; use std::collections::{hash_map::Entry, HashMap}; +use std::convert::{AsMut, AsRef, From}; use std::str::FromStr; #[cfg(feature = "mouse")] use termion::event::MouseEvent; use termion::event::{Event, Key}; -use crate::config::{parse_to_config_file, ConfigStructure, Flattenable}; +use crate::config::{parse_to_config_file, TomlConfigFile}; use crate::error::JoshutoResult; use crate::io::IoWorkerOptions; use crate::key_command::{Command, CommandKeybind}; @@ -22,15 +23,46 @@ struct CommandKeymap { } #[derive(Debug, Deserialize)] -struct RawAppKeyMapping { +struct AppKeyMappingCrude { #[serde(default)] - mapcommand: Vec<CommandKeymap>, + pub mapcommand: Vec<CommandKeymap>, } -impl Flattenable<AppKeyMapping> for RawAppKeyMapping { - fn flatten(self) -> AppKeyMapping { - let mut keymaps = AppKeyMapping::new(); - for m in self.mapcommand { +#[derive(Debug)] +pub struct AppKeyMapping { + map: HashMap<Event, CommandKeybind>, +} + +impl AppKeyMapping { + pub fn new() -> Self { + Self { + map: HashMap::new(), + } + } + + pub fn default_res() -> JoshutoResult<Self> { + let crude: AppKeyMappingCrude = toml::from_str(DEFAULT_KEYMAP)?; + let keymapping: Self = Self::from(crude); + Ok(keymapping) + } +} + +impl AsRef<HashMap<Event, CommandKeybind>> for AppKeyMapping { + fn as_ref(&self) -> &HashMap<Event, CommandKeybind> { + &self.map + } +} + +impl AsMut<HashMap<Event, CommandKeybind>> for AppKeyMapping { + fn as_mut(&mut self) -> &mut HashMap<Event, CommandKeybind> { + &mut self.map + } +} + +impl From<AppKeyMappingCrude> for AppKeyMapping { + fn from(crude: AppKeyMappingCrude) -> Self { + let mut keymaps = Self::new(); + for m in crude.mapcommand { match Command::from_str(m.command.as_str()) { Ok(command) => { let events: Vec<Event> = m @@ -57,34 +89,10 @@ impl Flattenable<AppKeyMapping> for RawAppKeyMapping { } } -#[derive(Debug)] -pub struct AppKeyMapping { - map: HashMap<Event, CommandKeybind>, -} - -impl std::convert::AsRef<HashMap<Event, CommandKeybind>> for AppKeyMapping { - fn as_ref(&self) -> &HashMap<Event, CommandKeybind> { - &self.map - } -} - -impl std::convert::AsMut<HashMap<Event, CommandKeybind>> for AppKeyMapping { - fn as_mut(&mut self) -> &mut HashMap<Event, CommandKeybind> { - &mut self.map - } -} - -impl AppKeyMapping { - pub fn new() -> Self { - Self { - map: HashMap::new(), - } - } - - pub fn default_res() -> JoshutoResult<Self> { - let raw: RawAppKeyMapping = toml::from_str(DEFAULT_KEYMAP)?; - let keymapping: Self = raw.flatten(); - Ok(keymapping) +impl TomlConfigFile for AppKeyMapping { + fn get_config(file_name: &str) -> Self { + parse_to_config_file::<AppKeyMappingCrude, AppKeyMapping>(file_name) + .unwrap_or_else(Self::default) } } @@ -94,13 +102,6 @@ impl std::default::Default for AppKeyMapping { } } -impl ConfigStructure for AppKeyMapping { - fn get_config(file_name: &str) -> Self { - parse_to_config_file::<RawAppKeyMapping, AppKeyMapping>(file_name) - .unwrap_or_else(Self::default) - } -} - fn insert_keycommand( keymap: &mut AppKeyMapping, keycommand: Command, |