summaryrefslogtreecommitdiff
path: root/rust/crates/bridge/src/bridge.rs
blob: 99b00615afe54fd896f272f5f730461cb0801186 (plain) (blame)
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
use proc_macro::TokenStream;
use quote::quote;
use syn::{FnArg, Item, ItemConst, ItemFn, parse_macro_input};

#[proc_macro_attribute]
pub fn export(_attr: TokenStream, item: TokenStream) -> TokenStream {
    match parse_macro_input!(item as Item) {
        Item::Fn(function) => export_fn(function),
        Item::Const(constant) => export_const(constant),
        other => syn::Error::new_spanned(other, "#[export] only supports fn and const items")
            .to_compile_error()
            .into(),
    }
}

fn export_fn(function: ItemFn) -> TokenStream {
    let param_count = function
        .sig
        .inputs
        .iter()
        .filter(|arg| matches!(arg, FnArg::Typed(_)))
        .count();

    if param_count > 1 {
        return syn::Error::new_spanned(
            &function.sig.inputs,
            "#[export] functions must accept a single options struct, not positional arguments. \
             Wrap parameters in a struct: `fn foo(FooOptions { a, b }: FooOptions)`",
        )
        .to_compile_error()
        .into();
    }

    let js_name = snake_to_camel(&function.sig.ident.to_string());

    quote! {
        #[cfg_attr(feature = "wasm", ::wasm_bindgen::prelude::wasm_bindgen(js_name = #js_name))]
        #function
    }
    .into()
}

fn export_const(constant: ItemConst) -> TokenStream {
    let js_name = constant.ident.to_string();
    let const_ident = &constant.ident;
    let getter_ident = syn::Ident::new(
        &format!("__const_{}", constant.ident.to_string().to_lowercase()),
        constant.ident.span(),
    );

    quote! {
        #constant

        #[cfg(feature = "wasm")]
        #[::wasm_bindgen::prelude::wasm_bindgen(js_name = #js_name)]
        pub fn #getter_ident() -> f64 {
            #const_ident as f64
        }
    }
    .into()
}

fn snake_to_camel(name: &str) -> String {
    let mut camel = String::with_capacity(name.len());
    let mut should_uppercase_next = false;

    for character in name.chars() {
        if character == '_' {
            should_uppercase_next = true;
            continue;
        }

        if should_uppercase_next {
            camel.push(character.to_ascii_uppercase());
            should_uppercase_next = false;
            continue;
        }

        camel.push(character);
    }

    camel
}

#[cfg(test)]
mod tests {
    use super::snake_to_camel;

    #[test]
    fn converts_snake_case_to_camel_case() {
        assert_eq!(snake_to_camel("do_something"), "doSomething");
        assert_eq!(snake_to_camel("a_b_c"), "aBC");
        assert_eq!(snake_to_camel("already"), "already");
    }
}