headless_lms_utils/
json_schema_types.rs1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
5#[serde(rename_all = "snake_case")]
6pub enum JSONType {
7 JsonSchema,
8 Object,
9 Array,
10 String,
11 Number,
12 Integer,
13 Boolean,
14}
15
16#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
19#[serde(rename_all = "camelCase")]
20pub struct Schema {
21 #[serde(rename = "type")]
22 pub type_field: JSONType,
24 pub properties: IndexMap<String, SchemaPropertyType>,
29 pub required: Vec<String>,
31 pub additional_properties: bool,
33 #[serde(skip_serializing_if = "Option::is_none")]
35 pub description: Option<String>,
36}
37
38impl Schema {
39 pub fn strict_object(
45 properties: IndexMap<String, SchemaPropertyType>,
46 description: Option<&str>,
47 ) -> Self {
48 let required = properties.keys().cloned().collect();
49 Self {
50 type_field: JSONType::Object,
51 properties,
52 required,
53 additional_properties: false,
54 description: description.map(str::to_string),
55 }
56 }
57}
58
59#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
60#[serde(untagged)]
61pub enum SchemaPropertyType {
62 ArrayProperty(ArrayProperty),
63 Object(Schema),
64 Item(JsonItem),
65}
66
67#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
68pub struct ArrayProperty {
69 #[serde(rename = "type")]
70 pub type_field: JSONType,
71 pub items: ArrayItem,
72 #[serde(skip_serializing_if = "Option::is_none")]
73 pub description: Option<String>,
74}
75
76#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
77#[serde(untagged)]
78pub enum ArrayItem {
79 Schema(Schema),
80 JsonItem(JsonItem),
81}
82
83#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
85pub struct JsonItem {
86 #[serde(rename = "type")]
87 pub type_field: JSONType,
88 #[serde(skip_serializing_if = "Option::is_none")]
89 pub description: Option<String>,
90}
91
92pub fn string_array_property(description: Option<&str>) -> SchemaPropertyType {
96 SchemaPropertyType::ArrayProperty(ArrayProperty {
97 type_field: JSONType::Array,
98 items: ArrayItem::JsonItem(JsonItem {
99 type_field: JSONType::String,
100 description: None,
101 }),
102 description: description.map(str::to_string),
103 })
104}