Skip to main content

eure_codegen_ir/
rust_binding.rs

1use crate::emission::TypeEmissionConfigIr;
2use crate::ids::{QualifiedTypeName, RustPathIr};
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct RustBindingIr {
6    kind: RustTypeKindIr,
7    container: ContainerAttrsIr,
8    fields: Vec<RustFieldIr>,
9    variants: Vec<RustVariantIr>,
10    generics: RustGenericsIr,
11    where_clause: WhereClauseIr,
12    emission: TypeEmissionConfigIr,
13}
14
15impl Default for RustBindingIr {
16    fn default() -> Self {
17        Self {
18            kind: RustTypeKindIr::Unit,
19            container: ContainerAttrsIr::default(),
20            fields: Vec::new(),
21            variants: Vec::new(),
22            generics: RustGenericsIr::default(),
23            where_clause: WhereClauseIr::default(),
24            emission: TypeEmissionConfigIr::default(),
25        }
26    }
27}
28
29impl RustBindingIr {
30    pub fn new(
31        kind: RustTypeKindIr,
32        container: ContainerAttrsIr,
33        fields: Vec<RustFieldIr>,
34        variants: Vec<RustVariantIr>,
35        generics: RustGenericsIr,
36        where_clause: WhereClauseIr,
37        emission: TypeEmissionConfigIr,
38    ) -> Self {
39        Self {
40            kind,
41            container,
42            fields,
43            variants,
44            generics,
45            where_clause,
46            emission,
47        }
48    }
49
50    pub fn kind(&self) -> &RustTypeKindIr {
51        &self.kind
52    }
53
54    pub fn container(&self) -> &ContainerAttrsIr {
55        &self.container
56    }
57
58    pub fn container_mut(&mut self) -> &mut ContainerAttrsIr {
59        &mut self.container
60    }
61
62    pub fn fields(&self) -> &[RustFieldIr] {
63        &self.fields
64    }
65
66    pub fn fields_mut(&mut self) -> &mut Vec<RustFieldIr> {
67        &mut self.fields
68    }
69
70    pub fn variants(&self) -> &[RustVariantIr] {
71        &self.variants
72    }
73
74    pub fn variants_mut(&mut self) -> &mut Vec<RustVariantIr> {
75        &mut self.variants
76    }
77
78    pub fn generics(&self) -> &RustGenericsIr {
79        &self.generics
80    }
81
82    pub fn generics_mut(&mut self) -> &mut RustGenericsIr {
83        &mut self.generics
84    }
85
86    pub fn where_clause(&self) -> &WhereClauseIr {
87        &self.where_clause
88    }
89
90    pub fn where_clause_mut(&mut self) -> &mut WhereClauseIr {
91        &mut self.where_clause
92    }
93
94    pub fn emission(&self) -> &TypeEmissionConfigIr {
95        &self.emission
96    }
97
98    pub fn emission_mut(&mut self) -> &mut TypeEmissionConfigIr {
99        &mut self.emission
100    }
101
102    pub fn set_kind(&mut self, kind: RustTypeKindIr) {
103        self.kind = kind;
104    }
105
106    pub fn push_field(&mut self, field: RustFieldIr) {
107        self.fields.push(field);
108    }
109
110    pub fn push_variant(&mut self, variant: RustVariantIr) {
111        self.variants.push(variant);
112    }
113}
114
115#[derive(Debug, Clone, PartialEq, Eq)]
116pub enum RustTypeKindIr {
117    Record,
118    Newtype,
119    Tuple,
120    Unit,
121    Enum,
122}
123
124#[derive(Debug, Clone, PartialEq, Eq, Default)]
125pub struct ContainerAttrsIr {
126    crate_path: Option<RustPathIr>,
127    rename_all: Option<RenameRuleIr>,
128    rename_all_fields: Option<RenameRuleIr>,
129    parse_ext: bool,
130    allow_unknown_fields: bool,
131    allow_unknown_extensions: bool,
132    parse_error: Option<RustPathIr>,
133    write_error: Option<RustPathIr>,
134    type_name: Option<String>,
135    non_exhaustive: bool,
136    proxy_target: Option<RustPathIr>,
137    opaque_target: Option<RustPathIr>,
138}
139
140impl ContainerAttrsIr {
141    #[allow(clippy::too_many_arguments)]
142    pub fn new(
143        crate_path: Option<RustPathIr>,
144        rename_all: Option<RenameRuleIr>,
145        rename_all_fields: Option<RenameRuleIr>,
146        parse_ext: bool,
147        allow_unknown_fields: bool,
148        allow_unknown_extensions: bool,
149        parse_error: Option<RustPathIr>,
150        write_error: Option<RustPathIr>,
151        type_name: Option<String>,
152        non_exhaustive: bool,
153        proxy_target: Option<RustPathIr>,
154        opaque_target: Option<RustPathIr>,
155    ) -> Self {
156        Self {
157            crate_path,
158            rename_all,
159            rename_all_fields,
160            parse_ext,
161            allow_unknown_fields,
162            allow_unknown_extensions,
163            parse_error,
164            write_error,
165            type_name,
166            non_exhaustive,
167            proxy_target,
168            opaque_target,
169        }
170    }
171
172    pub fn crate_path(&self) -> Option<&RustPathIr> {
173        self.crate_path.as_ref()
174    }
175
176    pub fn rename_all(&self) -> Option<RenameRuleIr> {
177        self.rename_all.clone()
178    }
179
180    pub fn rename_all_fields(&self) -> Option<RenameRuleIr> {
181        self.rename_all_fields.clone()
182    }
183
184    pub fn parse_ext(&self) -> bool {
185        self.parse_ext
186    }
187
188    pub fn allow_unknown_fields(&self) -> bool {
189        self.allow_unknown_fields
190    }
191
192    pub fn allow_unknown_extensions(&self) -> bool {
193        self.allow_unknown_extensions
194    }
195
196    pub fn parse_error(&self) -> Option<&RustPathIr> {
197        self.parse_error.as_ref()
198    }
199
200    pub fn write_error(&self) -> Option<&RustPathIr> {
201        self.write_error.as_ref()
202    }
203
204    pub fn type_name(&self) -> Option<&str> {
205        self.type_name.as_deref()
206    }
207
208    pub fn non_exhaustive(&self) -> bool {
209        self.non_exhaustive
210    }
211
212    pub fn proxy_target(&self) -> Option<&RustPathIr> {
213        self.proxy_target.as_ref()
214    }
215
216    pub fn opaque_target(&self) -> Option<&RustPathIr> {
217        self.opaque_target.as_ref()
218    }
219
220    pub fn proxy_mode(&self) -> Option<ProxyModeIr> {
221        match (&self.proxy_target, &self.opaque_target) {
222            (Some(target), None) => Some(ProxyModeIr::Transparent(target.clone())),
223            (None, Some(target)) => Some(ProxyModeIr::Opaque(target.clone())),
224            _ => None,
225        }
226    }
227
228    pub fn proxy_target_mut(&mut self) -> &mut Option<RustPathIr> {
229        &mut self.proxy_target
230    }
231
232    pub fn opaque_target_mut(&mut self) -> &mut Option<RustPathIr> {
233        &mut self.opaque_target
234    }
235
236    pub fn parse_ext_mut(&mut self) -> &mut bool {
237        &mut self.parse_ext
238    }
239}
240
241#[derive(Debug, Clone, PartialEq, Eq)]
242pub enum ProxyModeIr {
243    Transparent(RustPathIr),
244    Opaque(RustPathIr),
245}
246
247#[derive(Debug, Clone, PartialEq, Eq)]
248pub struct RustFieldIr {
249    rust_name: String,
250    wire_name: String,
251    mode: FieldModeIr,
252    source_attrs: FieldSourceAttrsIr,
253    ty: RustTypeExprIr,
254    default: DefaultValueIr,
255    via: Option<RustPathIr>,
256}
257
258impl RustFieldIr {
259    pub fn new(
260        rust_name: String,
261        wire_name: String,
262        mode: FieldModeIr,
263        source_attrs: FieldSourceAttrsIr,
264        ty: RustTypeExprIr,
265        default: DefaultValueIr,
266        via: Option<RustPathIr>,
267    ) -> Self {
268        Self {
269            rust_name,
270            wire_name,
271            mode,
272            source_attrs,
273            ty,
274            default,
275            via,
276        }
277    }
278
279    pub fn rust_name(&self) -> &str {
280        &self.rust_name
281    }
282
283    pub fn wire_name(&self) -> &str {
284        &self.wire_name
285    }
286
287    pub fn mode(&self) -> &FieldModeIr {
288        &self.mode
289    }
290
291    pub fn source_attrs(&self) -> &FieldSourceAttrsIr {
292        &self.source_attrs
293    }
294
295    pub fn ty(&self) -> &RustTypeExprIr {
296        &self.ty
297    }
298
299    pub fn default(&self) -> &DefaultValueIr {
300        &self.default
301    }
302
303    pub fn via(&self) -> Option<&RustPathIr> {
304        self.via.as_ref()
305    }
306}
307
308#[derive(Debug, Clone, PartialEq, Eq)]
309pub enum FieldModeIr {
310    Record,
311    Ext,
312    Flatten,
313    FlattenExt,
314}
315
316#[derive(Debug, Clone, PartialEq, Eq, Default)]
317pub struct FieldSourceAttrsIr {
318    pub ext: bool,
319    pub flatten: bool,
320    pub flatten_ext: bool,
321}
322
323#[derive(Debug, Clone, PartialEq, Eq, Default)]
324pub enum DefaultValueIr {
325    #[default]
326    None,
327    DefaultTrait,
328    Function(RustPathIr),
329}
330
331#[derive(Debug, Clone, PartialEq, Eq)]
332pub struct RustVariantIr {
333    rust_name: String,
334    wire_name: String,
335    allow_unknown_fields: bool,
336    shape: VariantShapeIr,
337}
338
339impl RustVariantIr {
340    pub fn new(
341        rust_name: String,
342        wire_name: String,
343        allow_unknown_fields: bool,
344        shape: VariantShapeIr,
345    ) -> Self {
346        Self {
347            rust_name,
348            wire_name,
349            allow_unknown_fields,
350            shape,
351        }
352    }
353
354    pub fn rust_name(&self) -> &str {
355        &self.rust_name
356    }
357
358    pub fn wire_name(&self) -> &str {
359        &self.wire_name
360    }
361
362    pub fn allow_unknown_fields(&self) -> bool {
363        self.allow_unknown_fields
364    }
365
366    pub fn shape(&self) -> &VariantShapeIr {
367        &self.shape
368    }
369}
370
371#[derive(Debug, Clone, PartialEq, Eq)]
372pub enum VariantShapeIr {
373    Unit,
374    Newtype {
375        ty: RustTypeExprIr,
376        via: Option<RustPathIr>,
377    },
378    Tuple(Vec<TupleElementIr>),
379    Record(Vec<RustFieldIr>),
380}
381
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub struct TupleElementIr {
384    pub ty: RustTypeExprIr,
385    pub via: Option<RustPathIr>,
386}
387
388#[derive(Debug, Clone, PartialEq, Eq)]
389pub enum RustTypeExprIr {
390    Primitive(PrimitiveRustTypeIr),
391    Named(QualifiedTypeName),
392    Path(RustPathIr),
393    GenericParam(String),
394    Option(Box<RustTypeExprIr>),
395    Vec(Box<RustTypeExprIr>),
396    Map {
397        key: Box<RustTypeExprIr>,
398        value: Box<RustTypeExprIr>,
399        impl_type: MapImplTypeIr,
400    },
401    Tuple(Vec<RustTypeExprIr>),
402    Result {
403        ok: Box<RustTypeExprIr>,
404        err: Box<RustTypeExprIr>,
405    },
406    Wrapper {
407        inner: Box<RustTypeExprIr>,
408        wrapper: WrapperKindIr,
409    },
410}
411
412#[derive(Debug, Clone, PartialEq, Eq)]
413pub enum PrimitiveRustTypeIr {
414    String,
415    Bool,
416    Unit,
417    Text,
418    Any,
419    I8,
420    I16,
421    I32,
422    I64,
423    I128,
424    Isize,
425    U8,
426    U16,
427    U32,
428    U64,
429    U128,
430    Usize,
431    F32,
432    F64,
433}
434
435#[derive(Debug, Clone, PartialEq, Eq)]
436pub enum MapImplTypeIr {
437    HashMap,
438    BTreeMap,
439    IndexMap,
440}
441
442#[derive(Debug, Clone, PartialEq, Eq)]
443pub enum WrapperKindIr {
444    Box,
445    Rc,
446    Arc,
447}
448
449#[derive(Debug, Clone, PartialEq, Eq)]
450pub enum RenameRuleIr {
451    Lower,
452    Upper,
453    Pascal,
454    Camel,
455    Snake,
456    ScreamingSnake,
457    Kebab,
458    Cobol,
459}
460
461#[derive(Debug, Clone, PartialEq, Eq, Default)]
462pub struct RustGenericsIr {
463    pub type_params: Vec<TypeParamIr>,
464    pub lifetime_params: Vec<LifetimeParamIr>,
465    pub const_params: Vec<ConstParamIr>,
466}
467
468#[derive(Debug, Clone, PartialEq, Eq)]
469pub struct TypeParamIr {
470    pub name: String,
471    pub bounds: Vec<String>,
472}
473
474#[derive(Debug, Clone, PartialEq, Eq)]
475pub struct LifetimeParamIr {
476    pub name: String,
477    pub bounds: Vec<String>,
478}
479
480#[derive(Debug, Clone, PartialEq, Eq)]
481pub struct ConstParamIr {
482    pub name: String,
483    pub ty: String,
484}
485
486#[derive(Debug, Clone, PartialEq, Eq, Default)]
487pub struct WhereClauseIr {
488    pub predicates: Vec<String>,
489}