Besedarium is a production-ready Rust library for building, verifying, and executing communication protocols with compile-time safety and automatic documentation generation. Using advanced session types and derive macros, it ensures your distributed systems and networked applications follow correct message flowsβcatching protocol violations before they reach production.
- π Type-Level Protocol Safety: Protocols defined as Rust types with compile-time verification
- π Derive Macro System: Automatic implementation generation with
#[derive(GlobalProtocol, Role, Message)]
- π Documentation for Free: Automatic Mermaid sequence diagram generation with
#[derive(GenerateDiagram)]
- π Dual Protocol Generation: Automatic generation of protocol duals for role symmetry
- β‘ Robust Runtime System: Async execution with graceful shutdown, resource tracking, and error recovery
- π― Advanced DSL: Attribute macros for elegant protocol definition syntax
- π§ͺ Comprehensive Testing: 256+ tests ensuring reliability and correctness
Add Besedarium to your Cargo.toml
:
[dependencies]
besedarium = "0.1"
[features]
derive = ["besedarium/derive"] # Enable derive macros
use besedarium::prelude::*;
// Define roles
#[derive(Role, Clone, Debug)]
struct Customer;
#[derive(Role, Clone, Debug)]
struct Agency;
// Define messages
#[derive(Message, Clone, Debug)]
struct OrderMsg(String);
#[derive(Message, Clone, Debug)]
struct QuoteMsg(u32);
// Define protocol with automatic documentation
#[derive(GlobalProtocol, GenerateDiagram)]
#[protocol(
roles(Customer, Agency),
io_type = "BiDirectionalAction"
)]
struct CustomerAgencyProtocol {
// Customer sends order, Agency responds with quote
step1: TChanSend<DefaultChan, OrderMsg, Customer, Agency, BiDirectionalAction>,
step2: TChanRecv<DefaultChan, QuoteMsg, Agency, Customer, BiDirectionalAction>,
end: TChanEnd<DefaultChan, DefaultMessage, BiDirectionalAction>,
}
The #[derive(GenerateDiagram)]
automatically generates this Mermaid sequence diagram in your documentation:
sequenceDiagram
participant Customer as Customer
participant Agency as Agency
Customer->>+Agency: OrderMsg
Agency->>+Customer: QuoteMsg
Session types provide a formal, type-based approach to describing and verifying communication protocols between concurrent or distributed processes. Key research includes:
- Honda, Vasconcelos, Kubo: "Language primitives and type discipline for structured communication-based programming" (ESOP '98)
- Yoshida, Carbone: "Multiparty asynchronous session types" (POPL '15)
- Gay, Vasconcelos: "Linear type theory for asynchronous session types" (JFP '10)
- Global Protocols: Define the overall communication structure between all participants
- Local Endpoints: Project global protocols to individual participant views
- Duality Verification: Automatic checking that protocol participants are compatible
- Runtime Execution: Safe async execution with comprehensive error handling
#[derive(GlobalProtocol)]
#[protocol(
roles(Client, Server),
generate_dual = true,
dual_name = "ServerClientProtocol"
)]
struct ClientServerProtocol {
// Original protocol definition
}
// Automatically generates the dual protocol for Server-side perspective
#[protocol]
struct WebServiceProtocol {
#[role(display_name = "WebClient")]
client: Client,
#[role(display_name = "APIGateway")]
gateway: Gateway,
#[session_type]
flow: "
Client -> Gateway: HttpRequest,
Gateway -> Backend: ProcessRequest,
Backend -> Gateway: Response,
Gateway -> Client: HttpResponse
"
}
- Graceful Shutdown: Configurable timeout-based termination
- Resource Tracking: Automatic detection and cleanup of leaked resources
- Deadlock Detection: Runtime analysis of potential deadlock conditions
- Error Recovery: Comprehensive error handling with recovery suggestions
Check out examples/verify_protocol_examples.rs
for real-world protocol patterns:
- Customer-Agency simple protocols
- Multi-party web service communication
- Complex choice and branching patterns
- Automatic diagram generation demos
- 256+ Unit Tests: Core protocol and duality verification
- Integration Tests: Multi-party protocol execution
- Derive Macro Tests: Complete macro functionality validation
- Runtime Tests: Async execution and error handling
- Build documentation:
cargo doc --open
- Core Concepts:
docs/duality.md
- Complete implementation guide - Protocol Examples:
docs/protocol-examples.md
- Real-world patterns - Runtime Patterns:
docs/runtime-implementation-patterns.md
Besed 70C3 arium is feature-complete and production-ready with:
- β Core Protocol System: Complete type-level protocol safety (Task 1.1-1.4)
- β Comprehensive Testing: 256+ tests covering all functionality (Task 2.1-2.5)
- β Advanced Features: Derive macros, DSL, visualization tools (Task 3.1-3.5)
- β Runtime System: Async execution with full lifecycle management
- β Documentation Generation: Automatic Mermaid diagrams and protocol docs
All major development phases are complete with extensive test coverage and production-ready implementations.
Contributions, questions, and protocol ideas are welcome!
- Open an Issue: Bug reports, feature requests, or questions
- Submit a PR: Code improvements, documentation, or examples
- Protocol Examples: Share interesting protocol patterns you've implemented
- Rust:
session-types
crate - Scala: Effpi library
- Haskell:
session
package
Besedarium: Type-safe protocols with documentation for free.