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::requestBoth are also available via
toolkit_zero::socket::prelude::*.The
toolkit-zero-macros/socket-serverandtoolkit-zero-macros/socket-clientfeature gates are automatically activated by the correspondingtoolkit-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
Usage
[]
# Server-side macro
= { = "3", = ["socket-server"] }
# Client-side macro
= { = "3", = ["socket-client"] }
# Both
= { = "3", = ["socket"] }
// Server
use mechanism;
// or
use *; // includes both mechanism and request
// Client
use request;
// or
use *;
License
MIT — same as toolkit-zero.
What this crate provides
A single attribute macro — #[mechanism] — that is a concise shorthand for
the toolkit-zero socket-server route builder chain.
Instead of:
server.mechanism;
you write:
async
The macro expands to the exact same server.mechanism(…) statement in-place.
The function name is discarded; the body is transplanted verbatim into the
.onconnect(…) closure.
Usage
Add toolkit-zero — not this crate — to your Cargo.toml:
[]
= { = "3", = ["socket-server"] }
Then import the macro from the server module:
use mechanism;
// or
use *;
Supported forms
| Attribute | Function parameters |
|---|---|
#[mechanism(server, METHOD, "/path")] |
() |
#[mechanism(server, METHOD, "/path", json)] |
(body: T) |
#[mechanism(server, METHOD, "/path", query)] |
(params: T) |
#[mechanism(server, METHOD, "/path", encrypted(key))] |
(body: T) |
#[mechanism(server, METHOD, "/path", encrypted_query(key))] |
(params: T) |
#[mechanism(server, METHOD, "/path", state(expr))] |
(state: S) |
#[mechanism(server, METHOD, "/path", state(expr), json)] |
(state: S, body: T) |
#[mechanism(server, METHOD, "/path", state(expr), query)] |
(state: S, params: T) |
#[mechanism(server, METHOD, "/path", state(expr), encrypted(key))] |
(state: S, body: T) |
#[mechanism(server, METHOD, "/path", state(expr), encrypted_query(key))] |
(state: S, params: T) |
Valid HTTP methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
The keywords after the path (json, query, state, encrypted,
encrypted_query) may appear in any order.
Full example
use ;
use SerializationKey;
use ;
use ;
async
License
MIT — same as toolkit-zero.