| Crates.io | winrt-xaml-macros |
| lib.rs | winrt-xaml-macros |
| version | 1.0.0 |
| created_at | 2025-12-31 19:31:13.484649+00 |
| updated_at | 2025-12-31 19:31:13.484649+00 |
| description | Procedural macros for compile-time XAML parsing in winrt-xaml |
| homepage | |
| repository | https://github.com/pegasusheavy/winrt-xaml |
| max_upload_size | |
| id | 2015174 |
| size | 22,638 |
Procedural macros for compile-time XAML parsing in Rust.
This crate provides the xaml! macro which parses XAML markup at compile time and generates Rust code to create WinRT controls. This provides:
use winrt_xaml::xaml;
// This XAML is parsed at compile time!
let button = xaml! {
r##"<Button Content="Click Me"
Width="200"
Height="50"
Background="#FF0078D4"
Foreground="#FFFFFFFF"
CornerRadius="8" />"##
}?;
// button is a Result<XamlUIElement, Error>
<Button> - Interactive button control<TextBlock> - Text display<TextBox> - Text input<StackPanel> - Layout panel<Grid> - Grid layout<ScrollViewer> - Scrollable containerWidth, Height - Dimensions (f64)Background, Foreground - Colors (hex: #AARRGGBB or #RRGGBB)Margin, Padding - Spacing (uniform f64)Content - Button textCornerRadius - Rounded corners (f64)Text - Display textFontSize - Font size (f64)FontWeight - Font weight (i32, e.g., 700 for bold)Text - Initial textPlaceholderText - Placeholder textOrientation - "Vertical" or "Horizontal"Spacing - Space between children (f64)use winrt_xaml::xaml;
use winrt_xaml::error::Result;
fn create_ui() -> Result<()> {
// Title
let title = xaml! {
r##"<TextBlock Text="My App"
FontSize="24"
FontWeight="700"
Foreground="#FF00D7FF" />"##
}?;
// Input field
let input = xaml! {
r##"<TextBox PlaceholderText="Enter text..."
Width="300"
Height="40"
Background="#FF2D2D2D"
Foreground="#FFFFFFFF" />"##
}?;
// Submit button
let button = xaml! {
r##"<Button Content="Submit"
Width="300"
Height="50"
Background="#FF28A745"
Foreground="#FFFFFFFF"
CornerRadius="10" />"##
}?;
Ok(())
}
The xaml! macro:
quick-xmlResult<XamlUIElement, Error>The macro expands to efficient Rust code:
// Input:
let button = xaml! {
r##"<Button Content="Hello" Width="200" Height="50" />"##
}?;
// Expands to:
let button = (|| -> winrt_xaml::error::Result<winrt_xaml::xaml_native::XamlUIElement> {
let __element = winrt_xaml::xaml_native::XamlButton::new()?;
__element.set_content("Hello")?;
__element.set_size(200.0, 50.0)?;
Ok(__element.as_uielement())
})()?;
Invalid XAML is caught at compile time:
// This will fail to compile:
let button = xaml! {
r##"<InvalidElement />"## // Error: Unsupported XAML element: InvalidElement
}?;
MIT OR Apache-2.0