aboutsummaryrefslogtreecommitdiff
path: root/alacritty_terminal/src/config/colors.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_terminal/src/config/colors.rs
parent37a3198d8882463c9873011c1d18c325ea46d7c8 (diff)
downloadalacritty-6e1b9d8b2502f5b47dc28eb5e0853e46ad8b4e84.tar.gz
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/colors.rs')
-rw-r--r--alacritty_terminal/src/config/colors.rs237
1 files changed, 79 insertions, 158 deletions
diff --git a/alacritty_terminal/src/config/colors.rs b/alacritty_terminal/src/config/colors.rs
index a292fde4..df52f19f 100644
--- a/alacritty_terminal/src/config/colors.rs
+++ b/alacritty_terminal/src/config/colors.rs
@@ -1,42 +1,24 @@
-use log::error;
+use serde::de::Error as SerdeError;
use serde::{Deserialize, Deserializer};
-use serde_yaml::Value;
-use crate::config::{failure_default, LOG_TARGET_CONFIG};
+use alacritty_config_derive::ConfigDeserialize;
+
use crate::term::color::{CellRgb, Rgb};
-#[serde(default)]
-#[derive(Deserialize, Clone, Debug, Default, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Clone, Debug, Default, PartialEq, Eq)]
pub struct Colors {
- #[serde(deserialize_with = "failure_default")]
pub primary: PrimaryColors,
- #[serde(deserialize_with = "failure_default")]
- pub cursor: CursorColors,
- #[serde(deserialize_with = "failure_default")]
- pub vi_mode_cursor: CursorColors,
- #[serde(deserialize_with = "failure_default")]
+ pub cursor: InvertedCellColors,
+ pub vi_mode_cursor: InvertedCellColors,
pub selection: InvertedCellColors,
- #[serde(deserialize_with = "failure_default")]
- normal: NormalColors,
- #[serde(deserialize_with = "failure_default")]
- bright: BrightColors,
- #[serde(deserialize_with = "failure_default")]
- pub dim: Option<AnsiColors>,
- #[serde(deserialize_with = "failure_default")]
+ pub normal: NormalColors,
+ pub bright: BrightColors,
+ pub dim: Option<DimColors>,
pub indexed_colors: Vec<IndexedColor>,
- #[serde(deserialize_with = "failure_default")]
pub search: SearchColors,
}
impl Colors {
- pub fn normal(&self) -> &AnsiColors {
- &self.normal.0
- }
-
- pub fn bright(&self) -> &AnsiColors {
- &self.bright.0
- }
-
pub fn search_bar_foreground(&self) -> Rgb {
self.search.bar.foreground.unwrap_or(self.primary.background)
}
@@ -46,158 +28,88 @@ impl Colors {
}
}
-#[derive(Deserialize, Copy, Clone, Debug, PartialEq, Eq)]
-struct DefaultForegroundCellRgb(CellRgb);
-
-impl Default for DefaultForegroundCellRgb {
- fn default() -> Self {
- Self(CellRgb::CellForeground)
- }
-}
-
-#[derive(Deserialize, Copy, Clone, Debug, PartialEq, Eq)]
-struct DefaultBackgroundCellRgb(CellRgb);
-
-impl Default for DefaultBackgroundCellRgb {
- fn default() -> Self {
- Self(CellRgb::CellBackground)
- }
-}
-
-#[serde(default)]
-#[derive(Deserialize, Clone, Default, Debug, PartialEq, Eq)]
+#[derive(Deserialize, Copy, Clone, Default, Debug, PartialEq, Eq)]
pub struct IndexedColor {
- #[serde(deserialize_with = "deserialize_color_index")]
- pub index: u8,
- #[serde(deserialize_with = "failure_default")]
pub color: Rgb,
-}
-fn deserialize_color_index<'a, D>(deserializer: D) -> Result<u8, D::Error>
-where
- D: Deserializer<'a>,
-{
- let value = Value::deserialize(deserializer)?;
- match u8::deserialize(value) {
- Ok(index) => {
- if index < 16 {
- error!(
- target: LOG_TARGET_CONFIG,
- "Problem with config: indexed_color's index is {}, but a value bigger than 15 \
- was expected; ignoring setting",
- index
- );
-
- // Return value out of range to ignore this color.
- Ok(0)
- } else {
- Ok(index)
- }
- },
- Err(err) => {
- error!(target: LOG_TARGET_CONFIG, "Problem with config: {}; ignoring setting", err);
+ index: ColorIndex,
+}
- // Return value out of range to ignore this color.
- Ok(0)
- },
+impl IndexedColor {
+ #[inline]
+ pub fn index(&self) -> u8 {
+ self.index.0
}
}
-#[serde(default)]
-#[derive(Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
-pub struct CursorColors {
- #[serde(deserialize_with = "failure_default")]
- text: DefaultBackgroundCellRgb,
- #[serde(deserialize_with = "failure_default")]
- cursor: DefaultForegroundCellRgb,
-}
+#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
+struct ColorIndex(u8);
-impl CursorColors {
- pub fn text(self) -> CellRgb {
- self.text.0
- }
+impl<'de> Deserialize<'de> for ColorIndex {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let index = u8::deserialize(deserializer)?;
- pub fn cursor(self) -> CellRgb {
- self.cursor.0
+ if index < 16 {
+ Err(SerdeError::custom(
+ "Config error: indexed_color's index is {}, but a value bigger than 15 was \
+ expected; ignoring setting",
+ ))
+ } else {
+ Ok(Self(index))
+ }
}
}
-#[serde(default)]
-#[derive(Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub struct InvertedCellColors {
- #[serde(deserialize_with = "failure_default", alias = "text")]
- foreground: DefaultBackgroundCellRgb,
- #[serde(deserialize_with = "failure_default")]
- background: DefaultForegroundCellRgb,
+ #[config(alias = "text")]
+ pub foreground: CellRgb,
+ #[config(alias = "cursor")]
+ pub background: CellRgb,
}
-impl InvertedCellColors {
- pub fn foreground(self) -> CellRgb {
- self.foreground.0
- }
-
- pub fn background(self) -> CellRgb {
- self.background.0
+impl Default for InvertedCellColors {
+ fn default() -> Self {
+ Self { foreground: CellRgb::CellBackground, background: CellRgb::CellForeground }
}
}
-#[serde(default)]
-#[derive(Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct SearchColors {
- #[serde(deserialize_with = "failure_default")]
- pub matches: MatchColors,
- #[serde(deserialize_with = "failure_default")]
pub focused_match: InvertedCellColors,
- #[serde(deserialize_with = "failure_default")]
+ pub matches: MatchColors,
bar: BarColors,
}
-#[serde(default)]
-#[derive(Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub struct MatchColors {
- #[serde(deserialize_with = "failure_default")]
pub foreground: CellRgb,
- #[serde(deserialize_with = "deserialize_match_background")]
pub background: CellRgb,
}
impl Default for MatchColors {
fn default() -> Self {
- Self { foreground: CellRgb::default(), background: default_match_background() }
+ Self {
+ background: CellRgb::Rgb(Rgb { r: 0xff, g: 0xff, b: 0xff }),
+ foreground: CellRgb::Rgb(Rgb { r: 0x00, g: 0x00, b: 0x00 }),
+ }
}
}
-fn deserialize_match_background<'a, D>(deserializer: D) -> Result<CellRgb, D::Error>
-where
- D: Deserializer<'a>,
-{
- let value = Value::deserialize(deserializer)?;
- Ok(CellRgb::deserialize(value).unwrap_or_else(|_| default_match_background()))
-}
-
-fn default_match_background() -> CellRgb {
- CellRgb::Rgb(Rgb { r: 0xff, g: 0xff, b: 0xff })
-}
-
-#[serde(default)]
-#[derive(Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct BarColors {
- #[serde(deserialize_with = "failure_default")]
foreground: Option<Rgb>,
- #[serde(deserialize_with = "failure_default")]
background: Option<Rgb>,
}
-#[serde(default)]
-#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Clone, Debug, PartialEq, Eq)]
pub struct PrimaryColors {
- #[serde(deserialize_with = "failure_default")]
- pub background: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub foreground: Rgb,
- #[serde(deserialize_with = "failure_default")]
+ pub background: Rgb,
pub bright_foreground: Option<Rgb>,
- #[serde(deserialize_with = "failure_default")]
pub dim_foreground: Option<Rgb>,
}
@@ -212,33 +124,21 @@ impl Default for PrimaryColors {
}
}
-/// The 8-colors sections of config.
-#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
-pub struct AnsiColors {
- #[serde(deserialize_with = "failure_default")]
+#[derive(ConfigDeserialize, Clone, Debug, PartialEq, Eq)]
+pub struct NormalColors {
pub black: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub red: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub green: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub yellow: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub blue: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub magenta: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub cyan: Rgb,
- #[serde(deserialize_with = "failure_default")]
pub white: Rgb,
}
-#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
-struct NormalColors(AnsiColors);
-
impl Default for NormalColors {
fn default() -> Self {
- NormalColors(AnsiColors {
+ NormalColors {
black: Rgb { r: 0x1d, g: 0x1f, b: 0x21 },
red: Rgb { r: 0xcc, g: 0x66, b: 0x66 },
green: Rgb { r: 0xb5, g: 0xbd, b: 0x68 },
@@ -247,16 +147,25 @@ impl Default for NormalColors {
magenta: Rgb { r: 0xb2, g: 0x94, b: 0xbb },
cyan: Rgb { r: 0x8a, g: 0xbe, b: 0xb7 },
white: Rgb { r: 0xc5, g: 0xc8, b: 0xc6 },
- })
+ }
}
}
-#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
-struct BrightColors(AnsiColors);
+#[derive(ConfigDeserialize, Clone, Debug, PartialEq, Eq)]
+pub struct BrightColors {
+ pub black: Rgb,
+ pub red: Rgb,
+ pub green: Rgb,
+ pub yellow: Rgb,
+ pub blue: Rgb,
+ pub magenta: Rgb,
+ pub cyan: Rgb,
+ pub white: Rgb,
+}
impl Default for BrightColors {
fn default() -> Self {
- BrightColors(AnsiColors {
+ BrightColors {
black: Rgb { r: 0x66, g: 0x66, b: 0x66 },
red: Rgb { r: 0xd5, g: 0x4e, b: 0x53 },
green: Rgb { r: 0xb9, g: 0xca, b: 0x4a },
@@ -265,6 +174,18 @@ impl Default for BrightColors {
magenta: Rgb { r: 0xc3, g: 0x97, b: 0xd8 },
cyan: Rgb { r: 0x70, g: 0xc0, b: 0xb1 },
white: Rgb { r: 0xea, g: 0xea, b: 0xea },
- })
+ }
}
}
+
+#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
+pub struct DimColors {
+ pub black: Rgb,
+ pub red: Rgb,
+ pub green: Rgb,
+ pub yellow: Rgb,
+ pub blue: Rgb,
+ pub magenta: Rgb,
+ pub cyan: Rgb,
+ pub white: Rgb,
+}