1use crate::PrimitiveType;
2use indexmap::IndexMap;
3
4pub fn primitive(typ: PrimitiveType) -> Type {
5 Type::Primitive(typ)
6}
7
8pub fn enumeration<I, S>(name: impl ToString, choices: I) -> Type
9where
10 S: ToString,
11 I: IntoIterator<Item = S>,
12{
13 Type::Enum {
14 name: name.to_string(),
15 choices: choices.into_iter().map(|s| s.to_string()).collect(),
16 }
17}
18
19pub struct Schema(pub Vec<ModuleSchema>);
20
21pub struct ModuleSchema {
22 pub extension_name: Option<String>,
23 pub object_types: IndexMap<String, ObjectType>,
24}
25
26#[derive(Clone)]
27pub struct ObjectType {
28 pub pointers: IndexMap<String, Pointer>,
30
31 pub is_top_level: bool,
34
35 pub is_non_locatable: bool,
39
40 pub is_multi: bool,
42}
43
44#[derive(Debug, Clone)]
45pub struct Pointer {
46 pub target: Type,
47
48 pub is_required: bool,
49 pub is_multi: bool,
50
51 pub description: Option<String>,
52 pub deprecated: Option<String>,
53 pub examples: Vec<String>,
54}
55
56#[derive(Debug, Clone)]
57pub enum Type {
58 Primitive(PrimitiveType),
59 Enum { name: String, choices: Vec<String> },
60 ObjectRef(String),
61
62 Union(Vec<String>),
64}
65
66impl Schema {
67 pub fn find_object(&self, key: &str) -> Option<(Option<&str>, &ObjectType)> {
68 for s in &self.0 {
69 if let Some(obj) = s.find_object(key) {
70 return Some((s.extension_name.as_deref(), obj));
71 }
72 }
73 None
74 }
75
76 pub fn std(&mut self) -> &mut ModuleSchema {
77 self.push(None)
78 }
79
80 pub fn ext(&mut self, extension_name: impl ToString) -> &mut ModuleSchema {
81 self.push(Some(extension_name.to_string()))
82 }
83
84 fn push(&mut self, extension_name: Option<String>) -> &mut ModuleSchema {
85 let schema = ModuleSchema {
86 extension_name,
87 object_types: Default::default(),
88 };
89 self.0.push(schema);
90 self.0.last_mut().unwrap()
91 }
92}
93
94impl ModuleSchema {
95 pub fn find_object<'s>(&'s self, key: &str) -> Option<&'s ObjectType> {
96 self.object_types.get(key)
97 }
98
99 pub fn register(&mut self, name: impl ToString, obj: ObjectType) -> Type {
100 for (_, ptr) in &obj.pointers {
101 let mut child_links = Vec::new();
102
103 for target_ref in ptr.target.get_object_refs() {
104 let target = self.object_types.get_mut(target_ref).unwrap();
105 target.is_top_level = false;
106 target.is_multi = ptr.is_multi;
107
108 for (_, ptr) in &target.pointers {
109 child_links.extend(ptr.target.get_object_refs().into_iter().cloned());
110 }
111 }
112
113 if ptr.is_multi {
114 let mut descendant_links = child_links;
116 while let Some(obj_ref) = descendant_links.pop() {
117 let obj = self.object_types.get_mut(&obj_ref).unwrap();
118 obj.is_non_locatable = true;
119
120 for (_, ptr) in &obj.pointers {
121 descendant_links.extend(ptr.target.get_object_refs().into_iter().cloned());
122 }
123 }
124 }
125 }
126
127 self.object_types.insert(name.to_string(), obj);
128 Type::ObjectRef(name.to_string())
129 }
130}
131
132impl ObjectType {
133 pub fn new<I, S>(pointers: I) -> Self
134 where
135 S: ToString,
136 I: IntoIterator<Item = (S, Pointer)>,
137 {
138 Self {
139 pointers: pointers
140 .into_iter()
141 .map(|(k, v)| (k.to_string(), v))
142 .collect(),
143 is_top_level: true,
144 is_multi: false,
145 is_non_locatable: false,
146 }
147 }
148}
149
150impl Type {
151 pub fn is_scalar(&self) -> bool {
152 match self {
153 Type::Primitive(_) => true,
154 Type::Enum { .. } => true,
155 Type::ObjectRef(_) => false,
156 Type::Union(_) => false,
157 }
158 }
159
160 pub fn get_object_refs(&self) -> Vec<&String> {
161 match self {
162 Type::Primitive(_) => Vec::new(),
163 Type::Enum { .. } => Vec::new(),
164 Type::ObjectRef(r) => vec![r],
165 Type::Union(components) => components.iter().collect(),
166 }
167 }
168
169 pub fn new_union(objects: impl IntoIterator<Item = Type>) -> Self {
170 Type::Union(
171 objects
172 .into_iter()
173 .map(|t| match t {
174 Type::ObjectRef(r) => r,
175 _ => panic!(),
176 })
177 .collect(),
178 )
179 }
180}
181
182impl std::fmt::Display for Type {
183 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184 std::fmt::Debug::fmt(&self, f)
185 }
186}
187
188impl Pointer {
189 pub fn new(target: Type) -> Pointer {
190 Pointer {
191 target,
192 is_multi: false,
193 is_required: false,
194 deprecated: None,
195 description: None,
196 examples: Vec::new(),
197 }
198 }
199
200 pub fn multi(mut self) -> Pointer {
201 self.is_multi = true;
202 self
203 }
204
205 pub fn required(mut self) -> Pointer {
206 self.is_required = true;
207 self
208 }
209}
210
211#[allow(dead_code)]
212impl Pointer {
213 fn with_description(mut self, description: impl ToString) -> Self {
214 self.description = Some(description.to_string());
215 self
216 }
217
218 fn with_deprecated(mut self, deprecated: impl ToString) -> Self {
219 self.deprecated = Some(deprecated.to_string());
220 self
221 }
222
223 fn with_examples<I, S>(mut self, examples: I) -> Self
224 where
225 S: ToString,
226 I: IntoIterator<Item = S>,
227 {
228 self.examples = examples.into_iter().map(|s| s.to_string()).collect();
229 self
230 }
231}
232
233pub trait Optional {
234 fn optional(self, key: &'static str) -> impl Iterator<Item = (String, Pointer)> + Clone;
235}
236
237impl<I> Optional for I
238where
239 I: Iterator<Item = (String, Pointer)> + Clone,
240{
241 fn optional(self, key: &'static str) -> impl Iterator<Item = (String, Pointer)> + Clone {
242 self.map(move |(k, mut p)| {
243 if k.as_str() == key {
244 p.is_required = false;
245 }
246 (k, p)
247 })
248 }
249}