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/scrolling.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/scrolling.rs')
-rw-r--r-- | alacritty_terminal/src/config/scrolling.rs | 78 |
1 files changed, 23 insertions, 55 deletions
diff --git a/alacritty_terminal/src/config/scrolling.rs b/alacritty_terminal/src/config/scrolling.rs index 136e9389..159b0f44 100644 --- a/alacritty_terminal/src/config/scrolling.rs +++ b/alacritty_terminal/src/config/scrolling.rs @@ -1,24 +1,23 @@ -use log::error; +use serde::de::Error as SerdeError; use serde::{Deserialize, Deserializer}; -use crate::config::{failure_default, LOG_TARGET_CONFIG, MAX_SCROLLBACK_LINES}; +use alacritty_config_derive::ConfigDeserialize; + +/// Maximum scrollback amount configurable. +const MAX_SCROLLBACK_LINES: u32 = 100_000; /// Struct for scrolling related settings. -#[serde(default)] -#[derive(Deserialize, Copy, Clone, Default, Debug, PartialEq, Eq)] +#[derive(ConfigDeserialize, Copy, Clone, Debug, PartialEq, Eq)] pub struct Scrolling { - #[serde(deserialize_with = "failure_default")] - history: ScrollingHistory, - #[serde(deserialize_with = "failure_default")] - multiplier: ScrollingMultiplier, + pub multiplier: u8, - // TODO: REMOVED - #[serde(deserialize_with = "failure_default")] - pub auto_scroll: Option<bool>, + history: ScrollingHistory, +} - // TODO: DEPRECATED - #[serde(deserialize_with = "failure_default")] - faux_multiplier: Option<ScrollingMultiplier>, +impl Default for Scrolling { + fn default() -> Self { + Self { multiplier: 3, history: Default::default() } + } } impl Scrolling { @@ -26,29 +25,12 @@ impl Scrolling { self.history.0 } - pub fn multiplier(self) -> u8 { - self.multiplier.0 - } - - pub fn faux_multiplier(self) -> Option<u8> { - self.faux_multiplier.map(|sm| sm.0) - } - // Update the history size, used in ref tests. pub fn set_history(&mut self, history: u32) { self.history = ScrollingHistory(history); } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)] -struct ScrollingMultiplier(u8); - -impl Default for ScrollingMultiplier { - fn default() -> Self { - Self(3) - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] struct ScrollingHistory(u32); @@ -59,33 +41,19 @@ impl Default for ScrollingHistory { } impl<'de> Deserialize<'de> for ScrollingHistory { - fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error> + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>, { - let value = serde_yaml::Value::deserialize(deserializer)?; - match u32::deserialize(value) { - Ok(lines) => { - if lines > MAX_SCROLLBACK_LINES { - error!( - target: LOG_TARGET_CONFIG, - "Problem with config: scrollback size is {}, but expected a maximum of \ - {}; using {1} instead", - lines, - MAX_SCROLLBACK_LINES, - ); - Ok(ScrollingHistory(MAX_SCROLLBACK_LINES)) - } else { - Ok(ScrollingHistory(lines)) - } - }, - Err(err) => { - error!( - target: LOG_TARGET_CONFIG, - "Problem with config: {}; using default value", err - ); - Ok(Default::default()) - }, + let lines = u32::deserialize(deserializer)?; + + if lines > MAX_SCROLLBACK_LINES { + Err(SerdeError::custom(format!( + "exceeded maximum scrolling history ({}/{})", + lines, MAX_SCROLLBACK_LINES + ))) + } else { + Ok(Self(lines)) } } } |