tauri_ts_generator_derive/
lib.rs

1//! Derive macro for tauri-ts-generator
2//!
3//! This crate provides the `#[derive(TS)]` macro which registers the `ts` attribute
4//! namespace, allowing `#[ts(optional)]` annotations on struct fields.
5//!
6//! The derive macro itself is a no-op - it doesn't generate any runtime code.
7//! Its sole purpose is to make the Rust compiler accept `#[ts(...)]` attributes
8//! which are then parsed at code generation time by `tauri-ts-generator`.
9
10use proc_macro::TokenStream;
11use quote::quote;
12use syn::{parse_macro_input, DeriveInput};
13
14/// Derive macro that enables `#[ts(...)]` attributes on struct/enum fields.
15///
16/// This macro is a no-op at compile time. It simply registers the `ts` attribute
17/// namespace so that the Rust compiler doesn't error on `#[ts(optional)]` attributes.
18///
19/// # Example
20///
21/// ```rust
22/// use tauri_ts_generator_derive::TS;
23///
24/// #[derive(TS)]
25/// pub struct Config {
26///     // This field will be typed as `number | undefined` in TypeScript
27///     #[ts(optional)]
28///     pub volume: Option<f32>,
29///     
30///     // This field will be typed as `string | null` (default behavior)
31///     pub name: Option<String>,
32/// }
33/// ```
34#[proc_macro_derive(TS, attributes(ts))]
35pub fn derive_ts(input: TokenStream) -> TokenStream {
36    // Parse the input to validate syntax, but don't generate any code
37    let _ = parse_macro_input!(input as DeriveInput);
38    
39    // Return empty token stream - this is a no-op derive
40    TokenStream::from(quote! {})
41}