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
//! 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 ["value_str", "value_num"]
//! let arr: [&str; 2] = path!(TestStructParent::{ value_str, value_num });
//!
//! // options, returns "valueChild/childValueStr"
//! let s2: &str = path!(TestStructParent::value_child.child_value_str; delim='/', case="camel") ;
//!
//! }
//!
//! ```
//!

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

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

    let mut opened_struct = false;
    let mut colons_counter = 0;
    let mut expected_multiple_fields = false;
    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();

    for token_tree in struct_path_stream.into_iter() {
        match token_tree {
            TokenTree::Ident(id) if found_struct_name.is_none() => {
                found_struct_name = Some(id.to_string());
                options_opened = false;
            }
            TokenTree::Punct(punct)
                if found_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 found_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 found_struct_name {
                        struct_name.push_str("::");
                        struct_name.push_str(field_path);
                    }
                }
            }
            TokenTree::Punct(punct) if opened_struct && 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
                    )
                }
            }
            TokenTree::Group(group) if opened_struct && current_field_path.is_none() => {
                expected_multiple_fields = true;
                parse_multiple_fields(group.stream(), &mut found_struct_fields)
            }
            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() {
        found_struct_fields.push(field_path);
    }

    if let Some(struct_name) = found_struct_name {
        let check_functions = found_struct_fields
            .iter()
            .map(|field_path| {
                format!(
                    r#"
                {{
                    #[allow(dead_code, unused_variables)]
                    fn _test_struct_field(test_struct: &{}) {{
                        let _t = &test_struct.{};
                    }}
                }}
            "#,
                    struct_name, field_path
                )
            })
            .collect::<Vec<String>>()
            .join("\n");

        found_struct_fields = found_struct_fields
            .iter()
            .map(|field_path| {
                let mut final_field_path = field_path.clone();
                if !options.is_empty() {
                    final_field_path = apply_options(&options, final_field_path);
                }
                format!("\"{}\"", final_field_path)
            })
            .collect();

        let result_str = if expected_multiple_fields {
            format!(
                "{{{}\n[{}]}}",
                check_functions,
                found_struct_fields.join(",")
            )
        } else if let Some(field_path) = found_struct_fields.pop() {
            format!("{{{}\n{}}}", check_functions, field_path)
        } else {
            panic!("Empty struct fields")
        };
        result_str.parse().unwrap()
    } else {
        panic!("No structure name is specified")
    }
}

#[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);
    }
}

#[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)
}