tsify_macros/
lib.rs

1mod attrs;
2mod comments;
3mod container;
4mod decl;
5mod derive;
6mod error_tracker;
7mod parser;
8mod type_alias;
9mod typescript;
10mod wasm_bindgen;
11
12use syn::{parse_macro_input, DeriveInput};
13
14fn declare_impl(
15    args: proc_macro2::TokenStream,
16    item: syn::Item,
17) -> syn::Result<proc_macro2::TokenStream> {
18    match item {
19        syn::Item::Type(item) => type_alias::expand(item),
20        syn::Item::Enum(item) => derive::expand_by_attr(args, item.into()),
21        syn::Item::Struct(item) => derive::expand_by_attr(args, item.into()),
22        _ => Err(syn::Error::new_spanned(
23            args,
24            "#[declare] can only be applied to a struct, enum, or type alias.",
25        )),
26    }
27}
28
29/// The `declare` macro, used in `#[declare]` annotations.
30#[proc_macro_attribute]
31pub fn declare(
32    args: proc_macro::TokenStream,
33    item: proc_macro::TokenStream,
34) -> proc_macro::TokenStream {
35    let item: syn::Item = parse_macro_input!(item);
36    let args = proc_macro2::TokenStream::from(args);
37
38    declare_impl(args, item)
39        .unwrap_or_else(syn::Error::into_compile_error)
40        .into()
41}
42
43/// The `Tsify` derive macro, used in `#[derive(Tsify, ...)]` annotations.
44#[proc_macro_derive(Tsify, attributes(tsify, serde))]
45pub fn derive_tsify(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
46    let item: DeriveInput = parse_macro_input!(input);
47
48    derive::expand(item)
49        .unwrap_or_else(syn::Error::into_compile_error)
50        .into()
51}