Skip to main content

Crate wabot_macros

Crate wabot_macros 

Source
Expand description

§wabot-macros

Procedural macros that give wabot-rust a developer experience close to the TypeScript decorator syntax of wabot-ts. Each macro maps to a decorator family in the original framework — see the docstring on each one for the TS analogue.

The macros emit code that depends on wabot-core. Re-exports flow through the wabot crate so users only need use wabot::prelude::*.

Attribute Macros§

action
@action('name') — marker on a method inside a #[ui_controller] impl block. Becomes POST <controller>/_action/<name> returning JSON; the name defaults to the method name.
chat_controller
@chatController() — apply to an impl Controller block. Walks every method; methods carrying a channel marker (#[cmd], future #[telegram] / #[socket], …) are wired into a per-channel dispatch closure, and a Self::register_channels(&Container, Arc<ChatResolver>) function is emitted that hands each channel its dispatch via listen.
cmd
@cmd() — marks a method inside a #[chat_controller] impl block as the handler for the CLI (CmdChannel) channel. Inert outside a #[chat_controller] impl block.
command
@command('name') — apply to a command-payload struct. Bakes Self::COMMAND_NAME = "name" and an impl CommandData so the typed run_command / schedule_command helpers can find the name. The struct must also derive Serialize + Deserialize.
command_handler
@commandHandler({ command, ...options }) — apply to a handler struct. Emits Self::__handler_entry(&Container) -> CommandHandlerEntry so the runtime can dispatch by COMMAND_NAME. The handler must implement async fn handle(&self, cmd: MyCommand) -> Result<(), E: Display> on a separate inherent impl, and the struct itself must be #[injectable].
cron_command
@cronCommand({ name, cron }) — apply to a command struct that’s fired periodically. Bakes COMMAND_NAME + CRON_SCHEDULE.
cron_handler
@cronHandler({ ...config }) — apply to a handler struct. Emits Self::__cron_entry(&Container) -> CronHandlerEntry. The handler must implement async fn handle(&self) -> Result<(), E: Display> (cron handlers carry no arguments) and the struct must be #[injectable].
delete
@onDelete(...) — see get.
description
@description("...") — attaches a human-readable description to a field or method. On struct fields it is consumed by #[derive(Validate)] and folded into [PropertyInfo::description]. On methods it is consumed by mindset macros (later phase). Used standalone, the attribute is a no-op so it never appears as “unknown attribute”.
get
@onGet(...) — marks a method inside a #[rest_controller] impl block as an HTTP GET endpoint. Inert when used outside such a block.
head
#[head("…")] — the headers a GET would answer with, and no body.
injectable
@injectable() — emits a Self::inject(&Container) -> Arc<Self> constructor that resolves each Arc<T> field from the container and fills the rest from Default. Does not auto-register; pair with #[singleton] or call container.register_* manually.
max_body
#[max_body(N)] — how many bytes this endpoint buffers before refusing the request. Defaults to DEFAULT_MAX_BODY (1 MiB), which is generous for a REST payload and mean for a file upload.
middleware
@middleware(MyMiddleware) — attaches a middleware to the next endpoint method. Multiple #[middleware(...)] attributes stack; they run in source order before the handler. Inert outside a #[rest_controller] impl block.
mindset_lang
Deprecated alias of tool_lang.
mindset_module
Deprecated alias of tools, from when tool sets were a mindset-only concept.
mindset_tool
Deprecated alias of tool.
on_socket_event
#[on_socket_event("name")] — marker on one method inside a #[socket_controller] impl block. Inert outside such a block.
patch
#[patch("…")] — a partial update. See get.
post
@onPost(...) — see get.
put
@onPut(...) — see get.
raw
#[raw] — take the Request and return the Response untouched.
rest_controller
@restController('/path') — apply to an impl Controller block. The macro walks every method, finds the #[get] / #[post] / #[put] / #[delete] / #[middleware] attributes, and emits a Self::register_routes(&Container, axum::Router) -> axum::Router function that adds every endpoint nested under the controller’s base path.
singleton
@singleton() — like #[injectable], but additionally registers the type as a singleton via inventory. Equivalent to writing container.register_singleton_factory::<Self, _>(|c| Self::inject(c)) at framework startup, except the registration is collected at compile time so a fresh Container::new() already knows about it.
socket
@socket() — marks a method as the handler for the Socket.IO (SocketChannel) channel. Namespace + bind address come from the runtime-registered SocketChannelConfig singleton. Inert outside a #[chat_controller] impl block.
socket_controller
@socketController('/ns') — apply to an impl MyController block. Walks every method, finds those carrying #[on_socket_event("name")], and emits Self::register_socket_handlers(&Container, &SocketIo).
telegram
@telegram() — marks a method as the handler for the Telegram (TelegramChannel) channel. The bot token is supplied at runtime via a registered Arc<TelegramChannelConfig> singleton, not as an attribute argument. Inert outside a #[chat_controller] impl block.
tool
#[tool("description")] — marker on one method inside a #[tools] impl block. Inert outside such a block — the enclosing macro reads it and strips it.
tool_lang
#[tool_lang("english")] — per-method language override, when one tool’s description is written in a different language from the rest of its set. Inert outside a #[tools] impl block.
tools
@tools() — apply to an impl MyTools block. Walks every method, finds those carrying #[tool("description")], and emits a Self::register_tools(&Container) -> Vec<ToolDefinition> helper. The struct must be #[injectable] (or #[singleton]) so dispatch can resolve it from the container.
ui_controller
@uiController({ path, app, layout }) — apply to the controller’s impl block. Walks every method, interprets #[view], #[action] and #[middleware], and emits Self::register_ui_routes.
view
@view('/path') — marker on a method inside a #[ui_controller] impl block. The method takes &self plus at most one typed request and returns UiResult<ViewBody> (or UiResult<ViewOutcome> to redirect).

Derive Macros§

Validate
#[derive(Validate)] — generates a Validate impl that owns a ModelInfo describing the struct and a validate() that runs every declared constraint against an instance.