| 1 |
// Copyright 2025 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/// Produce a synthetic JSON document containing a default config according to |
| 16 |
/// the schema. |
| 17 |
/// |
| 18 |
/// # Limitations |
| 19 |
/// Defaults in `"additionalProperties"` are ignored, as are those in |
| 20 |
/// `"definitions"` nodes (which might be referenced by `"$ref"`). |
| 21 |
pub fn default_config_from_schema() -> serde_json::Value { |
| 22 |
fn visit(schema: &serde_json::Value) -> Option<serde_json::Value> { |
| 23 |
let schema = schema.as_object().expect("schemas to be objects"); |
| 24 |
if let Some(default) = schema.get("default") { |
| 25 |
Some(default.clone()) |
| 26 |
} else if let Some(properties) = schema.get("properties") { |
| 27 |
let prop_defaults: serde_json::Map<_, _> = properties |
| 28 |
.as_object() |
| 29 |
.expect("`properties` to be an object") |
| 30 |
.iter() |
| 31 |
.filter_map(|(prop_name, prop_schema)| { |
| 32 |
visit(prop_schema).map(|prop_default| (prop_name.clone(), prop_default)) |
| 33 |
}) |
| 34 |
.collect(); |
| 35 |
(!prop_defaults.is_empty()).then_some(serde_json::Value::Object(prop_defaults)) |
| 36 |
} else { |
| 37 |
None |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
visit( |
| 42 |
&serde_json::from_str(include_str!("../../src/config-schema.json")) |
| 43 |
.expect("`config-schema.json` to be valid JSON"), |
| 44 |
) |
| 45 |
.expect("some settings to have defaults") |
| 46 |
} |