
The major change in schemars 1.0 is that now schemas are represented as plain json values instead of specialized datatypes. This allows for more concise construction and manipulation. This change also improves how settings schemas are generated. Each top level settings type was being generated as a full root schema including the definitions it references, and then these were merged. This meant generating all shared definitions multiple times, and might have bugs in cases where there are two types with the same names. Now instead the schemar generator's `definitions` are built up as they normally are and the `Settings` trait no longer has a special `json_schema` method. To handle types that have schema that vary at runtime (`FontFamilyName`, `ThemeName`, etc), values of `ParameterizedJsonSchema` are collected by `inventory`, and the schema definitions for these types are replaced. To help check that this doesn't break anything, I tried to minimize the overall [schema diff](https://gist.github.com/mgsloan/1de549def20399d6f37943a3c1583ee7) with some patches to make the order more consistent + schemas also sorted with `jq -S .`. A skim of the diff shows that the diffs come from: * `enum: ["value"]` turning into `const: "value"` * Differences in handling of newlines for "description" * Schemas for generic types no longer including the parameter name, now all disambiguation is with numeric suffixes * Enums now using `oneOf` instead of `anyOf`. Release Notes: - N/A
76 lines
2.1 KiB
Rust
76 lines
2.1 KiB
Rust
use collections::HashMap;
|
|
use schemars::{JsonSchema, json_schema};
|
|
use serde::Deserialize;
|
|
use serde_json_lenient::Value;
|
|
use std::borrow::Cow;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct VsSnippetsFile {
|
|
#[serde(flatten)]
|
|
pub(crate) snippets: HashMap<String, VsCodeSnippet>,
|
|
}
|
|
|
|
impl VsSnippetsFile {
|
|
pub fn generate_json_schema() -> Value {
|
|
let schema = schemars::generate::SchemaSettings::draft07()
|
|
.into_generator()
|
|
.root_schema_for::<Self>();
|
|
|
|
serde_json_lenient::to_value(schema).unwrap()
|
|
}
|
|
}
|
|
|
|
impl JsonSchema for VsSnippetsFile {
|
|
fn schema_name() -> Cow<'static, str> {
|
|
"VsSnippetsFile".into()
|
|
}
|
|
|
|
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
|
|
let snippet_schema = generator.subschema_for::<VsCodeSnippet>();
|
|
json_schema!({
|
|
"type": "object",
|
|
"additionalProperties": snippet_schema
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, JsonSchema)]
|
|
#[serde(untagged)]
|
|
pub(crate) enum ListOrDirect {
|
|
Single(String),
|
|
List(Vec<String>),
|
|
}
|
|
|
|
impl From<ListOrDirect> for Vec<String> {
|
|
fn from(list: ListOrDirect) -> Self {
|
|
match list {
|
|
ListOrDirect::Single(entry) => vec![entry],
|
|
ListOrDirect::List(entries) => entries,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for ListOrDirect {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
Self::Single(v) => v.to_owned(),
|
|
Self::List(v) => v.join("\n"),
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, JsonSchema)]
|
|
pub(crate) struct VsCodeSnippet {
|
|
/// The snippet prefix used to decide whether a completion menu should be shown.
|
|
pub(crate) prefix: Option<ListOrDirect>,
|
|
|
|
/// The snippet content. Use `$1` and `${1:defaultText}` to define cursor positions and `$0` for final cursor position.
|
|
pub(crate) body: ListOrDirect,
|
|
|
|
/// The snippet description displayed inside the completion menu.
|
|
pub(crate) description: Option<ListOrDirect>,
|
|
}
|