winrt-xaml-macros
Procedural macros for compile-time XAML parsing in Rust.
Overview
This crate provides the xaml! macro which parses XAML markup at compile time and generates Rust code to create WinRT controls. This provides:
- ✅ Compile-time validation: XAML errors are caught during compilation
- ⚡ Zero runtime overhead: No parsing at runtime
- 🔒 Type safety: Generated code is fully typed
- 💡 IDE support: Full autocomplete and error checking
Usage
use xaml;
// This XAML is parsed at compile time!
let button = xaml! ?;
// button is a Result<XamlUIElement, Error>
Supported Elements
<Button>- Interactive button control<TextBlock>- Text display<TextBox>- Text input<StackPanel>- Layout panel<Grid>- Grid layout<ScrollViewer>- Scrollable container
Supported Attributes
Common Attributes
Width,Height- Dimensions (f64)Background,Foreground- Colors (hex:#AARRGGBBor#RRGGBB)Margin,Padding- Spacing (uniform f64)
Button Attributes
Content- Button textCornerRadius- Rounded corners (f64)
TextBlock Attributes
Text- Display textFontSize- Font size (f64)FontWeight- Font weight (i32, e.g., 700 for bold)
TextBox Attributes
Text- Initial textPlaceholderText- Placeholder text
StackPanel Attributes
Orientation- "Vertical" or "Horizontal"Spacing- Space between children (f64)
Example
use xaml;
use Result;
How It Works
The xaml! macro:
- Parses XAML at compile time using
quick-xml - Validates element names and attributes
- Generates Rust code that creates WinRT controls
- Returns a
Result<XamlUIElement, Error>
Generated Code
The macro expands to efficient Rust code:
// Input:
let button = xaml! ?;
// Expands to:
let button = ?;
Compile-Time Validation
Invalid XAML is caught at compile time:
// This will fail to compile:
let button = xaml! ?;
License
MIT OR Apache-2.0