1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Component {
9 pub name: String,
11
12 pub modules: Vec<CoreModule>,
14
15 pub components: Vec<Component>,
17
18 pub instances: Vec<ComponentInstance>,
20
21 pub interfaces: HashMap<String, WITInterface>,
23
24 pub imports: Vec<Import>,
26
27 pub exports: Vec<Export>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct CoreModule {
34 pub id: String,
36
37 pub binary: Vec<u8>,
39
40 pub functions: Vec<Function>,
42
43 pub memories: Vec<Memory>,
45
46 pub tables: Vec<Table>,
48
49 pub globals: Vec<Global>,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct Function {
56 pub index: u32,
58
59 pub name: Option<String>,
61
62 pub signature: FunctionSignature,
64
65 pub exported: bool,
67
68 pub imported: bool,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct FunctionSignature {
75 pub params: Vec<ValueType>,
77
78 pub results: Vec<ValueType>,
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
84pub enum ValueType {
85 I32,
86 I64,
87 F32,
88 F64,
89 V128,
90 FuncRef,
91 ExternRef,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Memory {
97 pub index: u32,
99
100 pub initial: u32,
102
103 pub maximum: Option<u32>,
105
106 pub shared: bool,
108
109 pub memory64: bool,
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct Table {
116 pub index: u32,
118
119 pub element_type: ValueType,
121
122 pub initial: u32,
124
125 pub maximum: Option<u32>,
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct Global {
132 pub index: u32,
134
135 pub value_type: ValueType,
137
138 pub mutable: bool,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct ComponentInstance {
145 pub id: String,
147
148 pub component: String,
150
151 #[serde(default)]
166 pub recursive_reentrance: bool,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct WITInterface {
172 pub name: String,
174
175 pub functions: Vec<WITFunction>,
177
178 pub types: Vec<WITType>,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
184pub struct WITFunction {
185 pub name: String,
187
188 pub params: Vec<(String, WITType)>,
190
191 pub results: Vec<WITType>,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
197pub enum WITType {
198 Bool,
199 U8,
200 U16,
201 U32,
202 U64,
203 S8,
204 S16,
205 S32,
206 S64,
207 F32,
208 F64,
209 String,
210 List(Box<WITType>),
211 Record(Vec<(String, WITType)>),
212 Variant(Vec<(String, Option<WITType>)>),
213 Enum(Vec<String>),
214 Option(Box<WITType>),
215 Result { ok: Box<WITType>, err: Box<WITType> },
216}
217
218#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct Import {
221 pub name: String,
223
224 pub kind: ImportKind,
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize)]
230pub enum ImportKind {
231 Function(FunctionSignature),
232 Memory(Memory),
233 Table(Table),
234 Global(Global),
235}
236
237#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct Export {
240 pub name: String,
242
243 pub kind: ExportKind,
245}
246
247#[derive(Debug, Clone, Serialize, Deserialize)]
249pub enum ExportKind {
250 Function(u32),
251 Memory(u32),
252 Table(u32),
253 Global(u32),
254}
255
256impl Component {
257 pub fn new(name: String) -> Self {
259 Self {
260 name,
261 modules: Vec::new(),
262 components: Vec::new(),
263 instances: Vec::new(),
264 interfaces: HashMap::new(),
265 imports: Vec::new(),
266 exports: Vec::new(),
267 }
268 }
269
270 pub fn total_memories(&self) -> usize {
272 self.modules.iter().map(|m| m.memories.len()).sum()
273 }
274
275 pub fn total_memory_size(&self) -> u64 {
277 self.modules
278 .iter()
279 .flat_map(|m| &m.memories)
280 .map(|mem| mem.initial as u64 * 65536) .sum()
282 }
283}