2021-06-19 16:59:19 +02:00
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use std::{collections::HashMap, str::FromStr};
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-06-19 16:59:19 +02:00
|
|
|
use serde::{de::Error as SerdeError, Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::keymap::{parse_keymaps, Keymaps};
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-06-18 05:57:36 +02:00
|
|
|
pub struct GlobalConfig {
|
|
|
|
pub lsp_progress: bool,
|
|
|
|
}
|
|
|
|
|
2021-06-18 15:41:49 +02:00
|
|
|
impl Default for GlobalConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { lsp_progress: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 16:59:19 +02:00
|
|
|
#[derive(Default)]
|
2021-06-17 13:08:05 +02:00
|
|
|
pub struct Config {
|
2021-06-18 05:57:36 +02:00
|
|
|
pub global: GlobalConfig,
|
2021-06-19 16:59:19 +02:00
|
|
|
pub keymaps: Keymaps,
|
2021-06-17 13:08:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 16:59:19 +02: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 18:52:41 +02:00
|
|
|
|
2021-06-19 16:59:19 +02: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 13:08:05 +02:00
|
|
|
}
|