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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! A helper macros implementation to build a string that represents struct fields path at compile time.
//!
//! Library provides a tiny macro implementation to reference Rust struct fields at compile time to represent its string format.
//! This is needed to work with JSON paths, and some others protocols when we still want to rely on the compiler to avoid inconsistent changes.
//!
//! Features:
//! - Fast and no macro parsing without huge deps;
//! - Macro produces the code to verify if the specified path really exists;
//! - Multiple fields/arrays support
//! - Optional camelCase and PascalCase conversion support;
//! - Optional delimiter parameter;
//! Example:
//!
//! ```rust,no_run
//! use struct_path::*;
//!
//! fn example() {
//!
//! pub struct TestStructParent {
//!     pub value_str: String,
//!     pub value_num: u64,
//!     pub value_child: TestStructChild,
//! }
//!
//! pub struct TestStructChild {
//!     pub child_value_str: String,
//!     pub child_value_num: u64,
//! }
//!
//!// returns "value_str"
//!let s1: &str = path!(TestStructParent::value_str);
//!
//!// returns "value_child.child_value_str"
//!let s2: &str = path!(TestStructParent::value_child.child_value_str) ;
//!
//!// returns also "value_child.child_value_str"
//!let s3: &str = path!(TestStructParent::value_child,TestStructChild::child_value_str);
//!
//!// options, returns "valueChild/childValueStr"
//!let s4: &str = path!(TestStructParent::value_child.child_value_str; delim="/", case="camel") ;
//!
//!// returns ["value_str", "value_num"]
//!let arr: [&str; 2] = paths!(TestStructParent::{ value_str, value_num });
//!
//! }
//!
//! ```
//!

use convert_case::{Case, Casing};
use proc_macro::{TokenStream, TokenTree};
use std::collections::HashMap;

#[proc_macro]
pub fn paths(struct_path_stream: TokenStream) -> TokenStream {
    let mut current_struct_name: Option<String> = None;
    let mut current_struct_fields: Vec<String> = Vec::with_capacity(16);

    let mut opened_struct = false;
    let mut colons_counter = 0;
    let mut options_opened = false;

    let mut current_field_path: Option<String> = None;

    let mut current_option_name: Option<String> = None;
    let mut expect_option_value: bool = false;

    let mut options: HashMap<String, String> = HashMap::new();
    let mut found_structs: Vec<(String, Vec<String>)> = Vec::new();

    for token_tree in struct_path_stream.into_iter() {
        match token_tree {
            TokenTree::Ident(id) if current_struct_name.is_none() => {
                current_struct_name = Some(id.to_string());
            }
            TokenTree::Punct(punct)
                if current_struct_name.is_some()
                    && !opened_struct
                    && punct == ':'
                    && colons_counter < 2 =>
            {
                colons_counter += 1;
                if colons_counter > 1 {
                    opened_struct = true;
                }
            }
            TokenTree::Ident(id) if opened_struct => {
                colons_counter = 0;
                if let Some(ref mut field_path) = &mut current_field_path {
                    field_path.push_str(id.to_string().as_str())
                } else {
                    current_field_path = Some(id.to_string());
                }
            }
            TokenTree::Punct(punct)
                if current_struct_name.is_some()
                    && opened_struct
                    && punct == ':'
                    && colons_counter < 2 =>
            {
                colons_counter += 1;
                opened_struct = false;
                if let Some(ref mut field_path) = current_field_path.take() {
                    if let Some(ref mut struct_name) = &mut current_struct_name {
                        struct_name.push_str("::");
                        struct_name.push_str(field_path);
                    }
                }
            }
            TokenTree::Punct(punct) if opened_struct && (punct == '.' || punct == '~') => {
                if let Some(ref mut field_path) = &mut current_field_path {
                    field_path.push(punct.as_char());
                } else {
                    panic!(
                        "Unexpected punctuation input for struct path group parameters: {:?}",
                        punct
                    )
                }
            }
            TokenTree::Group(group) if opened_struct && current_field_path.is_none() => {
                parse_multiple_fields(group.stream(), &mut current_struct_fields)
            }
            TokenTree::Punct(punct) if !options_opened && opened_struct && punct == ',' => {
                opened_struct = false;
                colons_counter = 0;
                if let Some(struct_name) = current_struct_name.take() {
                    if let Some(field_path) = current_field_path.take() {
                        current_struct_fields.push(field_path);
                    }
                    if !current_struct_fields.is_empty() {
                        found_structs
                            .push((struct_name, current_struct_fields.drain(..).collect()));
                    } else {
                        panic!("Unexpected comma with empty fields for {}!", struct_name);
                    }
                } else {
                    panic!("Unexpected comma with empty definitions!");
                }
            }
            TokenTree::Punct(punct) if punct == ';' && opened_struct && !options_opened => {
                options_opened = true;
                opened_struct = false;
            }
            TokenTree::Ident(id) if options_opened && !expect_option_value => {
                current_option_name = Some(id.to_string())
            }
            TokenTree::Ident(id) if options_opened && expect_option_value => {
                expect_option_value = false;
                match current_option_name.take() {
                    Some(option_name) => {
                        options.insert(option_name, id.to_string());
                    }
                    _ => {
                        panic!("Wrong options format")
                    }
                }
            }
            TokenTree::Literal(lit) if options_opened && expect_option_value => {
                expect_option_value = false;
                match current_option_name.take() {
                    Some(option_name) => {
                        let lit_str = lit.to_string();
                        options.insert(
                            option_name,
                            lit_str.as_str()[1..lit_str.len() - 1].to_string(),
                        );
                    }
                    _ => {
                        panic!("Wrong options format")
                    }
                }
            }
            TokenTree::Punct(punct) if options_opened && punct == '=' => {
                expect_option_value = true;
            }
            TokenTree::Punct(punct) if options_opened && punct == ',' => {
                expect_option_value = false;
            }
            others => {
                panic!("Unexpected input for struct path parameters: {:?}", others)
            }
        }
    }

    if let Some(field_path) = current_field_path.take() {
        current_struct_fields.push(field_path);
    }

    if let Some(struct_name) = current_struct_name.take() {
        if let Some(field_path) = current_field_path.take() {
            current_struct_fields.push(field_path);
        }
        if !current_struct_fields.is_empty() {
            found_structs.push((struct_name, current_struct_fields.drain(..).collect()));
        } else {
            panic!("Unexpected comma with empty fields for {}!", struct_name);
        }
    } else {
        panic!("Unexpected comma with empty definitions!");
    }

    let all_check_functions = generate_checks_code_for(&found_structs);

    let mut all_final_fields: Vec<String> = Vec::with_capacity(16);

    for (_, struct_fields) in &found_structs {
        for field_path in struct_fields {
            let mut final_field_path = field_path.clone().replace('~', ".");
            if !options.is_empty() {
                final_field_path = apply_options(&options, final_field_path);
            }
            all_final_fields.push(format!("\"{}\"", final_field_path))
        }
    }

    if !all_final_fields.is_empty() {
        format!(
            "{{{}\n[{}]}}",
            all_check_functions,
            all_final_fields.join(",")
        )
        .parse()
        .unwrap()
    } else {
        panic!("Empty struct fields")
    }
}

