diff options
Diffstat (limited to 'helix-term/src/config.rs')
-rw-r--r-- | helix-term/src/config.rs | 84 |
1 files changed, 38 insertions, 46 deletions
diff --git a/helix-term/src/config.rs b/helix-term/src/config.rs index 9c962299..2c95fae3 100644 --- a/helix-term/src/config.rs +++ b/helix-term/src/config.rs @@ -1,63 +1,55 @@ -use serde::Deserialize; +use anyhow::{Error, Result}; +use std::{collections::HashMap, str::FromStr}; -use crate::commands::Command; -use crate::keymap::Keymaps; +use serde::{de::Error as SerdeError, Deserialize, Serialize}; + +use crate::keymap::{parse_keymaps, Keymaps}; -#[derive(Debug, PartialEq, Deserialize)] pub struct GlobalConfig { + pub theme: Option<String>, pub lsp_progress: bool, } impl Default for GlobalConfig { fn default() -> Self { - Self { lsp_progress: true } + Self { + lsp_progress: true, + theme: None, + } } } -#[derive(Debug, Default, PartialEq, Deserialize)] -#[serde(default)] +#[derive(Default)] pub struct Config { pub global: GlobalConfig, - pub keys: Keymaps, + pub keymaps: Keymaps, } -#[test] -fn parsing_keymaps_config_file() { - use helix_core::hashmap; - use helix_view::document::Mode; - use helix_view::input::{KeyCode, KeyEvent, KeyModifiers}; - - let sample_keymaps = r#" - [keys.insert] - y = "move_line_down" - S-C-a = "delete_selection" - - [keys.normal] - A-F12 = "move_next_word_end" - "#; +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +struct TomlConfig { + theme: Option<String>, + lsp_progress: Option<bool>, + keys: Option<HashMap<String, HashMap<String, String>>>, +} - assert_eq!( - toml::from_str::<Config>(sample_keymaps).unwrap(), - Config { - global: Default::default(), - keys: Keymaps(hashmap! { - Mode::Insert => hashmap! { - KeyEvent { - code: KeyCode::Char('y'), - modifiers: KeyModifiers::NONE, - } => Command::move_line_down, - KeyEvent { - code: KeyCode::Char('a'), - modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL, - } => Command::delete_selection, - }, - Mode::Normal => hashmap! { - KeyEvent { - code: KeyCode::F(12), - modifiers: KeyModifiers::ALT, - } => Command::move_next_word_end, - }, - }) - } - ); +impl<'de> Deserialize<'de> for Config { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: serde::Deserializer<'de>, + { + let config = TomlConfig::deserialize(deserializer)?; + Ok(Self { + global: GlobalConfig { + lsp_progress: config.lsp_progress.unwrap_or(true), + theme: config.theme, + }, + keymaps: config + .keys + .map(|r| parse_keymaps(&r)) + .transpose() + .map_err(|e| D::Error::custom(format!("Error deserializing keymap: {}", e)))? + .unwrap_or_else(Keymaps::default), + }) + } } |