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
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_error::{abort, proc_macro_error};
use quote::quote;
use syn::{
    AngleBracketedGenericArguments,
    AttrStyle::Outer,
    Attribute,
    Expr::Lit,
    ExprLit, Field,
    Fields::Named,
    GenericArgument,
    Lit::Str,
    Meta::{List, NameValue},
    MetaList, MetaNameValue, PathArguments, PathSegment, Type, TypePath,
};

#[derive(Debug)]
enum DefaultSource {
    DefaultValue(String),
    DefaultFn(Option<String>),
    #[allow(dead_code)]
    SerdeDefaultFn(String),
}

#[derive(PartialEq)]
enum NestingType {
    None,
    Vec,
    Dict,
}

#[derive(PartialEq)]
enum NestingFormat {
    Section(NestingType),
    Prefix,
}

fn default_value(ty: String) -> String {
    match ty.as_str() {
        "usize" | "u8" | "u16" | "u32" | "u64" | "u128" | "isize" | "i8" | "i16" | "i32"
        | "i64" | "i128" => "0",
        "f32" | "f64" => "0.0",
        _ => "\"\"",
    }
    .to_string()
}

/// return type without Option, Vec
fn parse_type(
    ty: &Type,
    default: &mut String,
    optional: &mut bool,
    nesting_format: &mut Option<NestingFormat>,
) -> Option<String> {
    let mut r#type = None;
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(PathSegment { ident, arguments }) = path.segments.last() {
            let id = ident.to_string();
            if arguments.is_none() {
                r#type = Some(id.clone());
                *default = default_value(id);
            } else if id == "Option" {
                *optional = true;
                if let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
                    args, ..
                }) = arguments
                {
                    if let Some(GenericArgument::Type(ty)) = args.first() {
                        r#type = parse_type(ty, default, &mut false, nesting_format);
                    }
                }
            } else if id == "Vec" {
                if nesting_format.is_some() {
                    *nesting_format = Some(NestingFormat::Section(NestingType::Vec));
                }
                if let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
                    args, ..
                }) = arguments
                {
                    if let Some(GenericArgument::Type(ty)) = args.first() {
                        let mut item_default_value = String::new();
                        r#type = parse_type(ty, &mut item_default_value, &mut false, &mut None);
                        *default = if item_default_value.is_empty() {
                            "[  ]".to_string()
                        } else {
                            format!("[ {item_default_value:}, ]")
                        }
                    }
                }
            } else if id == "HashMap" || id == "BTreeMap" {
                if let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
                    args, ..
                }) = arguments
                {
                    if let Some(GenericArgument::Type(ty)) = args.last() {
                        let mut item_default_value = String::new();
                        r#type = parse_type(ty, &mut item_default_value, &mut false, &mut None);
                    }
                }
                if nesting_format.is_some() {
                    *nesting_format = Some(NestingFormat::Section(NestingType::Dict));
                }
            }
            // TODO else Complex struct in else
        }
    }
    r#type
}

