worker_bindings/
lib.rs

1mod internal;
2
3
4/// Automatically bind bindings in your `wrangler.toml` into a Rust struct
5/// 
6/// - This uses the default (top-level) env by default. You can configure it
7///   by passing an env name as argument like `#[bindings(dev)]`
8/// - You can the bindings instance by `<struct name>::from(&env)`.
9/// 
10/// <br>
11/// 
12/// ## Example
13/// ---
14/// *wrangler.toml*
15/// ```ignore
16/// [[kv_namespaces]]
17/// binding = "MY_KV"
18/// id      = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
19/// ```
20/// ---
21/// *lib.rs*
22/// ```ignore
23/// use worker::*;
24/// use worker_bindings::bindings;
25/// 
26/// #[bindings]
27/// struct Bindings;
28/// 
29/// #[event(fetch)]
30/// pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
31///     let b = Bindings::from(&env);
32/// 
33///     let data = b.MY_KV.get("data").text().await
34///         .expect("Failed to get data");
35/// 
36///     //...
37/// }
38/// ```
39/// ---
40/// 
41/// <br>
42/// 
43/// _**note**_ : `#[bindings]` supports
44/// 
45/// - KV
46/// - D1
47/// - Vars
48/// - Service
49/// - Queue (producer)
50/// - Durable Objects
51/// 
52/// in cuurent version, as `worker` crate does.
53/// ( `worker` supports secrets, but secrets aren't written in wrangler.toml... )
54/// 
55/// <br>
56/// 
57/// _**tips**_ :
58/// 
59/// - You can switch between multiple `env`s by feature flags like `#[cfg_attr(feature = "...", bindings(env_name))]`
60/// - For `rust-analyzer` user : when editting wrangler.toml around bindings,
61///   you'll need to reload `#[bindings] struct ...;` to notice the new bindings to rust-analyer.
62///   For that, what you have to do is just **deleting `;` and immediate restoring it**.
63#[proc_macro_attribute]
64pub fn bindings(env: proc_macro::TokenStream, bindings_struct: proc_macro::TokenStream) -> proc_macro::TokenStream {
65    internal::bindings(env.into(), bindings_struct.into())
66        .unwrap_or_else(syn::Error::into_compile_error)
67        .into()
68}