resext-macro
Procedural macro implementation for ResExt
This crate provides the #[resext] attribute macro for ergonomic error handling. It is not meant to be used directly - use the resext crate instead.
Overview
The proc macro generates all necessary error handling code from a simple attribute:
This expands to approximately 200 lines of boilerplate including:
DisplayandErrortrait implementationsResErrwrapper struct with context storageResExttrait with context methodsFromimplementations for automatic conversion- Type alias for
Result<T, ResErr>
Attribute Options
Basic Options
prefix- Prepended to all error messagessuffix- Appended to all error messages
Context Formatting
msg_prefix- Prepended to each context messagemsg_suffix- Appended to each context messagemsg_delimiter- Separator between contexts (default: " - ")
Source Formatting
source_prefix- Prepended to underlying error (default: "Error: ")
Variant Display
With include_variant = true, errors display as:
Context
Error: Io: No such file or directory
Without it (default):
Context
Error: No such file or directory
Custom Type Alias
// Generated:
pub type AppResult<T> = ;
Supported Variant Types
Tuple Variants (Single Field)
Named Fields (Single Field)
Unit Variants
Displays as the variant name: NotFound
Limitations
- Variants must have at most one field
- Multi-field variants are not supported
- Generic enums are not supported
Generated Code
For this input:
The macro generates (simplified):
pub type Res<T> = ;
Performance Characteristics
- Context storage uses
Vec<u8>for string data - Pre-calculates required capacity to minimize allocations
- Reuses buffer for multiple context calls
- Zero overhead when errors don't occur
Implementation Details
The macro uses:
synfor parsing Rust syntaxquotefor code generationproc-macro2for token manipulation
Error Messages
The macro provides helpful error messages:
error: enum variants used in `#[resext]` can only have 1 field
--> src/main.rs:X:Y
|
X | Multi(std::io::Error, String),
| ^^^^^^^^^^^^^^^^^^^^^^^^
Dependencies
syn- Parsingquote- Code generationproc-macro2- Token manipulation
License
MIT - See LICENSE for details.
See Also
- resext - Main crate documentation
- ResExt repository