summaryrefslogtreecommitdiff
path: root/helix-term/src/config.rs
blob: 839235f1391b014694423e6a45b19b1767bddc97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use anyhow::{Error, Result};
use std::collections::HashMap;

use serde::{de::Error as SerdeError, Deserialize, Serialize};

use crate::keymap::{parse_keymaps, Keymaps};

#[derive(Default)]
pub struct Config {
    pub theme: Option<String>,
    pub lsp: LspConfig,
    pub keymaps: Keymaps,
}

#[derive(Default, Serialize, Deserialize)]
pub struct LspConfig {
    pub display_messages: bool,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct TomlConfig {
    theme: Option<String>,
    #[serde(default)]
    lsp: LspConfig,
    keys: Option<HashMap<String, HashMap<String, String>>>,
}

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 {
            theme: config.theme,
            lsp: config.lsp,
            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),
        })
    }
}