worker-bindings

Crates.ioworker-bindings
lib.rsworker-bindings
version
sourcesrc
created_at2024-06-25 09:33:05.024211
updated_at2024-11-08 20:07:07.275327
descriptionBindings in wrangler.toml to Rust struct
homepagehttps://crates.io/crates/worker-bindings
repositoryhttps://github.com/ohkami-rs/worker-bindings
max_upload_size
id1283152
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
core (github:ohkami-rs:core)

documentation

https://docs.rs/worker-bindings

README

worker-bindings

Automatically detect bindings in wrangler.toml and bind them to a Rust struct

License CI status crates.io

Example

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?;

    //...
}

Note

  • #[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.
  • This crate is originally developed in Ohkami web framework and later extracted as an independent edition.

License

worker-bindings is licensed under the MIT License (LICENSE or https://opensource.org/licenses/MIT).

Commit count: 10

cargo fmt