aboutsummaryrefslogtreecommitdiff
path: root/alacritty/src/config/font.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/font.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/font.rs')
-rw-r--r--alacritty/src/config/font.rs153
1 files changed, 40 insertions, 113 deletions
diff --git a/alacritty/src/config/font.rs b/alacritty/src/config/font.rs
index 9982352f..3dc11671 100644
--- a/alacritty/src/config/font.rs
+++ b/alacritty/src/config/font.rs
@@ -1,14 +1,11 @@
use std::fmt;
-use crossfont::Size;
-use log::error;
-use serde::de::Visitor;
+use crossfont::Size as FontSize;
+use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer};
-use alacritty_terminal::config::{failure_default, LOG_TARGET_CONFIG};
+use alacritty_config_derive::ConfigDeserialize;
-#[cfg(target_os = "macos")]
-use crate::config::ui_config::DefaultTrueBool;
use crate::config::ui_config::Delta;
/// Font config.
@@ -17,62 +14,41 @@ use crate::config::ui_config::Delta;
/// field in this struct. It might be nice in the future to have defaults for
/// each value independently. Alternatively, maybe erroring when the user
/// doesn't provide complete config is Ok.
-#[serde(default)]
-#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Default, Debug, Clone, PartialEq, Eq)]
pub struct Font {
+ /// Extra spacing per character.
+ pub offset: Delta<i8>,
+
+ /// Glyph offset within character cell.
+ pub glyph_offset: Delta<i8>,
+
+ pub use_thin_strokes: bool,
+
/// Normal font face.
- #[serde(deserialize_with = "failure_default")]
normal: FontDescription,
/// Bold font face.
- #[serde(deserialize_with = "failure_default")]
bold: SecondaryFontDescription,
/// Italic font face.
- #[serde(deserialize_with = "failure_default")]
italic: SecondaryFontDescription,
/// Bold italic font face.
- #[serde(deserialize_with = "failure_default")]
bold_italic: SecondaryFontDescription,
/// Font size in points.
- #[serde(deserialize_with = "DeserializeSize::deserialize")]
- pub size: Size,
-
- /// Extra spacing per character.
- #[serde(deserialize_with = "failure_default")]
- pub offset: Delta<i8>,
-
- /// Glyph offset within character cell.
- #[serde(deserialize_with = "failure_default")]
- pub glyph_offset: Delta<i8>,
-
- #[cfg(target_os = "macos")]
- #[serde(deserialize_with = "failure_default")]
- use_thin_strokes: DefaultTrueBool,
-}
-
-impl Default for Font {
- fn default() -> Font {
- Font {
- size: default_font_size(),
- normal: Default::default(),
- bold: Default::default(),
- italic: Default::default(),
- bold_italic: Default::default(),
- glyph_offset: Default::default(),
- offset: Default::default(),
- #[cfg(target_os = "macos")]
- use_thin_strokes: Default::default(),
- }
- }
+ size: Size,
}
impl Font {
/// Get a font clone with a size modification.
- pub fn with_size(self, size: Size) -> Font {
- Font { size, ..self }
+ pub fn with_size(self, size: FontSize) -> Font {
+ Font { size: Size(size), ..self }
+ }
+
+ #[inline]
+ pub fn size(&self) -> FontSize {
+ self.size.0
}
/// Get normal font description.
@@ -94,29 +70,12 @@ impl Font {
pub fn bold_italic(&self) -> FontDescription {
self.bold_italic.desc(&self.normal)
}
-
- #[cfg(target_os = "macos")]
- pub fn use_thin_strokes(&self) -> bool {
- self.use_thin_strokes.0
- }
-
- #[cfg(not(target_os = "macos"))]
- pub fn use_thin_strokes(&self) -> bool {
- false
- }
-}
-
-fn default_font_size() -> Size {
- Size::new(11.)
}
/// Description of the normal font.
-#[serde(default)]
-#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Debug, Clone, PartialEq, Eq)]
pub struct FontDescription {
- #[serde(deserialize_with = "failure_default")]
pub family: String,
- #[serde(deserialize_with = "failure_default")]
pub style: Option<String>,
}
@@ -135,12 +94,9 @@ impl Default for FontDescription {
}
/// Description of the italic and bold font.
-#[serde(default)]
-#[derive(Debug, Default, Deserialize, Clone, PartialEq, Eq)]
+#[derive(ConfigDeserialize, Debug, Default, Clone, PartialEq, Eq)]
pub struct SecondaryFontDescription {
- #[serde(deserialize_with = "failure_default")]
family: Option<String>,
- #[serde(deserialize_with = "failure_default")]
style: Option<String>,
}
@@ -153,66 +109,37 @@ impl SecondaryFontDescription {
}
}
-trait DeserializeSize: Sized {
- fn deserialize<'a, D>(_: D) -> ::std::result::Result<Self, D::Error>
- where
- D: serde::de::Deserializer<'a>;
+#[derive(Debug, Clone, PartialEq, Eq)]
+struct Size(FontSize);
+
+impl Default for Size {
+ fn default() -> Self {
+ Self(FontSize::new(11.))
+ }
}
-impl DeserializeSize for Size {
- fn deserialize<'a, D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
+impl<'de> Deserialize<'de> for Size {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
- D: serde::de::Deserializer<'a>,
+ D: Deserializer<'de>,
{
- use std::marker::PhantomData;
-
- struct NumVisitor<__D> {
- _marker: PhantomData<__D>,
- }
-
- impl<'a, __D> Visitor<'a> for NumVisitor<__D>
- where
- __D: serde::de::Deserializer<'a>,
- {
- type Value = f64;
+ struct NumVisitor;
+ impl<'v> Visitor<'v> for NumVisitor {
+ type Value = Size;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("f64 or u64")
}
- fn visit_f64<E>(self, value: f64) -> ::std::result::Result<Self::Value, E>
- where
- E: ::serde::de::Error,
- {
- Ok(value)
+ fn visit_f64<E: de::Error>(self, value: f64) -> Result<Self::Value, E> {
+ Ok(Size(FontSize::new(value as f32)))
}
- fn visit_u64<E>(self, value: u64) -> ::std::result::Result<Self::Value, E>
- where
- E: ::serde::de::Error,
- {
- Ok(value as f64)
+ fn visit_u64<E: de::Error>(self, value: u64) -> Result<Self::Value, E> {
+ Ok(Size(FontSize::new(value as f32)))
}
}
- let value = serde_yaml::Value::deserialize(deserializer)?;
- let size = value
- .deserialize_any(NumVisitor::<D> { _marker: PhantomData })
- .map(|v| Size::new(v as _));
-
- // Use default font size as fallback.
- match size {
- Ok(size) => Ok(size),
- Err(err) => {
- let size = default_font_size();
- error!(
- target: LOG_TARGET_CONFIG,
- "Problem with config: {}; using size {}",
- err,
- size.as_f32_pts()
- );
- Ok(size)
- },
- }
+ deserializer.deserialize_any(NumVisitor)
}
}