/// return (doc, default, nesting, require, skip)
fn parse_attrs(
    attrs: &[Attribute],
) -> (
    Vec<String>,
    Option<DefaultSource>,
    Option<NestingFormat>,
    bool,
    bool,
) {
    let mut docs = Vec::new();
    let mut default_source = None;
    let mut nesting_format = None;
    let mut require = false;
    let mut skip = false;
    for attr in attrs.iter() {
        match (attr.style, &attr.meta) {
            (Outer, NameValue(MetaNameValue { path, value, .. })) => {
                for seg in path.segments.iter() {
                    if seg.ident == "doc" {
                        if let Lit(ExprLit {
                            lit: Str(lit_str), ..
                        }) = value
                        {
                            docs.push(lit_str.value());
                        }
                    }
                }
            }
            (
                Outer,
                List(MetaList {
                    path,
                    tokens: _tokens,
                    ..
                }),
            ) if path
                .segments
                .last()
                .map(|s| s.ident == "serde")
                .unwrap_or_default() =>
            {
                #[cfg(feature = "serde")]
                {
                    let token_str = _tokens.to_string();
                    if token_str.starts_with("default") {
                        if let Some((_, s)) = token_str.split_once('=') {
                            default_source = Some(DefaultSource::SerdeDefaultFn(
                                s.trim().trim_matches('"').into(),
                            ));
                        } else {
                            default_source = Some(DefaultSource::DefaultFn(None));
                        }
                    }
                    if token_str == "skip_deserializing" || token_str == "skip" {
                        skip = true;
                    }
                }
            }
            (Outer, List(MetaList { path, tokens, .. }))
                if path
                    .segments
                    .last()
                    .map(|s| s.ident == "toml_example")
                    .unwrap_or_default() =>
            {
                let token_str = tokens.to_string();
                if token_str.starts_with("default") {
                    if let Some((_, s)) = token_str.split_once('=') {
                        default_source = Some(DefaultSource::DefaultValue(s.trim().into()));
                    } else {
                        default_source = Some(DefaultSource::DefaultFn(None));
                    }
                } else if token_str.starts_with("nesting") {
                    if let Some((_, s)) = token_str.split_once('=') {
                        nesting_format = match s.trim() {
                            "prefix" => Some(NestingFormat::Prefix),
                            "section" => Some(NestingFormat::Section(NestingType::None)),
                            _ => abort!(&attr, "please use prefix or section for nesting derive"),
                        }
                    } else {
                        nesting_format = Some(NestingFormat::Section(NestingType::None));
                    }
                } else if token_str == "require" {
                    require = true;
                } else if token_str == "skip" {
                    skip = true;
                } else {
                    abort!(&attr, format!("{} is not allowed attribute", token_str))
                }
            }
            _ => (),
        }
    }
    (docs, default_source, nesting_format, require, skip)
}

fn parse_field(
    field: &Field,
) -> (
    DefaultSource,
    Vec<String>,
    bool,
    Option<NestingFormat>,
    bool,
) {
    let mut default_value = String::new();
    let mut optional = false;
    let (docs, default_source, mut nesting_format, require, skip) = parse_attrs(&field.attrs);
    let ty = parse_type(
        &field.ty,
        &mut default_value,
        &mut optional,
        &mut nesting_format,
    );
    let default_source = match default_source {
        Some(DefaultSource::DefaultFn(_)) => DefaultSource::DefaultFn(ty),
        Some(DefaultSource::SerdeDefaultFn(f)) => DefaultSource::SerdeDefaultFn(f),
        Some(DefaultSource::DefaultValue(v)) => DefaultSource::DefaultValue(v),
        _ => DefaultSource::DefaultValue(default_value),
    };
    (
        default_source,
        docs,
        optional && !require,
        nesting_format,
        skip,
    )
}

fn push_doc_string(example: &mut String, docs: Vec<String>) {
    for doc in docs.into_iter() {
        example.push('#');
        example.push_str(&doc);
        example.push('\n');
    }
}

fn default_key(default: DefaultSource) -> String {
    if let DefaultSource::DefaultValue(v) = default {
        let key = v.trim_matches('\"').replace(' ', "").replace('.', "-");
        if !key.is_empty() {
            return key;
        }
    }
    "example".into()
}

