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
mod internal;


/// Automatically bind bindings in your `wrangler.toml` into a Rust struct
/// 
/// - This uses the default (top-level) env by default. You can configure it
///   by passing an env name as argument like `#[bindings(dev)]`
/// - You can the bindings instance by `<struct name>::from(&env)`.
/// 
/// <br>
/// 
/// ## Example
/// ---
/// *wrangler.toml*
/// ```ignore
/// [[kv_namespaces]]
/// binding = "MY_KV"
/// id      = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
/// ```
/// ---
/// *lib.rs*
/// ```ignore
/// use worker::*;
/// use worker_bindings::bindings;
/// 
/// #[bindings]
/// struct Bindings;
/// 
/// #[event(fetch)]
/// pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
///     let b = Bindings::from(&env);
/// 
///     let data = b.MY_KV.get("data").text().await
///         .expect("Failed to get data");
/// 
///     //...
/// }
/// ```
/// ---
/// 
/// <br>
/// 
/// _**note**_ : `#[bindings]` only supports
/// 
/// - KV
/// - D1
/// - Vars
/// - Service
/// - Queue (producer)
/// 
/// in cuurent version, as `worker` crate does.
/// ( `worker` supports secrets, but secrets aren't written in wrangler.toml... )
/// 
/// <br>
/// 
/// _**tips**_ :
/// 
/// - You can switch multiple envs by package features using some `#[cfg_attr(feature = "...", bindings(env_name))]`s
/// - For rust-analyzer user : When you add an new binding into wrangler.toml,
///   you will need to reload `#[bindings] struct ...;` to notice the new one to analyer.
///   Then what you have to do is just deleting `;` and immediate restoring it.
#[proc_macro_attribute]
pub fn bindings(env: proc_macro::TokenStream, bindings_struct: proc_macro::TokenStream) -> proc_macro::TokenStream {
    internal::bindings(env.into(), bindings_struct.into())
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}