2021-06-17 15:08:05 +04:00
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use std::{collections::HashMap, str::FromStr};
|
|
|
|
|
|
|
|
use serde::{de::Error as SerdeError, Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::keymap::{parse_keymaps, Keymaps};
|
|
|
|
|
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-17 15:08:05 +04:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Config {
|
2021-06-18 07:57:36 +04:00
|
|
|
pub global: GlobalConfig,
|
2021-06-17 15:08:05 +04:00
|
|
|
pub keymaps: Keymaps,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2021-06-18 08:09:42 +04:00
|
|
|
#[serde(rename_all = "kebab-case")]
|
2021-06-17 15:08:05 +04:00
|
|
|
struct TomlConfig {
|
2021-06-18 07:57:36 +04:00
|
|
|
lsp_progress: Option<bool>,
|
2021-06-17 15:08:05 +04:00
|
|
|
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 {
|
2021-06-18 07:57:36 +04:00
|
|
|
global: GlobalConfig {
|
|
|
|
lsp_progress: config.lsp_progress.unwrap_or(true),
|
|
|
|
},
|
2021-06-17 15:08:05 +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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|