Generate AsyncAPI 3.0 specs from your Rust code β¨
Just likeutoipadoes for OpenAPI, but for async messaging! π―
protofolio provides procedural macros and runtime support to generate AsyncAPI 3.0 specifications directly from your Rust code. This ensures your documentation stays in sync with your code automatically. No more manual spec maintenance! π
Get up and running in minutes! Here's a taste:
use protofolio::AsyncApi;
use protofolio_derive::{AsyncApi, AsyncApiMessage};
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
// Define your message type
#[derive(Serialize, Deserialize, JsonSchema, AsyncApiMessage)]
#[asyncapi(channel = "events", messageId = "event-v1")]
pub struct Event {
pub id: String,
pub data: String,
}
// Define your AsyncAPI specification
#[derive(AsyncApi)]
#[asyncapi(
info(title = "My API", version = "1.0.0"),
channels("events"),
messages(Event)
)]
pub struct MyApi;
// Generate the spec π¨
let spec = MyApi::asyncapi();
let json = MyApi::asyncapi_json()?;See the Getting Started Guide for installation and detailed instructions. π
- π― Code-first approach: Generate AsyncAPI specs from Rust types
- π Compile-time validation: Channel and message references validated at compile time
- π‘οΈ Type safety: Documentation always matches your code
- π Multi-protocol support: Built-in support for NATS, Kafka, and MQTT
- π YAML and JSON output: Generate specs in both YAML and JSON formats
- π TypeScript generation: CLI tool to generate TypeScript types from AsyncAPI specs
- π·οΈ Enhanced attributes: Support for messageId, name, title, contentType, tags, and more
- π¨ Root-level tags: Reusable tag definitions at the specification level for better organization
- π Components and
$refreferences: Reusable messages, schemas, parameters, bindings, and traits with component references - β Validation: Built-in validation for generated specifications
- π Error handling: Both panic-on-error (
asyncapi()) and Result-based (try_asyncapi()) APIs - β‘ Schema caching: Automatic caching of generated JSON schemas for performance
- π§ Extensible: Structured for easy expansion to other protocols
- π Getting Started - Installation, requirements, and quick start
- π¬ Messages - How to define and configure message types
- π Operations - How to define publish/subscribe operations
- π Security - How to define and use security schemes
- β Validation - Understanding validation and error handling
- π TypeScript Generation - Generate TypeScript types from AsyncAPI specs
- β Best Practices - Recommended patterns and conventions
- π― Basic Examples - Simple usage examples
- π Advanced Examples - Complex patterns and edge cases
- π Integration Examples - Framework integration (Axum, etc.)
- π οΈ Troubleshooting - Common scenarios and solutions
- π Considerations - Design decisions and recommended approaches
- π Migration Guide - Migrating from other approaches
- βοΈ Macro Expansion - How macros work internally
- ARCHITECTURE.md - Architecture and design decisions
- PROTOCOLS.md - Protocol-specific documentation and examples
- CONTRIBUTING.md - Guidelines for contributors
- API Documentation - Full API reference (when published)
Add to your Cargo.toml:
[dependencies]
protofolio = "0.1.0"
protofolio-derive = "0.1.0"
serde = { version = "1.0", features = ["derive"] }
schemars = { version = "1.0", features = ["derive"] }Requirements: Rust 1.80 or later. Your message types must implement Serialize, Deserialize, and JsonSchema.
See the Getting Started Guide for detailed installation instructions. π
This is a Rust workspace containing:
protofolio/- Main runtime library with AsyncAPI data structuresprotofolio-derive/- Procedural macros for code generationprotofolio-cli/- CLI tool for generating TypeScript types from AsyncAPI specs
This crate is in active development. Here's what's currently available:
- β Basic AsyncAPI 3.0 spec generation
- β Multi-protocol support (NATS, Kafka, MQTT)
- β Message and channel mapping
- β JSON Schema generation from Rust types with automatic caching
- β JSON and YAML output formats
- β Enhanced message attributes (messageId, name, title, contentType, tags)
- β Channel parameters and bindings support
- β Specification validation
- β
Comprehensive error handling (
try_asyncapi()with Result types) - β Operations support (publish/subscribe with full attribute support)
- β Protocol-specific bindings for NATS, Kafka, and MQTT
- β Security schemes (userPassword, apiKey, http, oauth2, etc.)
- β External documentation support (Info, Message, Operation)
- β Info fields (contact, license, termsOfService)
- β Server variables (templated URLs with variable definitions)
- β Message examples (single or multiple example payloads)
- β Message headers (schema definition for message headers)
- β
Components and
$refreferences (reusable messages, schemas, parameters, bindings, and traits) - β Correlation IDs (message correlation tracking with location expressions)
- β Channel address field (required AsyncAPI 3.0 field)
- β Operation ID field (unique operation identifiers)
- β Root-level tags (reusable tag definitions at specification level)
- β TypeScript type generation (CLI tool for generating TypeScript types from AsyncAPI specs)
π§ Coming soon:
- Full AsyncAPI 3.0 feature set
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.