try_from_expr
A Rust procedural macro for generating TryFrom<&syn::Expr> implementations for
enums. This allows you to parse Rust syntax expressions into strongly-typed enum
values, making it easy to work with configuration DSLs, macro arguments, and
other syntax-based APIs.
Features
- 🚀 Automatic Parser Generation - Derives
TryFrom<&syn::Expr>for your enums - 📦 Multiple Variant Types - Supports unit, tuple, and struct variants
- 🎯 Smart Type Detection - Automatically detects wrapper vs leaf enums
- 🔧 Flexible Parsing - Works with primitives, collections, and custom types
- 💡 Helpful Error Messages - Provides clear error messages for parse failures
- ⚡ Zero Runtime Overhead - All parsing logic is generated at compile time
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use TryFromExpr;
use Expr;
// Parse from a syn::Expr
let expr: Expr = parse_str.unwrap;
let config = try_from.unwrap;
Usage Examples
Basic Enum with Different Variant Types
// Parse unit variant
let expr = parse_str.unwrap;
let setting = try_from.unwrap;
// Parse tuple variant
let expr = parse_str.unwrap;
let setting = try_from.unwrap;
// Parse struct variant
let expr = parse_str.unwrap;
let setting = try_from.unwrap;
Wrapper Enums (Composite Enums)
The macro automatically detects "wrapper" enums that contain other enum types and generates optimized parsing:
// The macro detects this is a wrapper and allows parsing nested enums
let expr = parse_str.unwrap;
let constraint = try_from.unwrap;
Working with Collections
// Parse Vec
let expr = parse_str.unwrap;
let data = try_from.unwrap;
// Parse HashMap
let expr = parse_str.unwrap;
let data = try_from.unwrap;
Optional Values
// Explicit Some
let expr = parse_str.unwrap;
// Explicit None
let expr = parse_str.unwrap;
// Implicit Some (bare value treated as Some)
let expr = parse_str.unwrap;
Force Mode Selection
By default, the macro automatically detects whether your enum is a wrapper or leaf enum. You can override this:
// Force wrapper mode
// Force leaf mode
How It Works
The macro analyzes your enum at compile time and generates a
TryFrom<&syn::Expr> implementation that:
- Unwraps any parentheses or group expressions
- Matches the expression type (path, call, struct, literal)
- Parses the variant name and validates it belongs to your enum
- Extracts and parses any parameters or fields
- Constructs the appropriate enum variant
- Returns helpful error messages for any parsing failures
Supported Types
The macro has built-in support for:
- Primitives:
bool,char,String, all integer types,f32,f64 - Collections:
Vec<T>,HashMap<K, V>,BTreeMap<K, V>,Option<T> - Special:
OrderedFloat<T>from theordered-floatcrate - Custom Types: Any type that implements
TryFrom<&syn::Expr>
Error Handling
The macro provides detailed error messages:
// Unknown variant
"Unknown variant 'Invalid' for enum 'Setting'. Valid unit variants: Default"
// Wrong number of arguments
"Variant 'Coordinate' expects exactly 2 arguments, but 3 were provided"
// Type parsing failure
"Failed to parse argument 1: expected u32, got string literal"
// Missing required field
"Missing required field 'name' for variant 'Config'"
Project Structure
This workspace contains two crates:
try_from_expr- The main library crate that users importtry_from_expr_derive- The procedural macro implementation
License
This project is licensed under the MIT license.
Contributing
Please feel free to submit a Pull Request.