| Crates.io | worker-bindings |
| lib.rs | worker-bindings |
| version | 2.1.0 |
| created_at | 2024-06-25 09:33:05.024211+00 |
| updated_at | 2024-11-08 20:07:07.275327+00 |
| description | Bindings in wrangler.toml to Rust struct |
| homepage | https://crates.io/crates/worker-bindings |
| repository | https://github.com/ohkami-rs/worker-bindings |
| max_upload_size | |
| id | 1283152 |
| size | 19,707 |
Automatically detect bindings in wrangler.toml and bind them to a Rust struct
wrangler.toml
[vars]
MY_VAR = "my-variable"
[[kv_namespaces]]
binding = "MY_KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
lib.rs
use worker::*;
use worker_bindings::bindings;
/* This knows all your bindings in wrangler.toml */
#[bindings]
struct Bindings;
#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
/* load bindings from env */
let b = Bindings::from(&env);
let var: &'static str = b.MY_VAR;
let data = b.MY_KV.get("data").text().await?;
//...
}
#[bindings] works in a cargo workspace but has a limitation that it can't resolve wrangler.toml if more than one members have wrangler.tomls.worker-bindings is licensed under the MIT License (LICENSE or https://opensource.org/licenses/MIT).