tsify_macros/
lib.rs

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