1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::ir::IrType::{EnumRef, StructRef};
use crate::ir::*;
use convert_case::{Case, Casing};
#[derive(Debug, Clone)]
pub struct IrTypeEnumRef {
pub name: String,
pub is_struct: bool,
}
impl IrTypeEnumRef {
pub fn get<'a>(&self, file: &'a IrFile) -> &'a IrEnum {
&file.enum_pool[&self.name]
}
}
impl IrTypeTrait for IrTypeEnumRef {
fn visit_children_types<F: FnMut(&IrType) -> bool>(&self, f: &mut F, ir_file: &IrFile) {
let enu = self.get(ir_file);
for variant in enu.variants() {
if let IrVariantKind::Struct(st) = &variant.kind {
st.fields
.iter()
.for_each(|field| field.ty.visit_types(f, ir_file));
}
}
}
fn safe_ident(&self) -> String {
self.dart_api_type().to_case(Case::Snake)
}
fn dart_api_type(&self) -> String {
self.name.to_string()
}
fn dart_wire_type(&self) -> String {
if self.is_struct {
self.rust_wire_type()
} else {
"int".to_owned()
}
}
fn rust_api_type(&self) -> String {
self.name.to_string()
}
fn rust_wire_type(&self) -> String {
if self.is_struct {
format!("wire_{}", self.name)
} else {
"i32".to_owned()
}
}
}
#[derive(Debug, Clone)]
pub struct IrEnum {
pub name: String,
pub wrapper_name: Option<String>,
pub path: Vec<String>,
pub comments: Vec<IrComment>,
_variants: Vec<IrVariant>,
_is_struct: bool,
}
impl IrEnum {
pub fn new(
name: String,
wrapper_name: Option<String>,
path: Vec<String>,
comments: Vec<IrComment>,
mut variants: Vec<IrVariant>,
) -> Self {
fn wrap_box(ty: IrType) -> IrType {
match ty {
StructRef(_)
| EnumRef(IrTypeEnumRef {
is_struct: true, ..
}) => IrType::Boxed(IrTypeBoxed {
exist_in_real_api: false,
inner: Box::new(ty),
}),
_ => ty,
}
}
let _is_struct = variants
.iter()
.any(|variant| !matches!(variant.kind, IrVariantKind::Value));
if _is_struct {
variants = variants
.into_iter()
.map(|variant| IrVariant {
kind: match variant.kind {
IrVariantKind::Struct(st) => IrVariantKind::Struct(IrStruct {
fields: st
.fields
.into_iter()
.map(|field| IrField {
ty: wrap_box(field.ty),
..field
})
.collect(),
..st
}),
_ => variant.kind,
},
..variant
})
.collect::<Vec<_>>();
}
Self {
name,
wrapper_name,
path,
comments,
_variants: variants,
_is_struct,
}
}
pub fn variants(&self) -> &[IrVariant] {
&self._variants
}
pub fn is_struct(&self) -> bool {
self._is_struct
}
}
#[derive(Debug, Clone)]
pub struct IrVariant {
pub name: IrIdent,
pub comments: Vec<IrComment>,
pub kind: IrVariantKind,
}
#[derive(Debug, Clone)]
pub enum IrVariantKind {
Value,
Struct(IrStruct),
}