#[inline]
fn parse_multiple_fields(group_stream: TokenStream, found_struct_fields: &mut Vec<String>) {
    let mut current_field_path: Option<String> = None;

    for token_tree in group_stream.into_iter() {
        match token_tree {
            TokenTree::Ident(id) => {
                if let Some(ref mut field_path) = &mut current_field_path {
                    field_path.push_str(id.to_string().as_str())
                } else {
                    current_field_path = Some(id.to_string());
                }
            }
            TokenTree::Punct(punct) if punct == ',' => {
                if let Some(field_path) = current_field_path.take() {
                    found_struct_fields.push(field_path);
                    current_field_path = None;
                } else {
                    panic!(
                        "Unexpected punctuation input for struct path group parameters: {:?}",
                        punct
                    )
                }
            }
            TokenTree::Punct(punct) if punct == '.' => {
                if let Some(ref mut field_path) = &mut current_field_path {
                    field_path.push('.');
                } else {
                    panic!(
                        "Unexpected punctuation input for struct path group parameters: {:?}",
                        punct
                    )
                }
            }
            others => {
                panic!(
                    "Unexpected input for struct path group parameters: {:?}",
                    others
                )
            }
        }
    }

    if let Some(field_path) = current_field_path.take() {
        found_struct_fields.push(field_path);
    }
}

