1use anyhow::Result;
2use gpui::SharedString;
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use serde_json::json;
6
7/// Represents a schema for a specific adapter
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
9pub struct AdapterSchema {
10 /// The adapter name identifier
11 pub adapter: SharedString,
12 /// The JSON schema for this adapter's configuration
13 pub schema: serde_json::Value,
14}
15
16#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
17#[serde(transparent)]
18pub struct AdapterSchemas(pub Vec<AdapterSchema>);
19
20impl AdapterSchemas {
21 pub fn generate_json_schema(&self) -> Result<serde_json_lenient::Value> {
22 let adapter_conditions = self
23 .0
24 .iter()
25 .map(|adapter_schema| {
26 let adapter_name = adapter_schema.adapter.to_string();
27 json!({
28 "if": {
29 "properties": {
30 "adapter": { "const": adapter_name }
31 }
32 },
33 "then": adapter_schema.schema
34 })
35 })
36 .collect::<Vec<_>>();
37
38 let schema = serde_json_lenient::json!({
39 "$schema": "http://json-schema.org/draft-07/schema#",
40 "title": "Debug Adapter Configurations",
41 "description": "Configuration for debug adapters. Schema changes based on the selected adapter.",
42 "type": "array",
43 "items": {
44 "type": "object",
45 "required": ["adapter", "label"],
46 "properties": {
47 "adapter": {
48 "type": "string",
49 "description": "The name of the debug adapter"
50 },
51 "label": {
52 "type": "string",
53 "description": "The name of the debug configuration"
54 },
55 },
56 "allOf": adapter_conditions
57 }
58 });
59
60 Ok(serde_json_lenient::to_value(schema)?)
61 }
62}