1use serde::{Deserialize, Serialize};
2
3#[allow(clippy::trivially_copy_pass_by_ref)]
4fn is_false(value: &bool) -> bool {
5 !*value
6}
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct ModelSchema {
11 pub name: String,
12 pub kind: ModelKind,
13 #[serde(skip_serializing_if = "Option::is_none", default)]
14 pub tag: Option<String>,
15 #[serde(skip_serializing_if = "Option::is_none", default)]
16 pub content: Option<String>,
17 #[serde(default, skip_serializing_if = "is_false")]
18 pub untagged: bool,
19 #[serde(skip_serializing_if = "Option::is_none", default)]
20 pub ts_type: Option<String>,
21 #[serde(skip_serializing_if = "Option::is_none", default)]
23 pub doc: Option<String>,
24 #[serde(default, skip_serializing_if = "is_false")]
26 pub deprecated: bool,
27 #[serde(default)]
28 pub fields: Vec<FieldSchema>,
29 #[serde(default)]
30 pub invariants: Vec<InvariantSpec>,
31 #[serde(skip_serializing_if = "Vec::is_empty", default)]
33 pub variants: Vec<VariantSchema>,
34 #[serde(default)]
36 pub commands: Vec<CommandSpec>,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
40#[serde(rename_all = "camelCase")]
41pub enum ModelKind {
42 Struct,
43 Enum,
44 CommandEnum,
45 Newtype,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51pub struct FieldSchema {
52 pub name: String,
53 pub type_name: String,
54 #[serde(default)]
55 pub optional: bool,
56 #[serde(skip_serializing_if = "Option::is_none", default)]
57 pub ts_type: Option<String>,
58 #[serde(skip_serializing_if = "Option::is_none", default)]
59 pub doc: Option<String>,
60 #[serde(default, skip_serializing_if = "is_false")]
61 pub deprecated: bool,
62 #[serde(default, skip_serializing_if = "is_false")]
64 pub flatten: bool,
65 #[serde(default)]
66 pub normalizers: Vec<String>,
67 #[serde(default)]
68 pub invariants: Vec<InvariantSpec>,
69 #[serde(skip_serializing_if = "Option::is_none", default)]
70 pub items: Option<Box<FieldSchema>>,
71 #[serde(skip_serializing_if = "Option::is_none", default)]
72 pub map_keys: Option<Box<FieldSchema>>,
73 #[serde(skip_serializing_if = "Option::is_none", default)]
74 pub map_values: Option<Box<FieldSchema>>,
75}
76
77#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79pub struct VariantSchema {
80 pub name: String,
81 #[serde(skip_serializing_if = "Option::is_none", default)]
82 pub doc: Option<String>,
83 #[serde(default, skip_serializing_if = "is_false")]
84 pub deprecated: bool,
85 #[serde(default)]
86 pub fields: Vec<FieldSchema>,
87 #[serde(default, skip_serializing_if = "is_false")]
88 pub tuple: bool,
89 #[serde(skip_serializing_if = "Vec::is_empty", default)]
90 pub invariants: Vec<InvariantSpec>,
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
95pub struct InvariantSpec {
96 pub code: String,
97 #[serde(default)]
98 pub field: Option<String>,
99 pub kind: String,
100 #[serde(default)]
101 pub detail: String,
102 #[serde(skip_serializing_if = "Option::is_none", default)]
103 pub scope: Option<String>,
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108pub struct CommandSpec {
109 pub name: String,
111 pub reply: String,
113 #[serde(skip_serializing_if = "Option::is_none", default)]
114 pub doc: Option<String>,
115 #[serde(default, skip_serializing_if = "is_false")]
116 pub deprecated: bool,
117 #[serde(default)]
118 pub fields: Vec<FieldSchema>,
119 #[serde(default)]
120 pub invariants: Vec<InvariantSpec>,
121}
122
123impl ModelSchema {
124 pub fn struct_model(name: impl Into<String>, fields: Vec<FieldSchema>) -> Self {
125 let invariants = fields
126 .iter()
127 .flat_map(|f| f.invariants.iter().cloned())
128 .collect();
129 Self {
130 name: name.into(),
131 kind: ModelKind::Struct,
132 tag: None,
133 content: None,
134 untagged: false,
135 ts_type: None,
136 doc: None,
137 deprecated: false,
138 fields,
139 invariants,
140 variants: Vec::new(),
141 commands: Vec::new(),
142 }
143 }
144
145 pub fn command_enum(name: impl Into<String>, commands: Vec<CommandSpec>) -> Self {
146 let invariants = commands
147 .iter()
148 .flat_map(|c| c.invariants.iter().cloned())
149 .collect();
150 Self {
151 name: name.into(),
152 kind: ModelKind::CommandEnum,
153 tag: None,
154 content: None,
155 untagged: false,
156 ts_type: None,
157 doc: None,
158 deprecated: false,
159 fields: Vec::new(),
160 invariants,
161 variants: Vec::new(),
162 commands,
163 }
164 }
165}