aboutsummaryrefslogtreecommitdiff
path: root/alacritty/src/config/mouse.rs
diff options
context:
space:
mode:
authorChristian Duerr <contact@christianduerr.com>2020-12-21 02:44:38 +0000
committerGitHub <noreply@github.com>2020-12-21 02:44:38 +0000
commit6e1b9d8b2502f5b47dc28eb5e0853e46ad8b4e84 (patch)
tree623a6cd8785529b28cc28af201c26b56fb47ac46 /alacritty/src/config/mouse.rs
parent37a3198d8882463c9873011c1d18c325ea46d7c8 (diff)
downloadr-alacritty-6e1b9d8b2502f5b47dc28eb5e0853e46ad8b4e84.tar.gz
r-alacritty-6e1b9d8b2502f5b47dc28eb5e0853e46ad8b4e84.tar.bz2
r-alacritty-6e1b9d8b2502f5b47dc28eb5e0853e46ad8b4e84.zip
Replace serde's derive with custom proc macro
This replaces the existing `Deserialize` derive from serde with a `ConfigDeserialize` derive. The goal of this new proc macro is to allow a more error-friendly deserialization for the Alacritty configuration file without having to manage a lot of boilerplate code inside the configuration modules. The first part of the derive macro is for struct deserialization. This takes structs which have `Default` implemented and will only replace fields which can be successfully deserialized. Otherwise the `log` crate is used for printing errors. Since this deserialization takes the default value from the struct instead of the value, it removes the necessity for creating new types just to implement `Default` on them for deserialization. Additionally, the struct deserialization also checks for `Option` values and makes sure that explicitly specifying `none` as text literal is allowed for all options. The other part of the derive macro is responsible for deserializing enums. While only enums with Unit variants are supported, it will automatically implement a deserializer for these enums which accepts any form of capitalization. Since this custom derive prevents us from using serde's attributes on fields, some of the attributes have been reimplemented for `ConfigDeserialize`. These include `#[config(flatten)]`, `#[config(skip)]` and `#[config(alias = "alias)]`. The flatten attribute is currently limited to at most one per struct. Additionally the `#[config(deprecated = "optional message")]` attribute allows easily defining uniform deprecation messages for fields on structs.
Diffstat (limited to 'alacritty/src/config/mouse.rs')
-rw-r--r--alacritty/src/config/mouse.rs71
1 files changed, 10 insertions, 61 deletions
diff --git a/alacritty/src/config/mouse.rs b/alacritty/src/config/mouse.rs
index 1a5aec3d..f562ebe0 100644
--- a/alacritty/src/config/mouse.rs
+++ b/alacritty/src/config/mouse.rs
@@ -1,35 +1,26 @@
use std::time::Duration;
use glutin::event::ModifiersState;
-use log::error;
-use serde::{Deserialize, Deserializer};
-use alacritty_terminal::config::{failure_default, Program, LOG_TARGET_CONFIG};
+use alacritty_config_derive::ConfigDeserialize;
+use alacritty_terminal::config::Program;
use crate::config::bindings::ModsWrapper;
-#[serde(default)]
-#[derive(Default, Clone, Debug, Deserialize, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Default, Clone, Debug, PartialEq, Eq)]
pub struct Mouse {
- #[serde(deserialize_with = "failure_default")]
pub double_click: ClickHandler,
- #[serde(deserialize_with = "failure_default")]
pub triple_click: ClickHandler,
- #[serde(deserialize_with = "failure_default")]
pub hide_when_typing: bool,
- #[serde(deserialize_with = "failure_default")]
pub url: Url,
}
-#[serde(default)]
-#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Clone, Debug, PartialEq, Eq)]
pub struct Url {
/// Program for opening links.
- #[serde(deserialize_with = "deserialize_launcher")]
pub launcher: Option<Program>,
/// Modifier used to open links.
- #[serde(deserialize_with = "failure_default")]
modifiers: ModsWrapper,
}
@@ -39,34 +30,6 @@ impl Url {
}
}
-fn deserialize_launcher<'a, D>(deserializer: D) -> std::result::Result<Option<Program>, D::Error>
-where
- D: Deserializer<'a>,
-{
- let default = Url::default().launcher;
-
- // Deserialize to generic value.
- let val = serde_yaml::Value::deserialize(deserializer)?;
-
- // Accept `None` to disable the launcher.
- if val.as_str().filter(|v| v.to_lowercase() == "none").is_some() {
- return Ok(None);
- }
-
- match <Option<Program>>::deserialize(val) {
- Ok(launcher) => Ok(launcher),
- Err(err) => {
- error!(
- target: LOG_TARGET_CONFIG,
- "Problem with config: {}; using {}",
- err,
- default.clone().unwrap().program()
- );
- Ok(default)
- },
- }
-}
-
impl Default for Url {
fn default() -> Url {
Url {
@@ -81,33 +44,19 @@ impl Default for Url {
}
}
-#[serde(default)]
-#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Clone, Debug, PartialEq, Eq)]
pub struct ClickHandler {
- #[serde(deserialize_with = "deserialize_duration_ms")]
- pub threshold: Duration,
+ threshold: u16,
}
impl Default for ClickHandler {
fn default() -> Self {
- ClickHandler { threshold: default_threshold_ms() }
+ Self { threshold: 300 }
}
}
-fn default_threshold_ms() -> Duration {
- Duration::from_millis(300)
-}
-
-fn deserialize_duration_ms<'a, D>(deserializer: D) -> ::std::result::Result<Duration, D::Error>
-where
- D: Deserializer<'a>,
-{
- let value = serde_yaml::Value::deserialize(deserializer)?;
- match u64::deserialize(value) {
- Ok(threshold_ms) => Ok(Duration::from_millis(threshold_ms)),
- Err(err) => {
- error!(target: LOG_TARGET_CONFIG, "Problem with config: {}; using default value", err);
- Ok(default_threshold_ms())
- },
+impl ClickHandler {
+ pub fn threshold(&self) -> Duration {
+ Duration::from_millis(self.threshold as u64)
}
}