TAP Message Derive Macro
Procedural derive macro for automatically implementing TAP message traits.
Overview
This crate provides the #[derive(TapMessage)] procedural macro that automatically implements both TapMessage and MessageContext traits for TAP protocol message types. It reduces boilerplate by generating implementations based on struct field attributes.
Usage
Add this to your Cargo.toml:
[]
= "0.2.0"
= "0.2.0"
= { = "1.0", = ["derive"] }
Basic Example
use TapMessage;
use ;
use PlainMessage;
use Result;
use ;
// You still need to implement TapMessageBody for message-specific logic
Enhanced Usage with TapMessageBody Derive
For automatic generation of TapMessageBody implementation (including to_didcomm() with automatic participant routing), use the separate TapMessageBody derive macro:
use ;
use Participant;
use ;
// No need to manually implement TapMessageBody - it's automatically generated!
// The generated implementation includes:
// - message_type() returning the specified type
// - validate() with basic validation (can be customized by implementing manually)
// - to_didcomm() with automatic participant extraction and routing
This approach eliminates boilerplate and automatically generates the to_didcomm() implementation with proper participant routing based on field attributes.
Supported Attributes
Struct-level Attributes
#[tap(message_type = "url")]- TAP message type URL (required for TapMessageBody derive)
Field-level Attributes
#[tap(participant)]- Marks a field as a single participant (type:ParticipantorOption<Participant>)#[tap(participant_list)]- Marks a field as a list of participants (type:Vec<Participant>)#[tap(transaction_id)]- Marks the transaction ID field (type:String)#[tap(optional_transaction_id)]- Marks an optional transaction ID field (type:Option<String>)#[tap(thread_id)]- Marks a thread ID field for thread-based messages (type:Option<String>)
What Gets Generated
The derive macro generates implementations for two traits:
1. TapMessage Trait
The generated implementation:
- Extracts participant DIDs from all marked fields
- Returns the appropriate thread/transaction ID
- Creates properly threaded reply messages
- Delegates validation to your
TapMessageBodyimplementation
2. MessageContext Trait
The generated implementation:
- Collects references to all
Participantobjects - Extracts DIDs from all participants
- Creates transaction context with ID and message type
Advanced Examples
Message with Optional Transaction ID
Thread-based Message
Message with Multiple Participant Lists
Integration with tap-msg
This derive macro is designed to work seamlessly with the tap-msg crate. When both are used together:
- Define your message struct with the derive macro
- Implement
TapMessageBodyfor message-specific behavior - The macro handles the boilerplate trait implementations
- Your message type is ready to use with TAP agents and protocols
Limitations
- Only works with structs that have named fields
- Requires serde
SerializeandDeserializeto be derived - The
TapMessageBodytrait must still be implemented manually - Field types must match exactly (e.g.,
Participant, not a type alias)
License
This project is licensed under the MIT License - see the LICENSE file for details.