summaryrefslogtreecommitdiffstats
path: root/src/config
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-05-01 12:55:50 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-05-01 12:58:36 -0400
commit6b013bb91969d7e269aa5a08da729a8370a31900 (patch)
tree3a1173b052e5abb7826509c380668d3e2e9c4be5 /src/config
parentef98d3d408f9a741433523ccf557b26aa7380dbc (diff)
add ConfigStructure trait
- make JoshutoKeymap just an alias for HashMap<i32, CommandKeybind> - change keymap.toml to use ascii values rather than strings that needs reparsing
Diffstat (limited to 'src/config')
-rw-r--r--src/config/config.rs6
-rw-r--r--src/config/keymap.rs91
-rw-r--r--src/config/mimetype.rs14
-rw-r--r--src/config/mod.rs4
-rw-r--r--src/config/preview.rs19
-rw-r--r--src/config/theme.rs6
6 files changed, 68 insertions, 72 deletions
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::<JoshutoRawConfig, JoshutoConfig>(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<String>,
+ pub keys: Vec<i32>,
pub command: String,
pub args: Option<Vec<String>>,
}
@@ -27,76 +27,63 @@ struct JoshutoRawKeymap {
impl Flattenable<JoshutoKeymap> for JoshutoRawKeymap {
fn flatten(self) -> JoshutoKeymap {
- let mut keymaps: HashMap<i32, CommandKeybind> = 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<i32, CommandKeybind>,
-}
+pub type JoshutoKeymap = HashMap<i32, CommandKeybind>;
-impl JoshutoKeymap {
- pub fn get_config() -> JoshutoKeymap {
+impl ConfigStructure for JoshutoKeymap {
+ fn get_config() -> Self {
parse_config_file::<JoshutoRawKeymap, JoshutoKeymap>(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<i32, CommandKeybind>,
+ map: &mut JoshutoKeymap,
keycommand: Box<JoshutoCommand>,
- 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::<JoshutoRawMimetype, JoshutoMimetype>(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::<JoshutoRawMimetype, JoshutoMimetype>(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<T> {
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<String, JoshutoPreviewEntry>,
}
-impl JoshutoPreview {
- pub fn new() -> Self {
+impl ConfigStructure for JoshutoPreview {
+ fn get_config() -> Self {
+ parse_config_file::<JoshutoRawPreview, JoshutoPreview>(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::<JoshutoRawPreview, JoshutoPreview>(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<String, JoshutoColorTheme>,
}
-impl JoshutoTheme {
- pub fn get_config() -> JoshutoTheme {
+impl ConfigStructure for JoshutoTheme {
+ fn get_config() -> Self {
parse_config_file::<JoshutoRawTheme, JoshutoTheme>(crate::THEME_FILE)
.unwrap_or_else(JoshutoTheme::default)
}