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

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

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-01-26 22:40:31 -05:00 committed by GitHub
parent fd3c96dbed
commit 536a4ab87a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 1 deletions

1
Cargo.lock generated
View File

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

View File

@ -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"] }

View File

@ -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

View File

@ -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())
}