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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! # json2struct: Compile-Time Struct Generation
//!
//! A powerful procedural macro for generating Rust structs from JSON-like structures
//! with extensive compile-time type safety and configuration options.
//!
//! ## Features
//!
//! - Automatic struct generation from JSON-like syntax
//! - Flexible type inference
//! - Serde integration
//! - Compile-time type checking
//! - Multiple configuration flags
//!
//! ## Basic Usage
//!
//! ```rust
//! // Simple struct generation
//! json2struct!(User {
//! "first_name" => "John",
//! "last_name" => "Doe",
//! "age" => 30
//! });
//! ```
//!
//! ### Output
//! ```rust
//! #[derive(Clone, Deserialize, Serialize)]
//! struct User {
//! #[serde(alias = "first_name")]
//! first_name: String,
//!
//! #[serde(alias = "last_name")]
//! last_name: String,
//!
//! #[serde(alias = "age")]
//! age: f64
//! }
//! ```
//!
//! ## Example with Flags
//!
//! ```rust
//! // Complex struct with multiple configurations
//! json2struct!(Company @debug @camel @derive(PartialEq) @store_json {
//! "company_name" => "Acme Corp",
//! "employees" => [
//! {
//! "id" => 1,
//! "details" => {
//! "email" => "john@example.com",
//! "department" => "Engineering"
//! }
//! }
//! ]
//! });
//! ```
//!
//! ### Output
//!
//! ```rust
//!
//!
//! static COMPANY_JSON_VALUE: LazyLock<Value> = LazyLock::new(||
//!
//! {
//! ::serde_json::from_str(
//! "{\"company_name\":\"Acme Corp\",\"employees\":[{\"details\":{\"department\":\"Engineering\",\"email\":\"john@example.
//! com\"},\"id\":1.0}]}",
//! )
//! .expect("Couldn't convert the text into valid json")
//! });
//!
//! #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
//! #[serde(rename_all = "camelCase")]
//! struct Company {
//! #[serde(alias = "company_name")]
//! company_name: String,
//!
//! #[serde(alias = "last_name")]
//! employees: CompanyEmplyees
//! }
//!
//! #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
//! #[serde(rename_all = "camelCase")]
//! struct CompanyEmplyees {
//! #[serde(alias = "id")]
//! id: f64,
//!
//! #[serde(alias = "details")]
//! details:
//! }
//!
//! #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
//! #[serde(rename_all = "camelCase")]
//! struct CompanyEmplyeesDetails {
//! #[serde(alias = "email")]
//! email: String,
//!
//! #[serde(alias = "department")]
//! department: String
//! }
//!
//! ```
//!
//! ## Supported Flags
//!
//! | Flag | Description | Example |
//! |-----------------|-----------------------------------------------|-------------------------------|
//! | `@debug` | Adds Debug derive | `@debug` |
//! | `@snake` | Renames fields to snake_case | `@snake` |
//! | `@camel` | Renames fields to camelCase | `@camel` |
//! | `@pascal` | Renames fields to pascal | `@pascal` |
//! | `@derive(Type)` | Adds custom derives | `@derive(PartialEq, Clone)` |
//! | `@store_json` | Generates a static JSON Value constant | `@store_json` |
//!
extern crate proc_macro;
use TokenStream;
use ;
use parse_macro_input;
/// json2struct: Generates Rust structs from JSON-like structures
///
/// # Macro Syntax
///
/// ```rust
/// json2struct!(StructName [flags] {
/// "key" => value,
/// ...
/// })
/// ```
///
/// # Supported Value Types
/// - Strings: `"value"`
/// - Numbers: `42`, `3.14`
/// - Booleans: `true`, `false`
/// - Null: `null`
/// - Objects: `{ ... }`
/// - Arrays: `[ ... ]`
///
/// # Examples
///
/// Basic Struct:
/// ```rust
/// json2struct!(User {
/// "name" => "John",
/// "age" => 30
/// });
/// ```
///
/// Nested Struct:
/// ```rust
/// json2struct!(Company @debug {
/// "name" => "Acme",
/// "address" => {
/// "street" => "123 Main St",
/// "city" => "Anytown"
/// }
/// });
/// ```
///
/// # Performance
/// - Zero-cost abstraction
/// - Compile-time struct generation
/// - No runtime overhead
///
/// # Errors
/// Compilation will fail if:
/// - Invalid JSON structure
/// - Unsupported types
/// - Conflicting flags