Easy Macros
Automatic error context for any Rust project + powerful procedural macro utilities.
Quick Start
# Only automatic error context
[]
= { = "...", = ["general"] }
# All tools for proc-macro development
[]
= { = "...", = ["full"] }
Features
1. Automatic Error Context - Works in Any Project
Feature flag: always-context (included in general and full)
Add .with_context() to all ? operators automatically:
use always_context;
use Result;
Control attributes: #[no_context], #[context(display)], #[context(ignore)]
2. Attribute Pattern Matching
Feature flag: attributes (included in full)
Extract values from attributes using __unknown__ placeholder:
use ;
// Extract from type attributes
let routes: = get_attributes!;
// Extract from field attributes
let methods: =
fields_get_attributes!;
// Partial identifier matching
let tests: = get_attributes!;
// Matches: #[test_case_one], #[test_case_two], etc.
Check attributes: has_attributes!(input, #[derive(Debug)] #[serde(...)])
Filter fields: fields_with_attributes!(input, #[validate])
3. Exhaustive AST Traversal
Feature flag: all-syntax-cases (included in full)
Generate recursive handlers for all syn types:
use all_syntax_cases;
all_syntax_cases!
Smart unwrapping of Box<T>, Vec<T>, Punctuated<T, _>. Generates handlers for Item, Expr, Stmt, Pat, Type, and more.
4. Helper Utilities
Feature flags: Individual helpers or full for all
use *;
// Manual error context with file/line info
// Feature: `context` (included in `general` and `full`)
read.with_context?;
// Token stream builder
// Feature: `tokens-builder` (included in `full`)
let mut tokens = default;
tokens.add;
tokens.braced; // Wrap in { }
// Parse with compile_error! on failure
// Feature: `parse-macro-input` (included in `full`)
let parsed = parse_macro_input!;
// Generate indexed names: field0, field1, field2
// Feature: `indexed-name` (included in `full`)
let names = indexed_name;
// Find crates (handles renames)
// Feature: `find-crate` (included in `full`)
let path = find_crate?;
let async_rt = find_crate_list?;
5. Result Type for Proc Macros
Feature flag: anyhow-result (included in full)
Use anyhow::Result<TokenStream> in proc macros:
use anyhow_result;
// Errors convert to compile_error! automatically
Complete Example
use ;
Feature Flags
Feature Groups
-
general- Automatic error context for any project- Includes:
always-context,context - Use when you only need automatic error context, not proc-macro development tools
- Includes:
-
full- Complete toolkit for proc-macro development- Includes:
all-syntax-cases,always-context,attributes,anyhow-result, and all helpers - Use when building procedural macros or need the full feature set
- Includes:
-
build- Build-time macro that auto-adds#[always_context]to all functions returninganyhow::Result- Standalone feature, not included in
generalorfull - Add to
[build-dependencies]and configure viabuild.rs
- Standalone feature, not included in
Individual Features
Core Proc-Macro Tools:
all-syntax-cases- Exhaustive AST traversal and handler generationalways-context-#[always_context]attribute for automatic error contextattributes- Attribute pattern matching macros (has_attributes!,get_attributes!, etc.)anyhow-result-#[anyhow_result]for usinganyhow::Result<TokenStream>in proc-macros
Helper Utilities (granular control):
context-context!()macro for manual error context with file/line infotokens-builder-TokensBuilderfor incrementally building token streamsindexed-name-indexed_name()for generating indexed identifiersfind-crate-find_crate()andfind_crate_list()for locating crates with rename supportparse-macro-input-parse_macro_input!()with automaticcompile_error!on parse failureexpr-error-wrap-expr_error_wrap()utilities for wrapping expressionsreadable-token-stream- Token stream formatting utilitiestoken-stream-consistent- Consistent token stream string conversion
Integration Features
easy-sql- Integration with Easy-SQL crate- Adds Easy-SQL support to
#[always_context]and attribute macros - Optional: only needed if using Easy-SQL in your project
- Adds Easy-SQL support to
License
Apache License, Version 2.0 - See LICENSE