sokf_core/
output_schema.rs1use schemars::{JsonSchema, Schema, generate::SchemaSettings, transform::RecursiveTransform};
7use serde_json::Value;
8
9use crate::{
10 FormatsJson, GraphJson, GuideReport, HarnessReport, InitResult, LocateReport, NewResult,
11 ValidateReport, harness,
12};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum Output {
17 Validate,
19 Graph,
21 Init,
23 Harness,
25 HarnessHookAnswer,
27 Locate,
29 New,
31 InfoFormats,
33 Guide,
35}
36
37impl Output {
38 pub const ALL: [Output; 9] = [
40 Output::Validate,
41 Output::Graph,
42 Output::Init,
43 Output::Harness,
44 Output::HarnessHookAnswer,
45 Output::Locate,
46 Output::New,
47 Output::InfoFormats,
48 Output::Guide,
49 ];
50
51 pub fn file(self) -> &'static str {
53 match self {
54 Output::Validate => "validate-report.schema.json",
55 Output::Graph => "graph.schema.json",
56 Output::Init => "init-report.schema.json",
57 Output::Harness => "harness-report.schema.json",
58 Output::HarnessHookAnswer => "harness-hook-answer.schema.json",
59 Output::Locate => "locate-report.schema.json",
60 Output::New => "new-report.schema.json",
61 Output::InfoFormats => "info-formats-registry.schema.json",
62 Output::Guide => "guide-report.schema.json",
63 }
64 }
65
66 pub fn schema(self) -> Schema {
68 match self {
69 Output::Validate => schema::<ValidateReport>(),
70 Output::Graph => schema::<GraphJson>(),
71 Output::Init => schema::<InitResult>(),
72 Output::Harness => schema::<HarnessReport>(),
73 Output::HarnessHookAnswer => schema::<harness::HookAnswer>(),
74 Output::Locate => schema::<LocateReport>(),
75 Output::New => schema::<NewResult>(),
76 Output::InfoFormats => schema::<FormatsJson>(),
77 Output::Guide => schema::<GuideReport>(),
78 }
79 }
80
81 pub fn schema_text(self) -> String {
84 serde_json::to_string_pretty(&self.schema()).expect("a schema serialises") + "\n"
85 }
86}
87
88pub fn output_schemas() -> Vec<(&'static str, Schema)> {
91 Output::ALL.iter().map(|o| (o.file(), o.schema())).collect()
92}
93
94fn schema<T: JsonSchema>() -> Schema {
98 SchemaSettings::draft2020_12()
99 .with_transform(RecursiveTransform(unwrap_description))
100 .into_generator()
101 .into_root_schema_for::<T>()
102}
103
104fn unwrap_description(schema: &mut Schema) {
105 if let Some(Value::String(text)) = schema.get_mut("description") {
106 *text = text
107 .split("\n\n")
108 .map(|paragraph| paragraph.split('\n').collect::<Vec<_>>().join(" "))
109 .collect::<Vec<_>>()
110 .join("\n\n");
111 }
112}