Add `font_style` and `font_weight` to serialized theme representation (#6838)

Marshall Bowers created

This PR adds `font_style` and `font_weight` as fields on the
`HighlightStyleContent` used in the serialized theme representation.

Release Notes:

- N/A

Change summary

Cargo.lock                 |  1 
Cargo.toml                 |  1 
crates/theme/Cargo.toml    |  1 
crates/theme/src/schema.rs | 69 +++++++++++++++++++++++++++++++++++++++
4 files changed, 71 insertions(+), 1 deletion(-)

Detailed changes

Cargo.lock 🔗

@@ -7864,6 +7864,7 @@ dependencies = [
  "serde",
  "serde_derive",
  "serde_json",
+ "serde_repr",
  "settings",
  "story",
  "toml",

Cargo.toml 🔗

@@ -117,6 +117,7 @@ schemars = { version = "0.8" }
 serde = { version = "1.0", features = ["derive", "rc"] }
 serde_derive = { version = "1.0", features = ["deserialize_in_place"] }
 serde_json = { version = "1.0", features = ["preserve_order", "raw_value"] }
+serde_repr = "0.1"
 smallvec = { version = "1.6", features = ["union"] }
 smol = { version = "1.2" }
 strum = { version = "0.25.0", features = ["derive"] }

crates/theme/Cargo.toml 🔗

@@ -32,6 +32,7 @@ schemars = { workspace = true, features = ["indexmap"] }
 serde.workspace = true
 serde_derive.workspace = true
 serde_json.workspace = true
+serde_repr.workspace = true
 settings = { path = "../settings" }
 story = { path = "../story", optional = true }
 toml.workspace = true

crates/theme/src/schema.rs 🔗

@@ -2,8 +2,12 @@ use anyhow::Result;
 use gpui::{HighlightStyle, Hsla};
 use indexmap::IndexMap;
 use palette::FromColor;
+use schemars::gen::SchemaGenerator;
+use schemars::schema::{Schema, SchemaObject};
 use schemars::JsonSchema;
-use serde::{Deserialize, Serialize};
+use serde::{Deserialize, Deserializer, Serialize};
+use serde_json::Value;
+use serde_repr::{Deserialize_repr, Serialize_repr};
 
 use crate::{StatusColorsRefinement, ThemeColorsRefinement};
 
@@ -1126,8 +1130,71 @@ impl StatusColorsContent {
     }
 }
 
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)]
+#[serde(rename_all = "snake_case")]
+pub enum FontStyleContent {
+    Normal,
+    Italic,
+    Oblique,
+}
+
+#[derive(Debug, Clone, Copy, Serialize_repr, Deserialize_repr)]
+#[repr(u16)]
+pub enum FontWeightContent {
+    Thin = 100,
+    ExtraLight = 200,
+    Light = 300,
+    Normal = 400,
+    Medium = 500,
+    Semibold = 600,
+    Bold = 700,
+    ExtraBold = 800,
+    Black = 900,
+}
+
+impl JsonSchema for FontWeightContent {
+    fn schema_name() -> String {
+        "FontWeightContent".to_owned()
+    }
+
+    fn is_referenceable() -> bool {
+        false
+    }
+
+    fn json_schema(_: &mut SchemaGenerator) -> Schema {
+        let mut schema_object = SchemaObject::default();
+        schema_object.enum_values = Some(vec![
+            100.into(),
+            200.into(),
+            300.into(),
+            400.into(),
+            500.into(),
+            600.into(),
+            700.into(),
+            800.into(),
+            900.into(),
+        ]);
+        schema_object.into()
+    }
+}
+
 #[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
 #[serde(default)]
 pub struct HighlightStyleContent {
     pub color: Option<String>,
+
+    #[serde(deserialize_with = "treat_error_as_none")]
+    pub font_style: Option<FontStyleContent>,
+
+    #[serde(deserialize_with = "treat_error_as_none")]
+    pub font_weight: Option<FontWeightContent>,
+}
+
+fn treat_error_as_none<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
+where
+    T: Deserialize<'de>,
+    D: Deserializer<'de>,
+{
+    let value: Value = Deserialize::deserialize(deserializer)?;
+    Ok(T::deserialize(value).ok())
 }