Skip to main content

fusionamm_macros/
lib.rs

1//
2// Copyright (c) Cryptic Dot
3//
4// Modification based on Orca Whirlpools (https://github.com/orca-so/whirlpools),
5// originally licensed under the Apache License, Version 2.0, prior to February 26, 2025.
6//
7// Modifications licensed under FusionAMM SDK Source-Available License v1.0
8// See the LICENSE file in the project root for license information.
9//
10
11mod wasm_const;
12mod wasm_enum;
13mod wasm_fn;
14mod wasm_struct;
15
16use proc_macro::TokenStream;
17use syn::{parse::Nothing, parse2, Item, Result};
18
19#[proc_macro_attribute]
20pub fn wasm_expose(attr: TokenStream, item: TokenStream) -> TokenStream {
21    match wasm_expose_impl(attr, item) {
22        Ok(expanded) => expanded,
23        Err(err) => err.to_compile_error().into(),
24    }
25}
26
27fn wasm_expose_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
28    let attr: Nothing = parse2(attr.into())?;
29    let item: Item = parse2(item.into())?;
30
31    let result = match item {
32        Item::Struct(s) => crate::wasm_struct::wasm_struct_impl(s, attr),
33        Item::Enum(e) => crate::wasm_enum::wasm_enum_impl(e, attr),
34        Item::Const(c) => crate::wasm_const::wasm_const_impl(c, attr),
35        Item::Fn(f) => crate::wasm_fn::wasm_fn_impl(f, attr),
36        _ => Err(syn::Error::new_spanned(item, "Unexpected item")),
37    };
38
39    result.map(|ts| ts.into())
40}