Skip to content

Commit

Permalink
Variable bindings as associated consts (#5)
Browse files Browse the repository at this point in the history
* & bump major version

* & bump major version
  • Loading branch information
kanarus authored Jun 28, 2024
1 parent ed3725b commit ef77fbd
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "worker-bindings"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
authors = ["kanarus <[email protected]>"]
documentation = "https://docs.rs/worker-bindings"
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

*wrangler.toml*
```toml
[vars]
MY_VAR = "my-variable"

[[kv_namespaces]]
binding = "MY_KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Expand All @@ -29,8 +32,11 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
/* load bindings from env */
let b = Bindings::from(&env);

let data = b.MY_KV.get("data").text().await
.expect("Failed to get data");
let var: &'static str = b.MY_VAR;

let var: &'static str = Bindings::MY_VAR;

let data = b.MY_KV.get("data").text().await?;

//...
}
Expand Down
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ edition = "2021"

[dependencies]
worker = { version = "0.3" }
worker-bindings = { version = "1.0", path = ".." }
worker-bindings = { path = ".." }
5 changes: 3 additions & 2 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub async fn main(_req: Request, env: Env, _ctx: worker::Context) -> Result<Resp
/* load bindings from env */
let b = Bindings::from(&env);

let _data = b.KV.get("data").text().await
.expect("Failed to get data");
let _var: &'static str = Bindings::VAR;

let _data = b.KV.get("data").text().await?;

Response::ok("Hello, worker-bindings!")
}
3 changes: 3 additions & 0 deletions example/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "example"
main = "build/worker/shim.mjs"
compatibility_date = "2024-04-19"

[vars]
VAR = "my_variable"

[[kv_namespaces]]
binding = "KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Expand Down
2 changes: 1 addition & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub(super) fn bindings(env: TokenStream, bindings_struct: TokenStream) -> Result
let methods = bindings.iter()
.filter_map(|(name, binding)| match binding {
Binding::Variable(var) => Some(quote! {
#vis const fn #name() -> &'static str { #var }
#vis const #name: &'static str = #var;
}),
_ => None
});
Expand Down

0 comments on commit ef77fbd

Please sign in to comment.