2021-06-19 18:59:19 +04:00
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use std::{collections::HashMap, str::FromStr};
|
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-18 07:57:36 +04:00
|
|
|
pub struct GlobalConfig {
|
|
|
|
pub lsp_progress: bool,
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:41:49 +04:00
|
|
|
impl Default for GlobalConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { lsp_progress: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 18:59:19 +04:00
|
|
|
#[derive(Default)]
|
2021-06-17 15:08:05 +04:00
|
|
|
pub struct Config {
|
2021-06-18 07:57:36 +04:00
|
|
|
pub global: GlobalConfig,
|
2021-06-19 18:59:19 +04:00
|
|
|
pub keymaps: Keymaps,
|
2021-06-17 15:08:05 +04:00
|
|
|
}
|
|
|
|
|
2021-06-19 18:59:19 +04:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
struct TomlConfig {
|
|
|
|
lsp_progress: Option<bool>,
|
|
|
|
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 {
|
|
|
|
global: GlobalConfig {
|
|
|
|
lsp_progress: config.lsp_progress.unwrap_or(true),
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|