Factor out construction of font-related JSON schemas (#15341)

This PR factors out the construction of the font-related JSON schemas,
as they were used in multiple places.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-27 12:28:50 -04:00 committed by GitHub
parent 1ffb34c5fc
commit b8982ad385
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 44 additions and 72 deletions

View file

@ -1,4 +1,39 @@
use schemars::schema::{RootSchema, Schema};
use schemars::schema::{ArrayValidation, InstanceType, RootSchema, Schema, SchemaObject};
use serde_json::Value;
pub struct SettingsJsonSchemaParams<'a> {
pub staff_mode: bool,
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(InstanceType::Array.into()),
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;