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
/// 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]` supports
///
/// - KV
/// - D1
/// - Vars
/// - Service
/// - Queue (producer)
/// - Durable Objects
///
/// in cuurent version, as `worker` crate does.
/// ( `worker` supports secrets, but secrets aren't written in wrangler.toml... )
///
/// <br>
///
/// _**tips**_ :
///
/// - You can switch between multiple `env`s by feature flags like `#[cfg_attr(feature = "...", bindings(env_name))]`
/// - For `rust-analyzer` user : when editting wrangler.toml around bindings,
/// you'll need to reload `#[bindings] struct ...;` to notice the new bindings to rust-analyer.
/// For that, what you have to do is just **deleting `;` and immediate restoring it**.