diff options
author | Christian Duerr <contact@christianduerr.com> | 2020-12-21 02:44:38 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-21 02:44:38 +0000 |
commit | 6e1b9d8b2502f5b47dc28eb5e0853e46ad8b4e84 (patch) | |
tree | 623a6cd8785529b28cc28af201c26b56fb47ac46 /alacritty_terminal/src/config/bell.rs | |
parent | 37a3198d8882463c9873011c1d18c325ea46d7c8 (diff) | |
download | r-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_terminal/src/config/bell.rs')
-rw-r--r-- | alacritty_terminal/src/config/bell.rs | 72 |
1 files changed, 11 insertions, 61 deletions
diff --git a/alacritty_terminal/src/config/bell.rs b/alacritty_terminal/src/config/bell.rs index 97010f31..825a7b1f 100644 --- a/alacritty_terminal/src/config/bell.rs +++ b/alacritty_terminal/src/config/bell.rs @@ -1,95 +1,45 @@ use std::time::Duration; -use log::error; -use serde::{Deserialize, Deserializer}; -use serde_yaml::Value; +use alacritty_config_derive::ConfigDeserialize; -use crate::config::{failure_default, Program, LOG_TARGET_CONFIG}; +use crate::config::Program; use crate::term::color::Rgb; -const DEFAULT_BELL_COLOR: Rgb = Rgb { r: 255, g: 255, b: 255 }; - -#[serde(default)] -#[derive(Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(ConfigDeserialize, Clone, Debug, PartialEq, Eq)] pub struct BellConfig { /// Visual bell animation function. - #[serde(deserialize_with = "failure_default")] pub animation: BellAnimation, - /// Visual bell duration in milliseconds. - #[serde(deserialize_with = "failure_default")] - duration: u16, + /// Command to run on bell. + pub command: Option<Program>, /// Visual bell flash color. - #[serde(deserialize_with = "deserialize_bell_color")] pub color: Rgb, - /// Command to run on bell. - #[serde(deserialize_with = "deserialize_bell_command")] - pub command: Option<Program>, + /// Visual bell duration in milliseconds. + duration: u16, } impl Default for BellConfig { fn default() -> Self { Self { + color: Rgb { r: 255, g: 255, b: 255 }, animation: Default::default(), - duration: Default::default(), command: Default::default(), - color: DEFAULT_BELL_COLOR, + duration: Default::default(), } } } impl BellConfig { - /// Visual bell duration in milliseconds. - #[inline] pub fn duration(&self) -> Duration { - Duration::from_millis(u64::from(self.duration)) - } -} - -fn deserialize_bell_color<'a, D>(deserializer: D) -> Result<Rgb, D::Error> -where - D: Deserializer<'a>, -{ - let value = Value::deserialize(deserializer)?; - match Rgb::deserialize(value) { - Ok(value) => Ok(value), - Err(err) => { - error!( - target: LOG_TARGET_CONFIG, - "Problem with config: {}, using default color value {}", err, DEFAULT_BELL_COLOR - ); - - Ok(DEFAULT_BELL_COLOR) - }, - } -} - -fn deserialize_bell_command<'a, D>(deserializer: D) -> Result<Option<Program>, D::Error> -where - D: Deserializer<'a>, -{ - // Deserialize to generic value. - let val = Value::deserialize(deserializer)?; - - // Accept `None` to disable the bell command. - if val.as_str().filter(|v| v.to_lowercase() == "none").is_some() { - return Ok(None); - } - - match Program::deserialize(val) { - Ok(command) => Ok(Some(command)), - Err(err) => { - error!(target: LOG_TARGET_CONFIG, "Problem with config: {}; ignoring field", err); - Ok(None) - }, + Duration::from_millis(self.duration as u64) } } /// `VisualBellAnimations` are modeled after a subset of CSS transitions and Robert /// Penner's Easing Functions. -#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)] +#[derive(ConfigDeserialize, Clone, Copy, Debug, PartialEq, Eq)] pub enum BellAnimation { // CSS animation. Ease, |