2021-06-19 18:59:19 +04:00
|
|
|
use anyhow::{Error, Result};
|
2021-06-20 23:31:45 +04:00
|
|
|
use std::collections::HashMap;
|
2021-06-17 15:08:05 +04:00
|
|
|
|
2021-06-19 18:59:19 +04:00
|
|
|
use serde::{de::Error as SerdeError, Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::keymap::{parse_keymaps, Keymaps};
|
2021-06-17 15:08:05 +04:00
|
|
|
|
2021-06-19 18:59:19 +04:00
|
|
|
#[derive(Default)]
|
2021-06-17 15:08:05 +04:00
|
|
|
pub struct Config {
|
2021-06-20 23:31:45 +04:00
|
|
|
pub theme: Option<String>,
|
|
|
|
pub lsp: LspConfig,
|
2021-06-19 18:59:19 +04:00
|
|
|
pub keymaps: Keymaps,
|
2021-06-17 15:08:05 +04:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:31:45 +04:00
|
|
|
#[derive(Default, Serialize, Deserialize)]
|
2021-06-24 00:33:28 +04:00
|
|
|
#[serde(rename_all = "kebab-case")]
|
2021-06-20 23:31:45 +04:00
|
|
|
pub struct LspConfig {
|
|
|
|
pub display_messages: bool,
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:59:19 +04:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
struct TomlConfig {
|
2021-06-20 06:17:43 +04:00
|
|
|
theme: Option<String>,
|
2021-06-20 23:31:45 +04:00
|
|
|
#[serde(default)]
|
|
|
|
lsp: LspConfig,
|
2021-06-19 18:59:19 +04:00
|
|
|
keys: Option<HashMap<String, HashMap<String, String>>>,
|
|
|
|
}
|
2021-06-17 20:52:41 +04:00
|
|
|
|
2021-06-19 18:59:19 +04:00
|
|
|
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 {
|
2021-06-20 23:31:45 +04:00
|
|
|
theme: config.theme,
|
|
|
|
lsp: config.lsp,
|
2021-06-19 18:59:19 +04:00
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
2021-06-17 15:08:05 +04:00
|
|
|
}
|