#[proc_macro]
pub fn path(struct_path_stream: TokenStream) -> TokenStream {
    let mut current_struct_name: Option<String> = None;

    let mut opened_struct = false;
    let mut colons_counter = 0;
    let mut options_opened = false;

    let mut current_field_path: Option<String> = None;
    let mut current_full_field_path: Option<String> = None;

    let mut current_option_name: Option<String> = None;
    let mut expect_option_value: bool = false;

    let mut options: HashMap<String, String> = HashMap::new();
    let mut found_structs: Vec<(String, Vec<String>)> = Vec::new();

    for token_tree in struct_path_stream.into_iter() {
        match token_tree {
            TokenTree::Ident(id) if current_struct_name.is_none() => {
                current_struct_name = Some(id.to_string());
            }
            TokenTree::Punct(punct)
                if current_struct_name.is_some()
                    && !opened_struct
                    && punct == ':'
                    && colons_counter < 2 =>
            {
                colons_counter += 1;
                if colons_counter > 1 {
                    opened_struct = true;
                }
            }
            TokenTree::Ident(id) if opened_struct => {
                colons_counter = 0;
                if let Some(ref mut field_path) = &mut current_field_path {
                    field_path.push_str(id.to_string().as_str())
                } else {
                    current_field_path = Some(id.to_string());
                }
            }
            TokenTree::Punct(punct)
                if current_struct_name.is_some()
                    && opened_struct
                    && punct == ':'
                    && colons_counter < 2 =>
            {
                colons_counter += 1;
                opened_struct = false;
                if let Some(ref mut field_path) = current_field_path.take() {
                    if let Some(ref mut struct_name) = &mut current_struct_name {
                        struct_name.push_str("::");
                        struct_name.push_str(field_path);
                    }
                }
            }
            TokenTree::Punct(punct) if opened_struct && (punct == '.' || punct == '~') => {
                if let Some(ref mut field_path) = &mut current_field_path {
                    field_path.push(punct.as_char());
                } else {
                    panic!(
                        "Unexpected punctuation input for struct path group parameters: {:?}",
                        punct
                    )
                }
            }
            TokenTree::Punct(punct) if !options_opened && opened_struct && punct == ',' => {
                opened_struct = false;
                colons_counter = 0;
                if let Some(struct_name) = current_struct_name.take() {
                    if let Some(field_path) = current_field_path.take() {
                        found_structs.push((struct_name, vec![field_path.clone()]));

                        if let Some(full_field_path) = &mut current_full_field_path {
                            full_field_path.push('.');
                            full_field_path.push_str(field_path.as_str());
                        } else {
                            current_full_field_path = Some(field_path)
                        }
                    } else {
                        panic!("Unexpected comma with empty fields for {}!", struct_name);
                    }
                } else {
                    panic!("Unexpected comma with empty definitions!");
                }
            }
            TokenTree::Punct(punct) if punct == ';' && opened_struct && !options_opened => {
                options_opened = true;
                opened_struct = false;
            }
            TokenTree::Ident(id) if options_opened && !expect_option_value => {
                current_option_name = Some(id.to_string())
            }
            TokenTree::Ident(id) if options_opened && expect_option_value => {
                expect_option_value = false;
                match current_option_name.take() {
                    Some(option_name) => {
                        options.insert(option_name, id.to_string());
                    }
                    _ => {
                        panic!("Wrong options format")
                    }
                }
            }
            TokenTree::Literal(lit) if options_opened && expect_option_value => {
                expect_option_value = false;
                match current_option_name.take() {
                    Some(option_name) => {
                        let lit_str = lit.to_string();
                        options.insert(
                            option_name,
                            lit_str.as_str()[1..lit_str.len() - 1].to_string(),
                        );
                    }
                    _ => {
                        panic!("Wrong options format")
                    }
                }
            }
            TokenTree::Punct(punct) if options_opened && punct == '=' => {
                expect_option_value = true;
            }
            TokenTree::Punct(punct) if options_opened && punct == ',' => {
                expect_option_value = false;
            }
            others => {
                panic!("Unexpected input for struct path parameters: {:?}", others)
            }
        }
    }

    if let Some(struct_name) = current_struct_name.take() {
        if let Some(field_path) = current_field_path.take() {
            found_structs.push((struct_name, vec![field_path.clone()]));

            if let Some(full_field_path) = &mut current_full_field_path {
                full_field_path.push('.');
                full_field_path.push_str(field_path.as_str());
            } else {
                current_full_field_path = Some(field_path)
            }
        }
    }

    if let Some(full_field_path) = current_full_field_path.take() {
        let all_check_functions = generate_checks_code_for(&found_structs);
        let final_field_path = apply_options(&options, full_field_path).replace('~', ".");
        let result_str = format!("{{{}\n\"{}\"}}", all_check_functions, final_field_path);
        result_str.parse().unwrap()
    } else {
        panic!("Unexpected empty path definition!");
    }
}

#[inline]
fn generate_checks_code_for(found_structs: &Vec<(String, Vec<String>)>) -> String {
    let mut all_check_functions = String::new();

    for (struct_name, struct_fields) in found_structs {
        let check_functions = struct_fields
            .iter()
            .map(|field_path| {
                let field_path_result = field_path.replace('~', ".iter().next().unwrap().");
                format!(
                    r#"
                {{
                    #[allow(dead_code, unused_variables)]
                    #[cold]
                    fn _check_sp(test_struct: &{}) {{
                        let _t = &test_struct.{};
                    }}
                }}
            "#,
                    struct_name, field_path_result
                )
            })
            .collect::<Vec<String>>()
            .join("\n");

        all_check_functions.push_str(&check_functions);
    }
    all_check_functions
}

#[inline]
fn apply_options(options: &HashMap<String, String>, field_path: String) -> String {
    let delim = options
        .get("delim")
        .as_ref()
        .map(|s| s.as_str())
        .unwrap_or_else(|| ".");
    let case = options.get("case");
    field_path
        .split('.')
        .map(|field_name| {
            if let Some(case_value) = case {
                match case_value.as_str() {
                    "camel" => field_name.from_case(Case::Snake).to_case(Case::Camel),
                    "pascal" => field_name.from_case(Case::Snake).to_case(Case::Pascal),
                    another => panic!("Unknown case is specified: {}", another),
                }
            } else {
                field_name.to_string()
            }
        })
        .collect::<Vec<String>>()
        .join(delim)
}