1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! # typewriter-macros
//!
//! Proc macro crate for the typewriter type sync SDK.
//! Provides `#[derive(TypeWriter)]` with `#[sync_to(...)]` and `#[tw(...)]` attributes.
//!
//! ## Overview
//!
//! This crate implements the `#[derive(TypeWriter)]` proc macro that generates
//! type definitions in multiple target languages from a single Rust struct or enum definition.
//!
//! ## Supported Attributes
//!
//! ### `#[sync_to(...)]`
//!
//! Specifies which target languages to generate types for:
//!
//! ```rust,ignore
//! #[derive(TypeWriter)]
//! #[sync_to(typescript, python, go)] // Generate TS, Python, and Go types
//! #[sync_to(typescript)] // TypeScript only
//! #[sync_to(typescript, python, go, swift, kotlin, graphql, json_schema)] // All languages
//! pub struct MyType { ... }
//! ```
//!
//! **Supported languages:**
//! - `typescript` / `ts` - TypeScript interfaces and Zod schemas
//! - `python` / `py` - Python Pydantic models
//! - `go` / `golang` - Go structs with JSON tags
//! - `swift` - Swift Codable structs
//! - `kotlin` / `kt` - Kotlin data classes
//! - `graphql` / `gql` - GraphQL SDL types
//! - `json_schema` / `jsonschema` - JSON Schema definitions
//!
//! ### `#[tw(...)]`
//!
//! Fine-tune the generated output per-type or per-field:
//!
//! | Attribute | Description |
//! |-----------|-------------|
//! | `#[tw(skip)]` | Exclude field from generated output |
//! | `#[tw(rename = "name")]` | Override field/variant name in output |
//! | `#[tw(optional)]` | Force field to be optional |
//! | `#[tw(type = "custom")]` | Override the generated type string |
//! | `#[tw(zod)]` | Enable Zod schema generation (TypeScript only) |
//! | `#[tw(zod = false)]` | Disable Zod schema generation (TypeScript only) |
//!
//! ## Example
//!
//! ```rust,ignore
//! use typebridge::TypeWriter;
//! use serde::{Serialize, Deserialize};
//!
//! /// A user profile with all supported features.
//! #[derive(Serialize, Deserialize, TypeWriter)]
//! #[sync_to(typescript, python)]
//! #[tw(zod)] // Enable Zod schema generation
//! pub struct UserProfile {
//! pub id: Uuid,
//!
//! /// User's email address
//! pub email: String,
//!
//! #[tw(skip)] // Not included in generated types
//! pub password_hash: String,
//!
//! #[tw(rename = "displayName")] // Renamed in output
//! pub username: String,
//!
//! pub age: Option<u32>,
//! }
//! ```
//!
//! This generates:
//! - `./generated/typescript/user-profile.ts` - TypeScript interface
//! - `./generated/typescript/user-profile.schema.ts` - Zod schema
//! - `./generated/python/user_profile.py` - Python Pydantic model
//!
//! ## Build-Time Behavior
//!
//! Type files are generated during `cargo build`. The macro:
//! 1. Parses the annotated struct/enum
//! 2. Reads `typewriter.toml` for configuration (if present)
//! 3. Generates type definitions for each target language
//! 4. Writes files to the configured output directories
use TokenStream;
use PathBuf;
/// Derive macro for typewriter type synchronization.
///
/// This macro generates type definitions in target languages from Rust structs and enums.
///
/// # Usage
///
/// ```rust,ignore
/// use typebridge::TypeWriter;
///
/// #[derive(TypeWriter)]
/// #[sync_to(typescript, python)]
/// pub struct UserProfile {
/// pub id: Uuid,
/// pub email: String,
/// pub age: Option<u32>,
/// }
/// ```
///
/// # Errors
///
/// The macro will produce a compile error if:
/// - `#[sync_to(...)]` is missing (required)
/// - An unsupported language is specified
/// - The type is a union (not supported)
///
/// # Output
///
/// On successful compilation, type files are generated:
/// - TypeScript: `generated/typescript/<type-name>.ts` (+ `.schema.ts` for Zod)
/// - Python: `generated/python/<type_name>.py`
/// - Go: `generated/go/<type_name>.go`
/// - And more for other target languages