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