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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
mod builder;
pub(crate) mod config;
use std::path::{Path, PathBuf};
use anyhow::Result;
pub use builder::configure;
use heck::{AsPascalCase, AsSnakeCase};
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, Expr, Lit, LitStr};
use wick_config::FlowOperation;
use wick_interface_types::{EnumSignature, EnumVariant, StructSignature, TypeDefinition};
fn snake(s: &str) -> String {
AsSnakeCase(s).to_string()
}
fn pascal(s: &str) -> String {
AsPascalCase(s).to_string()
}
fn op_wrapper_name(op: &FlowOperation) -> String {
snake(&format!("{}_wrapper", op.name))
}
fn op_outputs_name(op: &FlowOperation) -> String {
format!("Op{}Outputs", pascal(&op.name))
}
fn structdef_name(ty: &StructSignature) -> String {
pascal(&ty.name)
}
fn enumdef_name(ty: &EnumSignature) -> String {
pascal(&ty.name)
}
fn enumvariant_name(ty: &EnumVariant) -> String {
pascal(&ty.name)
}
fn gen_register_channels<'a>(
config: &config::Config,
component: &Ident,
op: impl Iterator<Item = &'a FlowOperation>,
) -> Vec<TokenStream> {
op.map(|op| {
let name = Ident::new(&op_wrapper_name(op), Span::call_site());
let string = &op.name;
quote! {
guest::register_request_channel("wick", #string, #component::#name);
}
})
.collect()
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Direction {
In,
Out,
}
fn expand_type(config: &config::Config, dir: Direction, ty: &wick_interface_types::TypeSignature) -> TokenStream {
if config.raw && dir != Direction::Out {
return quote! { wick_component::packet::Packet };
}
match ty {
wick_interface_types::TypeSignature::Bool => quote! { bool },
wick_interface_types::TypeSignature::U8 => quote! { u8 },
wick_interface_types::TypeSignature::U16 => quote! { u16 },
wick_interface_types::TypeSignature::U32 => quote! { u32 },
wick_interface_types::TypeSignature::U64 => quote! { u64 },
wick_interface_types::TypeSignature::I8 => quote! { i8 },
wick_interface_types::TypeSignature::I16 => quote! { i16 },
wick_interface_types::TypeSignature::I32 => quote! { i32 },
wick_interface_types::TypeSignature::I64 => quote! { i64 },
wick_interface_types::TypeSignature::F32 => quote! { f32 },
wick_interface_types::TypeSignature::F64 => quote! { f64 },
wick_interface_types::TypeSignature::String => quote! { String },
wick_interface_types::TypeSignature::List { ty } => {
let ty = expand_type(config, dir, ty);
quote! { Vec<#ty> }
}
wick_interface_types::TypeSignature::Bytes => {
quote! {bytes::Bytes}
}
wick_interface_types::TypeSignature::Custom(name) => {
let ty = Ident::new(name, Span::call_site());
quote! {#ty}
}
wick_interface_types::TypeSignature::Optional { ty } => {
let ty = expand_type(config, dir, ty);
quote! { Option<#ty> }
}
wick_interface_types::TypeSignature::Map { key, value } => {
let key = expand_type(config, dir, key);
let value = expand_type(config, dir, value);
quote! { std::collections::HashMap<#key,#value> }
}
wick_interface_types::TypeSignature::Link { schemas } => quote! {wick_component::packet::CollectionLink},
wick_interface_types::TypeSignature::Datetime => todo!("implement datetime in new codegen"),
wick_interface_types::TypeSignature::Value => todo!("implement value in new codegen"),
wick_interface_types::TypeSignature::Internal(_) => todo!("implement internal types in new codegen"),
wick_interface_types::TypeSignature::Ref { reference } => todo!("implement ref in new codegen"),
wick_interface_types::TypeSignature::Stream { ty } => {
let ty = expand_type(config, dir, ty);
quote! { WickStream<#ty> }
}
wick_interface_types::TypeSignature::Struct => todo!("implement struct in new codegen"),
wick_interface_types::TypeSignature::AnonymousStruct(_) => todo!("implement anonymous struct in new codegen"),
}
}
fn gen_types<'a>(config: &config::Config, ty: impl Iterator<Item = &'a TypeDefinition>) -> Vec<TokenStream> {
ty.map(|v| gen_type(config, v))
.collect::<Vec<_>>()
.into_iter()
.collect()
}
fn gen_type(config: &config::Config, ty: &TypeDefinition) -> TokenStream {
match ty {
TypeDefinition::Enum(ty) => gen_enum(config, ty),
TypeDefinition::Struct(ty) => gen_struct(config, ty),
}
}
fn gen_enum(config: &config::Config, ty: &EnumSignature) -> TokenStream {
let name = Ident::new(&enumdef_name(ty), Span::call_site());
let variants = ty
.variants
.iter()
.map(|v| {
let name = Ident::new(&enumvariant_name(v), Span::call_site());
quote! {#name}
})
.collect::<Vec<_>>();
let display_match_arms = ty
.variants
.iter()
.map(|v| {
let identname = Ident::new(&enumvariant_name(v), Span::call_site());
let name = v.name.clone();
quote! {Self::#identname => f.write_str(#name)}
})
.collect::<Vec<_>>();
let value_match_arms = ty
.variants
.iter()
.map(|v| {
let identname = Ident::new(&enumvariant_name(v), Span::call_site());
let name = v.value.as_ref().map_or_else(|| quote! {None}, |v| quote! { Some(#v) });
quote! {Self::#identname => #name}
})
.collect::<Vec<_>>();
let fromstr_match_arms = ty
.variants
.iter()
.map(|v| {
let identname = Ident::new(&enumvariant_name(v), Span::call_site());
let name = v.name.clone();
quote! {#name => Ok(Self::#identname)}
})
.collect::<Vec<_>>();
quote! {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub enum #name {
#(#variants,)*
}
impl #name {
#[allow(unused)]
pub fn value(&self) -> Option<&'static str> {
match self {
#(#value_match_arms,)*
}
}
}
impl std::str::FromStr for #name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#(#fromstr_match_arms,)*
_ => Err(s.to_owned())
}
}
}
impl std::fmt::Display for #name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#(#display_match_arms,)*
}
}
}
}
}
fn gen_struct(config: &config::Config, ty: &StructSignature) -> TokenStream {
let name = Ident::new(&structdef_name(ty), Span::call_site());
let fields = ty
.fields
.iter()
.map(|f| {
let name = Ident::new(&snake(&f.name), Span::call_site());
let ty = expand_type(config, Direction::In, &f.ty);
quote! {pub #name: #ty}
})
.collect::<Vec<_>>();
quote! {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct #name {
#(#fields,)*
}
}
}
fn gen_wrapper_fns<'a>(
config: &config::Config,
component: &Ident,
op: impl Iterator<Item = &'a FlowOperation>,
) -> Vec<TokenStream> {
op.map(|op| gen_wrapper_fn(config, component, op))
.collect::<Vec<_>>()
.into_iter()
.collect()
}
fn gen_wrapper_fn(config: &config::Config, component: &Ident, op: &FlowOperation) -> TokenStream {
let impl_name = Ident::new(&snake(&op.name), Span::call_site());
let wrapper_name = Ident::new(&op_wrapper_name(op), Span::call_site());
let string = &snake(&op.name);
let input_pairs = op
.inputs
.iter()
.map(|i| {
let port_name = &i.name;
let port_type = expand_type(config, Direction::In, &i.ty);
quote! {(#port_name, #port_type)}
})
.collect::<Vec<_>>();
let inputs = op
.inputs
.iter()
.map(|i| Ident::new(&snake(&i.name), Span::call_site()))
.collect::<Vec<_>>();
let outputs_name = Ident::new(&op_outputs_name(op), Span::call_site());
let sanitized_input_names = if inputs.len() == 1 {
quote! {#(#inputs)*}
} else {
quote! {(#(#inputs,)*)}
};
let raw = if config.raw {
quote! {raw:true}
} else {
quote! {raw:false}
};
quote! {
fn #wrapper_name(mut input: FluxReceiver<Payload, PayloadError>) -> std::result::Result<FluxReceiver<RawPayload, PayloadError>,Box<dyn std::error::Error + Send + Sync>> {
let (channel, rx) = FluxChannel::<RawPayload, PayloadError>::new_parts();
let outputs = #outputs_name::new(channel.clone());
spawn(async move {
let #sanitized_input_names = wick_component::payload_fan_out!(input, #raw, [#(#input_pairs,)*]);
if let Err(e) = #component::#impl_name(#(#inputs,)* outputs).await {
let _ = channel.send_result(
wick_component::packet::Packet::component_error(e.to_string()).into(),
);
}
});
Ok(rx)
}
}
}
fn gen_trait_fns<'a>(
config: &config::Config,
component: &Ident,
op: impl Iterator<Item = &'a FlowOperation>,
) -> Vec<TokenStream> {
op.map(|op| gen_trait_signature(config, component, op))
.collect::<Vec<_>>()
.into_iter()
.collect()
}
fn gen_trait_signature(config: &config::Config, component: &Ident, op: &FlowOperation) -> TokenStream {
let outputs_name = Ident::new(&op_outputs_name(op), Span::call_site());
let trait_name = Ident::new(&format!("Op{}", &pascal(&op.name)), Span::call_site());
let impl_name = Ident::new(&snake(&op.name), Span::call_site());
let input_pairs = op
.inputs
.iter()
.map(|i| {
let port_name = &i.name;
let port_type = expand_type(config, Direction::In, &i.ty);
quote! {(#port_name, #port_type)}
})
.collect::<Vec<_>>();
let output_ports = op
.outputs
.iter()
.map(|i| {
let port_name = &i.name;
let port_field_name = Ident::new(&snake(&i.name), Span::call_site());
let port_type = expand_type(config, Direction::Out, &i.ty);
quote! {pub(crate) #port_field_name: wick_component::packet::Output<#port_type>}
})
.collect::<Vec<_>>();
let output_ports_new = op
.outputs
.iter()
.map(|i| {
let port_name = &i.name;
let port_field_name = Ident::new(&snake(&i.name), Span::call_site());
quote! {#port_field_name: wick_component::packet::Output::new(#port_name, channel.clone())}
})
.collect::<Vec<_>>();
let inputs = op
.inputs
.iter()
.map(|i| {
let port_name = Ident::new(&snake(&i.name), Span::call_site());
let port_type = expand_type(config, Direction::In, &i.ty);
quote! {#port_name: WickStream<#port_type>}
})
.collect::<Vec<_>>();
quote! {
pub struct #outputs_name {
#[allow(unused)]
#(#output_ports,)*
}
impl #outputs_name {
pub fn new(channel: FluxChannel<RawPayload, PayloadError>) -> Self {
Self {
#(#output_ports_new,)*
}
}
}
#[cfg_attr(target_family = "wasm",async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
pub trait #trait_name {
#[allow(unused)]
async fn #impl_name(#(#inputs),*, outputs: #outputs_name) -> Result<()> {unimplemented!()}
}
}
}
fn codegen(config: &config::Config) -> Result<String> {
let component = wick_config::ComponentConfiguration::load_from_file(&config.spec).unwrap();
let component_name = Ident::new("Component", Span::call_site());
let register_stmts = gen_register_channels(config, &component_name, component.operations().values());
let wrapper_fns = gen_wrapper_fns(config, &component_name, component.operations().values());
let trait_defs = gen_trait_fns(config, &component_name, component.operations().values());
let typedefs = gen_types(config, component.types().iter());
let expanded = quote! {
#[allow(unused)]
use guest::*;
use wasmrs_guest as guest;
#[allow(unused)]
pub(crate) type WickStream<T> = FluxReceiver<T, wick_component::anyhow::Error>;
pub use wick_component::anyhow::Result;
#[no_mangle]
extern "C" fn __wasmrs_init(guest_buffer_size: u32, host_buffer_size: u32, max_host_frame_len: u32) {
guest::init(guest_buffer_size, host_buffer_size, max_host_frame_len);
#(#register_stmts)*
}
#( #typedefs )*
#( #trait_defs )*
#[derive(Default, Clone)]
pub struct #component_name;
impl #component_name {
#( #wrapper_fns )*
}
};
Ok(expanded.to_string())
}
#[allow(clippy::needless_pass_by_value)]
pub fn build(config: config::Config) -> Result<()> {
let src = codegen(&config)?;
std::fs::create_dir_all(&config.out_dir)?;
let target = config.out_dir.join("mod.rs");
println!("Writing to {}", target.display());
std::fs::write(target, src)?;
Ok(())
}
#[cfg(test)]
mod test {
use anyhow::Result;
use super::*;
use crate::Config;
#[test]
fn test_build() -> Result<()> {
let config = Config::new().spec("./tests/testdata/component.yaml");
let src = codegen(&config)?;
assert!(src.contains("pub struct Component"));
Ok(())
}
}