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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// This file contains code adapted from the wasm-utils-rs project
// (https://github.com/ryangoree/wasm-utils-rs).
//
// Original Copyright 2024 DELV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::ts_type::{ToTsType, TsType};
use heck::{ToLowerCamelCase, ToPascalCase};
use quote::{format_ident, quote};
use syn::{
Error, Fields, FieldsNamed, Ident, ItemStruct, Meta, Token,
parse::{Parse, ParseStream},
punctuated::Punctuated,
};
pub(crate) struct TsArgs {
name: Option<Ident>,
extends: Option<Punctuated<Ident, Token![,]>>,
rename_all: Option<String>,
}
impl Parse for TsArgs {
fn parse(input: ParseStream) -> Result<Self, Error> {
let mut args = TsArgs {
name: None,
extends: None,
rename_all: None,
};
while !input.is_empty() {
let key = input.parse::<Ident>()?;
input.parse::<Token![=]>()?;
match key.to_string().as_str() {
"name" => args.name = Some(input.parse()?),
"extends" => args.extends = Some(input.parse_terminated(Ident::parse, Token![,])?),
"rename_all" => {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}) = input.parse()?
{
args.rename_all = Some(lit_str.value());
} else {
return Err(Error::new(
key.span(),
"Expected string literal for `rename_all`",
));
}
}
_ => {
return Err(Error::new(
key.span(),
format!("Unknown argument: `{}`", key),
));
}
}
if !input.is_empty() {
input.parse::<Token![,]>()?;
}
}
Ok(args)
}
}
/// Generate TypeScript interface bindings from a Rust struct.
pub fn ts_internal(args: TsArgs, item: ItemStruct) -> proc_macro2::TokenStream {
// Ensure the input is a struct with named fields
let (struct_name, fields) = match &item {
ItemStruct {
ident,
fields: Fields::Named(fields),
..
} => (ident, fields),
_ => {
return quote! {
compile_error!("The `ts` attribute can only be used on structs with named fields.");
};
}
};
let binding_name = match args.name {
Some(name) => format_ident!("{}", name),
None => format_ident!("I{}", struct_name),
};
let ts_interface_name = struct_name.to_string();
let mut ts_fields = vec![];
let mut field_conversions = vec![];
let mut field_getters = vec![];
let mut field_setters = vec![];
let mut processed_fields = vec![];
// Iterate over the fields of the struct to generate entries for the
// TypeScript interface and the field conversions
for field in &fields.named {
let field_type = &field.ty;
let field_name = field.ident.as_ref().unwrap();
let mut field = field.clone();
let mut doc_lines = vec![];
let mut is_optional = false;
// Convert the Rust field name to a TypeScript field name
let mut ts_field_name = match args.rename_all.as_deref() {
Some("none") => format_ident!("{}", field_name),
_ => format_ident!("{}", field_name.to_string().to_lower_camel_case()),
};
// Convert the Rust type to a TypeScript type
let mut ts_field_type = match field_type.to_ts_type() {
Ok(ts_type) => {
// if the type is `undefined` or unioned with `undefined`, make
// it optional
let undefined = TsType::Base("undefined".to_string());
if ts_type == undefined || ts_type.is_union_with(&undefined) {
is_optional = true;
}
ts_type
}
Err(err) => {
let msg = format!("{}", err);
return quote! { compile_error!(#msg); };
}
};
// Iterate over the attributes of the field to extract the `ts`
// attribute and doc comments
let mut i = 0;
while i < field.attrs.len() {
let attr = &field.attrs[i];
// Collect doc comments
if attr.path().is_ident("doc") {
if let Meta::NameValue(syn::MetaNameValue {
value:
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}),
..
}) = &attr.meta
{
doc_lines.push(lit_str.value());
}
field.attrs.remove(i);
continue;
}
if !attr.path().is_ident("ts") {
i += 1;
continue;
}
// Parse the `ts` attribute arguments
match &attr.meta {
Meta::List(list) => {
let result =
list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);
match result {
Ok(nested) => {
for arg in nested {
if let Meta::NameValue(nv) = arg {
let key = match nv.path.get_ident() {
Some(ident) => ident.to_string(),
None => {
let msg = "Expected an identifier for the key";
return quote! { compile_error!(#msg); };
}
};
match key.as_str() {
"name" => {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}) = nv.value
{
ts_field_name =
format_ident!("{}", lit_str.value());
} else {
let msg = format!(
"`name` for field `{field_name}` must be a string literal."
);
return quote! { compile_error!(#msg); };
}
}
"type" => {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}) = nv.value
{
match TsType::from_ts_str(lit_str.value().as_str())
{
Ok(ts_type) => ts_field_type = ts_type,
Err(err) => {
let msg = format!("{}", err);
return quote! { compile_error!(#msg); };
}
}
} else {
let msg = format!(
"`type` for field `{field_name}` must be a string literal."
);
return quote! { compile_error!(#msg); };
}
}
"optional" => {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Bool(bool_lit),
..
}) = nv.value
{
is_optional = bool_lit.value;
} else {
let msg = format!(
"`optional` for field `{field_name}` must be a boolean literal."
);
return quote! { compile_error!(#msg); };
}
}
unknown => {
let msg = format!(
r#"Unknown argument for field `{field}`: `{attr}`. Options are:
- type: The TypeScript type of the field
- name: The name of the field in the TypeScript interface
- optional: Whether the field is optional in TypeScript"#,
field = field_name,
attr = unknown
);
return quote! { compile_error!(#msg); };
}
}
} else {
let msg = format!(
"`ts` attribute for field `{}` must be a list of name-value pairs, e.g. `#[ts(type = \"{}\")]`.",
field_name,
field_name.to_string().to_pascal_case()
);
return quote! { compile_error!(#msg); };
}
}
}
Err(err) => {
let msg = format!("{}", err);
return quote! { compile_error!(#msg); };
}
}
}
_ => {
let msg = format!(
"`ts` attribute for field `{}` must be a list, e.g. `#[ts(type = \"Js{}\")]`.",
field_name,
field_name.to_string().to_pascal_case(),
);
return quote! { compile_error!(#msg); };
}
}
// Remove the attribute from the field
field.attrs.remove(i);
}
// Add an entry for the TypeScript interface
let optional_char = match is_optional {
true => "?",
false => "",
};
let ts_doc_comment = match doc_lines.is_empty() {
true => "".to_string(),
false => format!("/**\n *{}\n */\n ", doc_lines.join("\n *")),
};
ts_fields.push(format!(
"{ts_doc_comment}{ts_field_name}{optional_char}: {ts_field_type};"
));
// Add a getter for the field to the binding
let rs_doc_comment = doc_lines.iter().map(|line| quote! { #[doc = #line] });
field_getters.push(quote! {
#(#rs_doc_comment)*
#[::wasm_bindgen::prelude::wasm_bindgen(method, getter = #ts_field_name)]
pub fn #field_name(this: &#binding_name) -> #field_type;
});
// Add an entry for the `From` implementation
field_conversions.push(quote! {
#field_name: js_value.#field_name()
});
// Add a setter for the `Into<JsValue>` implementation
let ts_field_name_str = ts_field_name.to_string();
field_setters.push(quote! {
::js_sys::Reflect::set(
&obj,
&::wasm_bindgen::JsValue::from_str(#ts_field_name_str),
&value.#field_name.into()
).unwrap();
});
// Add the processed field to the struct
processed_fields.push(field);
}
// Generate the TypeScript interface definition
let const_name = format_ident!("{}__TS_DEF", struct_name.to_string().to_uppercase());
let (extends_clause, extends) = match args.extends {
Some(extends) => (
format!(
" extends {}",
extends
.iter()
.map(|base| base.to_string())
.collect::<Vec<String>>()
.join(", ")
),
extends.into_iter().collect(),
),
None => ("".to_string(), vec![]),
};
let ts_definition = format!(
r#"export interface {ts_interface_name}{extends_clause} {{
{}
}}"#,
ts_fields.join("\n ")
);
// Prep the expanded struct with the processed attributes removed
let processed_struct = ItemStruct {
fields: Fields::Named(FieldsNamed {
named: Punctuated::from_iter(processed_fields),
brace_token: fields.brace_token,
}),
..item.clone()
};
let expanded = quote! {
#[::wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
const #const_name: &'static str = #ts_definition;
#[::wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[derive(Clone)]
#[wasm_bindgen(typescript_type = #ts_interface_name, #(extends = #extends),*)]
pub type #binding_name;
#(#field_getters)*
}
impl ::wasm_bindgen::describe::WasmDescribe for #struct_name {
fn describe() {
<::wasm_bindgen::JsValue as ::wasm_bindgen::describe::WasmDescribe>::describe()
}
}
impl ::wasm_bindgen::convert::FromWasmAbi for #struct_name {
type Abi = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::FromWasmAbi>::Abi;
#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
let js_value = unsafe { <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::FromWasmAbi>::from_abi(js) };
::std::convert::Into::<Self>::into(js_value)
}
}
impl ::wasm_bindgen::convert::IntoWasmAbi for #struct_name {
type Abi = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::IntoWasmAbi>::Abi;
#[inline]
fn into_abi(self) -> Self::Abi {
let js_value: ::wasm_bindgen::JsValue = ::std::convert::Into::<::wasm_bindgen::JsValue>::into(self);
js_value.into_abi()
}
}
impl ::wasm_bindgen::convert::OptionFromWasmAbi for #struct_name {
#[inline]
fn is_none(abi: &Self::Abi) -> bool {
<::wasm_bindgen::JsValue as ::wasm_bindgen::convert::OptionFromWasmAbi>::is_none(abi)
}
}
impl ::wasm_bindgen::convert::OptionIntoWasmAbi for #struct_name {
#[inline]
fn none() -> Self::Abi {
<::wasm_bindgen::JsValue as ::wasm_bindgen::convert::OptionIntoWasmAbi>::none()
}
}
impl From<#binding_name> for #struct_name {
/// Convert the JS binding into the Rust struct
fn from(js_value: #binding_name) -> Self {
js_value.parse()
}
}
impl From<::wasm_bindgen::JsValue> for #struct_name {
fn from(js_value: ::wasm_bindgen::JsValue) -> Self {
use ::wasm_bindgen::JsCast;
js_value.unchecked_into::<#binding_name>().parse()
}
}
impl From<#struct_name> for ::wasm_bindgen::JsValue {
fn from(value: #struct_name) -> Self {
let obj = ::js_sys::Object::new();
#( #field_setters )*
::wasm_bindgen::JsValue::from(obj)
}
}
impl From<#struct_name> for #binding_name {
fn from(value: #struct_name) -> Self {
use ::wasm_bindgen::JsCast;
::wasm_bindgen::JsValue::from(value).unchecked_into::<#binding_name>()
}
}
impl #binding_name {
/// Parse the JS binding into its Rust struct
pub fn parse(&self) -> #struct_name {
let js_value = self;
#struct_name {
#(#field_conversions),*
}
}
}
#[allow(unused)]
#[doc = "### Typescript Binding"]
#[doc = ""]
#[doc = "Below is the TypeScript definition for the binding generated by the `ts` attribute."]
#[doc = ""]
#[doc = "```ts"]
#[doc = #ts_definition]
#[doc = "```"]
#[doc = ""]
#processed_struct
};
expanded
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ts_args_parse() {
let attr = quote! { name = MyStruct, rename_all = "none", extends = Base1, Base2 };
let args: TsArgs = syn::parse2(attr).unwrap();
assert_eq!(args.name.unwrap().to_string(), "MyStruct");
assert_eq!(args.extends.unwrap().len(), 2);
assert_eq!(args.rename_all.unwrap(), "none");
}
#[test]
fn test_ts_enum_field() {
let attr = quote! {};
let input = quote! {
pub struct User {
pub status: Status,
}
};
let args: TsArgs = syn::parse2(attr).unwrap();
let item: ItemStruct = syn::parse2(input).unwrap();
let result = ts_internal(args, item);
let result_str = result.to_string();
assert!(result_str.contains("export interface User"));
assert!(result_str.contains("status: Status;"));
}
}