helix-mirror/helix-term/src/config.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

use anyhow::{Error, Result};
2021-06-20 23:31:45 +04:00
use std::collections::HashMap;
use serde::{de::Error as SerdeError, Deserialize, Serialize};
use crate::keymap::{parse_keymaps, Keymaps};
#[derive(Default)]
pub struct Config {
2021-06-20 23:31:45 +04:00
pub theme: Option<String>,
pub lsp: LspConfig,
pub keymaps: Keymaps,
}
2021-06-20 23:31:45 +04:00
#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
2021-06-20 23:31:45 +04:00
pub struct LspConfig {
pub display_messages: bool,
}
#[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,
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-20 23:31:45 +04:00
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),
})
}
}