zed/crates/settings/src/json_schema.rs
Marshall Bowers 77de20c23a
settings: Allow null as a value for font fallback fields (#20186)
This PR updates the `buffer_font_fallbacks` and `ui_font_fallbacks`
settings to allow `null` as a value instead of showing a warning.

Related to https://github.com/zed-industries/zed/issues/18006.

Release Notes:

- Updated the settings schema to allow `null` as a value for
`buffer_font_fallbacks` and `ui_font_fallbacks` instead of showing a
warning.
2024-11-04 12:58:10 -05:00

75 lines
2.3 KiB
Rust

use schemars::schema::{
ArrayValidation, InstanceType, RootSchema, Schema, SchemaObject, SingleOrVec,
};
use serde_json::Value;
pub struct SettingsJsonSchemaParams<'a> {
pub language_names: &'a [String],
pub font_names: &'a [String],
}
impl<'a> SettingsJsonSchemaParams<'a> {
pub fn font_family_schema(&self) -> Schema {
let available_fonts: Vec<_> = self.font_names.iter().cloned().map(Value::String).collect();
SchemaObject {
instance_type: Some(InstanceType::String.into()),
enum_values: Some(available_fonts),
..Default::default()
}
.into()
}
pub fn font_fallback_schema(&self) -> Schema {
SchemaObject {
instance_type: Some(SingleOrVec::Vec(vec![
InstanceType::Array,
InstanceType::Null,
])),
array: Some(Box::new(ArrayValidation {
items: Some(schemars::schema::SingleOrVec::Single(Box::new(
self.font_family_schema(),
))),
unique_items: Some(true),
..Default::default()
})),
..Default::default()
}
.into()
}
}
type PropertyName<'a> = &'a str;
type ReferencePath<'a> = &'a str;
/// Modifies the provided [`RootSchema`] by adding references to all of the specified properties.
///
/// # Examples
///
/// ```
/// # let root_schema = RootSchema::default();
/// add_references_to_properties(&mut root_schema, &[
/// ("property_a", "#/definitions/DefinitionA"),
/// ("property_b", "#/definitions/DefinitionB"),
/// ])
/// ```
pub fn add_references_to_properties(
root_schema: &mut RootSchema,
properties_with_references: &[(PropertyName, ReferencePath)],
) {
for (property, definition) in properties_with_references {
let Some(schema) = root_schema.schema.object().properties.get_mut(*property) else {
log::warn!("property '{property}' not found in JSON schema");
continue;
};
match schema {
Schema::Object(schema) => {
schema.reference = Some(definition.to_string());
}
Schema::Bool(_) => {
// Boolean schemas can't have references.
}
}
}
}