Skip to main content

tyzen_macro/
lib.rs

1//! Procedural macros for the Tyzen ecosystem.
2//! 
3//! This crate provides the bridge between Rust types/functions and TypeScript code generation.
4//! It handles attribute parsing, type metadata collection, and code expansion for Tauri commands.
5
6use proc_macro::TokenStream;
7
8mod command_attr;
9mod event_attr;
10mod export_attr;
11mod type_derive;
12
13/// Marks a function as a Tyzen command.
14/// 
15/// This macro collects metadata about the function's parameters and return type to generate
16/// matching TypeScript bindings. It does NOT emit `#[tauri::command]`.
17/// 
18/// # Example
19/// ```rust
20/// #[tyzen::command(namespace = "auth")]
21/// fn login(username: String) -> Result<User, Error> { ... }
22/// ```
23#[proc_macro_attribute]
24pub fn command(attr: TokenStream, item: TokenStream) -> TokenStream {
25    command_attr::command(attr, item)
26}
27
28/// Marks a function as both a Tyzen command and a Tauri command.
29/// 
30/// Shorthand for applying both `#[tyzen::command]` and `#[tauri::command]`.
31#[proc_macro_attribute]
32pub fn tauri_command(attr: TokenStream, item: TokenStream) -> TokenStream {
33    command_attr::tauri_command(attr, item)
34}
35
36/// Marks a struct as a Tyzen Event.
37/// 
38/// Used to synchronize events emitted from Tauri to the frontend.
39#[proc_macro_attribute]
40pub fn event(attr: TokenStream, item: TokenStream) -> TokenStream {
41    event_attr::event(attr, item)
42}
43
44/// Derives the `TsEvent` trait for a struct.
45/// 
46/// This allows the struct to be used with the `emit` utility on the frontend.
47#[proc_macro_derive(Event, attributes(tyzen, event))]
48pub fn derive_event(item: TokenStream) -> TokenStream {
49    event_attr::derive_event(item)
50}
51
52/// Derives the `TsType` trait and registers metadata for TypeScript generation.
53/// 
54/// Supports both structs and enums. Correctly handles `serde` attributes
55/// and Tyzen-specific metadata like `#[tyzen(optional)]` or `#[tyzen(apply = Template)]`.
56#[proc_macro_derive(Type, attributes(tyzen, serde))]
57pub fn derive_type(item: TokenStream) -> TokenStream {
58    type_derive::derive_type(item)
59}
60
61/// Exports a constant value to TypeScript.
62/// 
63/// Useful for sharing configuration or magic numbers between the backend and frontend.
64/// 
65/// # Example
66/// ```rust
67/// #[tyzen::export]
68/// pub const MAX_RETRIES: u32 = 5;
69/// ```
70#[proc_macro_attribute]
71pub fn export(attr: TokenStream, item: TokenStream) -> TokenStream {
72    export_attr::export(attr, item)
73}