toolkit-zero-macros
Procedural macros for toolkit-zero.
This crate is an implementation detail of
toolkit-zero.
Do not depend on it directly. Addtoolkit-zerowith the appropriate feature flag and use the re-exported attribute macros:
Macro toolkit-zerofeature flagtoolkit-zero-macrosfeature gateImport path #[mechanism]socket-serversocket-servertoolkit_zero::socket::server::mechanism#[request]socket-clientsocket-clienttoolkit_zero::socket::client::request#[serializable]serializationserializationtoolkit_zero::serialization::serializable#[serialize]serializationserializationtoolkit_zero::serialization::serialize#[deserialize]serializationserializationtoolkit_zero::serialization::deserialize
#[mechanism]/#[request]are also available viatoolkit_zero::socket::prelude::*.Feature gates are automatically activated by the corresponding
toolkit-zerofeatures — you do not need to set them manually.
#[mechanism] — server-side route declaration
A concise alternative to the server.mechanism(ServerMechanism::…) builder
chain. The decorated async fn is replaced in-place with the equivalent
server.mechanism(…) statement. The function body is transplanted verbatim
into the .onconnect(…) closure.
Syntax
#[mechanism(server, METHOD, "/path")]
#[mechanism(server, METHOD, "/path", json)]
#[mechanism(server, METHOD, "/path", query)]
#[mechanism(server, METHOD, "/path", encrypted(<key_expr>))]
#[mechanism(server, METHOD, "/path", encrypted_query(<key_expr>))]
#[mechanism(server, METHOD, "/path", state(<state_expr>))]
#[mechanism(server, METHOD, "/path", state(<state_expr>), json)]
#[mechanism(server, METHOD, "/path", state(<state_expr>), query)]
#[mechanism(server, METHOD, "/path", state(<state_expr>), encrypted(<key_expr>))]
#[mechanism(server, METHOD, "/path", state(<state_expr>), encrypted_query(<key_expr>))]
Positional: server, METHOD, "/path". Keywords after the path may appear
in any order.
Supported forms
| Attribute keywords | fn parameters |
|---|---|
| (none) | () |
json |
(body: T) |
query |
(params: T) |
encrypted(key) |
(body: T) — VEIL-decrypted before delivery |
encrypted_query(key) |
(params: T) — VEIL-decrypted before delivery |
state(expr) |
(state: S) |
state(expr), json |
(state: S, body: T) |
state(expr), query |
(state: S, params: T) |
state(expr), encrypted(key) |
(state: S, body: T) |
state(expr), encrypted_query(key) |
(state: S, params: T) |
Example
use ;
use ;
use ;
async
#[request] — client-side request shorthand
A concise alternative to the client.method(endpoint)[.json/query/…].send() builder
chain. The decorated fn item is replaced in-place with a let binding statement
that performs the HTTP request. The function name becomes the binding name;
the return type becomes R in the .send::<R>() turbofish. The function body
is discarded.
Syntax
#[request(client, METHOD, "/path", async|sync)]
#[request(client, METHOD, "/path", json(<body_expr>), async|sync)]
#[request(client, METHOD, "/path", query(<params_expr>), async|sync)]
#[request(client, METHOD, "/path", encrypted(<body_expr>, <key_expr>), async|sync)]
#[request(client, METHOD, "/path", encrypted_query(<params_expr>, <key_expr>), async|sync)]
Positional: client, METHOD, "/path". Mode keyword (if any) comes before
async/sync. async uses .send::<R>().await?; sync uses .send_sync::<R>()?.
Supported forms
| Attribute | Generated call | Error type from ? |
|---|---|---|
| (no mode) | .send::<R>() / .send_sync::<R>() |
reqwest::Error |
json(expr) |
.json(expr).send::<R>() |
reqwest::Error |
query(expr) |
.query(expr).send::<R>() |
reqwest::Error |
encrypted(body, key) |
.encryption(body, key).send::<R>() |
ClientError |
encrypted_query(params, key) |
.encrypted_query(params, key).send::<R>() |
ClientError |
The return type annotation on the fn is required — omitting it is a compile error.
Example
use ;
use ;
async
#[serializable] — derive + inject seal/open methods
Automatically derives bincode::Encode + bincode::Decode on a struct or enum and
injects .seal(key) / ::open(bytes, key) methods. Field-level
#[serializable(key = "...")] additionally generates per-field seal_<field> /
open_<field> helpers with a hardcoded key.
Syntax
#[serializable]
struct Foo { ... }
#[serializable]
enum Bar { ... }
#[serializable]
struct Creds {
pub user: String,
#[serializable(key = "field-key")]
pub password: String, // → seal_password / open_password
}
Injected methods
// Struct-level
Example
use serializable;
let c = Config ;
let blob = c.seal.unwrap;
let back = open.unwrap;
#[serialize] — inline seal statement
Replaces a fn item with a seal statement. Two modes:
- Variable mode — fn name → binding name, return type → type annotation (required).
- File write mode — presence of
path = "..."→fs::write(path, seal(...)?)?.
Syntax
#[serialize(expr)] // variable, default key
#[serialize(expr, key = key_expr)] // variable, custom key
#[serialize(expr, path = "file.bin")] // file write, default key
#[serialize(expr, path = "file.bin", key = k)] // file write, custom key
Example
use serialize;
// expands to: let blob: Vec<u8> = seal(&cfg, Some(my_key))?;
fn _
// expands to: fs::write("config.bin", seal(&cfg, None)?)?;
#[deserialize] — inline open statement
Replaces a fn item with an open statement. The return type annotation is required.
Two modes:
- Variable mode — open from a blob expression in scope.
- File read mode — presence of
path = "..."→open(&fs::read(path)?, ...).
Syntax
#[deserialize(blob_expr)] // variable, default key
#[deserialize(blob_expr, key = key_expr)] // variable, custom key
#[deserialize(path = "file.bin")] // file read, default key
#[deserialize(path = "file.bin", key = k)] // file read, custom key
Example
use deserialize;
// expands to: let config: Config = open::<Config>(&blob, Some(my_key))?;
// expands to: let config: Config = open::<Config>(&fs::read("config.bin")?, None)?;
Usage
[]
# Server-side macro
= { = "3", = ["socket-server"] }
# Client-side macro
= { = "3", = ["socket-client"] }
# Serialization macros
= { = "3", = ["serialization"] }
# All socket + serialization
= { = "3", = ["socket", "serialization"] }
// Server
use mechanism;
// Client
use request;
// Serialization
use ;
// or all socket
use *;
License
MIT — same as toolkit-zero.