Skip to main content

eure_codegen_ir/
schema.rs

1use indexmap::{IndexMap, IndexSet};
2
3use crate::codegen::FieldCodegenIr;
4use crate::ids::{QualifiedTypeName, SchemaNodeIrId};
5use crate::value::{DecimalInt, ValueIr};
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct SchemaNodeIr {
9    content: SchemaNodeContentIr,
10    metadata: SchemaMetadataIr,
11    ext_types: IndexMap<String, ExtTypeIr>,
12}
13
14impl SchemaNodeIr {
15    pub fn new(
16        content: SchemaNodeContentIr,
17        metadata: SchemaMetadataIr,
18        ext_types: IndexMap<String, ExtTypeIr>,
19    ) -> Self {
20        Self {
21            content,
22            metadata,
23            ext_types,
24        }
25    }
26
27    pub fn content(&self) -> &SchemaNodeContentIr {
28        &self.content
29    }
30
31    pub fn content_mut(&mut self) -> &mut SchemaNodeContentIr {
32        &mut self.content
33    }
34
35    pub fn metadata(&self) -> &SchemaMetadataIr {
36        &self.metadata
37    }
38
39    pub fn metadata_mut(&mut self) -> &mut SchemaMetadataIr {
40        &mut self.metadata
41    }
42
43    pub fn ext_types(&self) -> &IndexMap<String, ExtTypeIr> {
44        &self.ext_types
45    }
46
47    pub fn ext_types_mut(&mut self) -> &mut IndexMap<String, ExtTypeIr> {
48        &mut self.ext_types
49    }
50}
51
52#[derive(Debug, Clone, PartialEq)]
53pub enum SchemaNodeContentIr {
54    Any,
55    Text(TextSchemaIr),
56    Integer(IntegerSchemaIr),
57    Float(FloatSchemaIr),
58    Boolean,
59    Null,
60    Literal(ValueIr),
61    Array(ArraySchemaIr),
62    Map(MapSchemaIr),
63    Record(RecordSchemaIr),
64    Tuple(TupleSchemaIr),
65    Union(UnionSchemaIr),
66    Reference(QualifiedTypeName),
67}
68
69#[derive(Debug, Clone, PartialEq)]
70pub struct TextSchemaIr {
71    pub language: Option<String>,
72    pub min_length: Option<u32>,
73    pub max_length: Option<u32>,
74    pub pattern: Option<String>,
75    pub unknown_fields: IndexMap<String, ValueIr>,
76}
77
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct IntegerSchemaIr {
80    pub min: BoundIr<DecimalInt>,
81    pub max: BoundIr<DecimalInt>,
82    pub multiple_of: Option<DecimalInt>,
83}
84
85#[derive(Debug, Clone, PartialEq)]
86pub struct FloatSchemaIr {
87    pub min: BoundIr<f64>,
88    pub max: BoundIr<f64>,
89    pub multiple_of: Option<f64>,
90    pub precision: FloatPrecisionIr,
91}
92
93#[derive(Debug, Clone, PartialEq)]
94pub struct ArraySchemaIr {
95    pub item: SchemaNodeIrId,
96    pub min_length: Option<u32>,
97    pub max_length: Option<u32>,
98    pub unique: bool,
99    pub contains: Option<SchemaNodeIrId>,
100    pub binding_style: Option<BindingStyleIr>,
101}
102
103#[derive(Debug, Clone, PartialEq)]
104pub struct MapSchemaIr {
105    pub key: SchemaNodeIrId,
106    pub value: SchemaNodeIrId,
107    pub min_size: Option<u32>,
108    pub max_size: Option<u32>,
109}
110
111#[derive(Debug, Clone, PartialEq)]
112pub struct RecordSchemaIr {
113    properties: IndexMap<String, RecordFieldSchemaIr>,
114    flatten: Vec<SchemaNodeIrId>,
115    unknown_fields: UnknownFieldsPolicyIr,
116}
117
118impl RecordSchemaIr {
119    pub fn new(
120        properties: IndexMap<String, RecordFieldSchemaIr>,
121        flatten: Vec<SchemaNodeIrId>,
122        unknown_fields: UnknownFieldsPolicyIr,
123    ) -> Self {
124        Self {
125            properties,
126            flatten,
127            unknown_fields,
128        }
129    }
130
131    pub fn properties(&self) -> &IndexMap<String, RecordFieldSchemaIr> {
132        &self.properties
133    }
134
135    pub fn properties_mut(&mut self) -> &mut IndexMap<String, RecordFieldSchemaIr> {
136        &mut self.properties
137    }
138
139    pub fn flatten(&self) -> &[SchemaNodeIrId] {
140        &self.flatten
141    }
142
143    pub fn flatten_mut(&mut self) -> &mut Vec<SchemaNodeIrId> {
144        &mut self.flatten
145    }
146
147    pub fn unknown_fields(&self) -> &UnknownFieldsPolicyIr {
148        &self.unknown_fields
149    }
150
151    pub fn unknown_fields_mut(&mut self) -> &mut UnknownFieldsPolicyIr {
152        &mut self.unknown_fields
153    }
154}
155
156#[derive(Debug, Clone, PartialEq)]
157pub struct RecordFieldSchemaIr {
158    schema: SchemaNodeIrId,
159    optional: bool,
160    binding_style: Option<BindingStyleIr>,
161    field_codegen: FieldCodegenIr,
162}
163
164impl RecordFieldSchemaIr {
165    pub fn new(
166        schema: SchemaNodeIrId,
167        optional: bool,
168        binding_style: Option<BindingStyleIr>,
169        field_codegen: FieldCodegenIr,
170    ) -> Self {
171        Self {
172            schema,
173            optional,
174            binding_style,
175            field_codegen,
176        }
177    }
178
179    pub fn schema(&self) -> SchemaNodeIrId {
180        self.schema
181    }
182
183    pub fn optional(&self) -> bool {
184        self.optional
185    }
186
187    pub fn binding_style(&self) -> Option<BindingStyleIr> {
188        self.binding_style
189    }
190
191    pub fn field_codegen(&self) -> &FieldCodegenIr {
192        &self.field_codegen
193    }
194
195    pub fn field_codegen_mut(&mut self) -> &mut FieldCodegenIr {
196        &mut self.field_codegen
197    }
198}
199
200#[derive(Debug, Clone, PartialEq)]
201pub enum UnknownFieldsPolicyIr {
202    Deny,
203    Allow,
204    Schema(SchemaNodeIrId),
205}
206
207#[derive(Debug, Clone, PartialEq)]
208pub struct TupleSchemaIr {
209    pub elements: Vec<SchemaNodeIrId>,
210    pub binding_style: Option<BindingStyleIr>,
211}
212
213#[derive(Debug, Clone, PartialEq)]
214pub struct UnionSchemaIr {
215    variants: IndexMap<String, SchemaNodeIrId>,
216    unambiguous: IndexSet<String>,
217    deny_untagged: IndexSet<String>,
218    interop: UnionInteropIr,
219}
220
221impl UnionSchemaIr {
222    pub fn new(
223        variants: IndexMap<String, SchemaNodeIrId>,
224        unambiguous: IndexSet<String>,
225        deny_untagged: IndexSet<String>,
226        interop: UnionInteropIr,
227    ) -> Self {
228        Self {
229            variants,
230            unambiguous,
231            deny_untagged,
232            interop,
233        }
234    }
235
236    pub fn variants(&self) -> &IndexMap<String, SchemaNodeIrId> {
237        &self.variants
238    }
239
240    pub fn variants_mut(&mut self) -> &mut IndexMap<String, SchemaNodeIrId> {
241        &mut self.variants
242    }
243
244    pub fn unambiguous(&self) -> &IndexSet<String> {
245        &self.unambiguous
246    }
247
248    pub fn unambiguous_mut(&mut self) -> &mut IndexSet<String> {
249        &mut self.unambiguous
250    }
251
252    pub fn deny_untagged(&self) -> &IndexSet<String> {
253        &self.deny_untagged
254    }
255
256    pub fn deny_untagged_mut(&mut self) -> &mut IndexSet<String> {
257        &mut self.deny_untagged
258    }
259
260    pub fn interop(&self) -> &UnionInteropIr {
261        &self.interop
262    }
263
264    pub fn interop_mut(&mut self) -> &mut UnionInteropIr {
265        &mut self.interop
266    }
267}
268
269#[derive(Debug, Clone, PartialEq, Eq, Default)]
270pub struct UnionInteropIr {
271    pub variant_repr: Option<VariantReprIr>,
272}
273
274#[derive(Debug, Clone, PartialEq, Eq)]
275pub enum VariantReprIr {
276    External,
277    Internal { tag: String },
278    Adjacent { tag: String, content: String },
279    Untagged,
280}
281
282#[derive(Debug, Clone, PartialEq, Eq)]
283pub enum FloatPrecisionIr {
284    F32,
285    F64,
286}
287
288#[derive(Debug, Clone, Copy, PartialEq, Eq)]
289pub enum BindingStyleIr {
290    Inline,
291    BindingBlock,
292    BindingValueBlock,
293    Section,
294    SectionBlock,
295    SectionValueBlock,
296    Flatten,
297}
298
299#[derive(Debug, Clone, Copy, PartialEq, Eq)]
300pub enum ArrayBindingStyleIr {
301    /// Single inline binding: `path = [...]`.
302    Inline,
303    /// Per-element emission with `[]` push marker.
304    PerElement(BindingStyleIr),
305    /// Per-element emission with explicit `[i]` indices.
306    PerElementIndexed(BindingStyleIr),
307}
308
309#[derive(Debug, Clone, PartialEq)]
310pub struct ExtTypeIr {
311    pub schema: SchemaNodeIrId,
312    pub optional: bool,
313    pub binding_style: Option<BindingStyleIr>,
314}
315
316#[derive(Debug, Clone, PartialEq, Default)]
317pub struct SchemaMetadataIr {
318    pub description: Option<DescriptionIr>,
319    pub deprecated: bool,
320    pub default: Option<ValueIr>,
321    pub examples: Option<Vec<ValueIr>>,
322}
323
324#[derive(Debug, Clone, PartialEq, Eq)]
325pub enum DescriptionIr {
326    String(String),
327    Markdown(String),
328}
329
330#[derive(Debug, Clone, PartialEq, Eq, Default)]
331pub enum BoundIr<T> {
332    #[default]
333    Unbounded,
334    Inclusive(T),
335    Exclusive(T),
336}