#[proc_macro_derive(TomlExample, attributes(toml_example))]
#[proc_macro_error]
pub fn derive_patch(item: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(item as syn::DeriveInput);
    let struct_name = &input.ident;
    let mut struct_doc = "r#\"".to_string();
    let mut field_example = "r#\"".to_string();
    push_doc_string(&mut struct_doc, parse_attrs(&input.attrs).0);

    let fields = if let syn::Data::Struct(syn::DataStruct { fields, .. }) = &input.data {
        fields
    } else {
        abort!(&input.ident, "TomlExample derive only use for struct")
    };
    if let Named(fields_named) = fields {
        for f in fields_named.named.iter() {
            let field_type = parse_type(&f.ty, &mut String::new(), &mut false, &mut None);
            if let Some(field_name) = f.ident.as_ref().map(|i| i.to_string()) {
                let (default, doc_str, optional, nesting_format, skip) = parse_field(f);
                if skip {
                    continue;
                }
                push_doc_string(&mut field_example, doc_str);

                if nesting_format
                    .as_ref()
                    .map(|f| matches!(f, NestingFormat::Section(_)))
                    .unwrap_or_default()
                {
                    if let Some(field_type) = field_type {
                        field_example.push_str("\"#.to_string()");
                        let key = default_key(default);
                        match nesting_format {
                            Some(NestingFormat::Section(NestingType::Vec)) if optional => field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"# [[{field_name:}]]\n\", \"# \")"
                            )),
                            Some(NestingFormat::Section(NestingType::Vec)) => field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"[[{field_name:}]]\n\", \"\")"
                            )),
                            Some(NestingFormat::Section(NestingType::Dict)) if optional => field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"# [{field_name:}.{key}]\n\", \"# \")"
                            )),
                            Some(NestingFormat::Section(NestingType::Dict)) => field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"[{field_name:}.{key}]\n\", \"\")"
                            )),
                            _ if optional => field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"# [{field_name:}]\n\", \"# \")"
                            )),
                            _ => field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"[{field_name:}]\n\", \"\")"
                            ))
                        };
                        field_example.push_str(" + &r#\"");
                    } else {
                        abort!(&f.ident, "nesting only work on inner structure")
                    }
                } else if nesting_format == Some(NestingFormat::Prefix) {
                    if let Some(field_type) = field_type {
                        field_example.push_str("\"#.to_string()");
                        if optional {
                            field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"\", \"# {field_name:}.\")"
                            ));
                        } else {
                            field_example.push_str(&format!(
                                " + &{field_type}::toml_field_example(\"\", \"{field_name:}.\")"
                            ));
                        }
                        field_example.push_str(" + &r#\"");
                    } else {
                        abort!(&f.ident, "nesting only work on inner structure")
                    }
                } else {
                    if optional {
                        field_example.push_str("# ");
                    }
                    match default {
                        DefaultSource::DefaultValue(default) => {
                            field_example.push_str("\"#.to_string() + prefix + &r#\"");
                            field_example.push_str(field_name.trim_start_matches("r#"));
                            field_example.push_str(" = ");
                            field_example.push_str(&default);
                            field_example.push('\n');
                        }
                        DefaultSource::DefaultFn(None) => {
                            field_example.push_str("\"#.to_string() + prefix + &r#\"");
                            field_example.push_str(&field_name);
                            field_example.push_str(" = \"\"\n");
                        }
                        DefaultSource::DefaultFn(Some(ty)) => {
                            field_example.push_str("\"#.to_string() + prefix + &r#\"");
                            field_example.push_str(&field_name);
                            field_example.push_str(" = \"#.to_string()");
                            field_example
                                .push_str(&format!(" + &format!(\"{{:?}}\",  {ty}::default())"));
                            field_example.push_str(" + &r#\"\n");
                        }
                        DefaultSource::SerdeDefaultFn(fn_str) => {
                            field_example.push_str("\"#.to_string() + prefix + &r#\"");
                            field_example.push_str(&field_name);
                            field_example.push_str(" = \"#.to_string()");
                            field_example
                                .push_str(&format!(" + &format!(\"{{:?}}\",  {fn_str}())"));
                            field_example.push_str("+ &r#\"\n");
                        }
                    }
                    field_example.push('\n');
                }
            }
        }
    }
    struct_doc.push_str("\"#.to_string()");
    field_example.push_str("\"#.to_string()");

    let struct_doc_stream: proc_macro2::TokenStream =
        struct_doc.parse().expect("unexpected token in struct doc");
    let field_example_stream: proc_macro2::TokenStream =
        field_example.parse().expect("unexpected token in fields");

    let output = quote! {
        impl toml_example::TomlExample for #struct_name {
            fn toml_example() -> String {
                #struct_name::toml_field_example("", "")
            }
            fn toml_field_example(label: &str, prefix: &str) -> String {
                #struct_doc_stream + label + &#field_example_stream
            }
        }
    };
    TokenStream::from(output)
}