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
#![doc = r#"The macros here should auto generate several traits and the major TildeAble trait.

For example:

```rust
#[derive(Debug, PartialEq, TildeAble)]
pub enum TildeKind {
    /// ~C ~:C
    #[implTo(char)]
    Char,

    /// ~$ ~5$ ~f
    #[implTo(float)]
    Float(Option<String>),

    /// ~d ~:d ~:@d
    Digit(Option<String>),

    /// ~a
    #[implTo(float, char, String)]
    Va,

    /// loop
    Loop(Vec<Tilde>),

    /// text inside the tilde
    Text(String),

    /// vec
    VecTilde(Vec<Tilde>),
}
```

Will generate:

```rust
/// all default method is return none.
trait TildeAble {
    fn len(&self) -> usize;
    fn into_tildekind_char(&self) -> Option<&dyn TildeKindChar>{None}
    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa>{None}
    // and all other fields...
}

impl TildeAble for char {
    fn into_tildekind_char(&self) -> Option<&dyn TildeKindChar> {
        Some(self)
    }

    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa> {
        Some(self)
    }
}

impl TildeAble for float {
    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa> {
        Some(self)
    }
}

impl TildeAble for String {
    fn into_tildekind_va(&self) -> Option<&dyn TildeKindVa> {
        Some(self)
    }
}

trait TildeKindChar {
    fn format(&self, tkind: &TildeKind) -> Result<Option<String>, Box<dyn std::error::Error>> {
        Err("un-implenmented yet".into())
    }
}

trait TildeKindVa {
    fn format(&self, tkind: &TildeKind) -> Result<Option<String>, Box<dyn std::error::Error>> {
        Err("un-implenmented yet".into())
    }
}

```
"#]

use std::{collections::HashMap, error::Error};

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::{quote, ToTokens};
use syn::{
    buffer::{Cursor, TokenBuffer},
    parse::{self, Parser},
    parse_macro_input, parse_quote,
    punctuated::Punctuated,
    spanned::Spanned,
    Attribute, Data, DataEnum, DeriveInput, Expr, Token, Variant,
};

#[proc_macro_derive(TildeAble, attributes(implTo))]
pub fn derive_tilde_able(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    //let mut all_methods_headers = vec![];
    let mut return_types_traits = vec![];
    let mut all_default_methods = vec![];
    let mut types_impl_methods = HashMap::new();

    match input.data {
        Data::Enum(DataEnum { ref variants, .. }) => {
            let all_vars = variants.iter().map(|var| parse_variant_attrs(var));

            all_vars.for_each(|(field, tys)| {
                let fname = Ident::new(
                    &(String::from("into_tildekind_") + &field.to_lowercase()),
                    Span::call_site(),
                );

                let return_type =
                    Ident::new(&(String::from("TildeKind") + &field), Span::call_site());

                // add default methods to TildeAble
                all_default_methods
                    .push(quote! {
						fn #fname(&self) -> Option<&dyn #return_type> {
							None
						}});

                // impl for types
                tys.for_each(|ty| {
                    let en = types_impl_methods.entry(ty).or_insert(vec![]);
                    en.push(quote! {fn #fname(&self) -> Option<&dyn #return_type> {
						Some(self)
					}})
                });

                //
                return_types_traits.push(quote! {
                    pub trait #return_type: Debug {
                        fn format(&self, tkind: &TildeKind) -> Result<Option<String>, Box<dyn std::error::Error>> {
                            Err("un-implenmented yet".into())
                        }
                }})
            });
        }
        _ => panic!("only support the enum"),
    };

    let mut result = vec![];

    // trait TildeAble defination
    let tilde_able_trait = quote! {
        pub trait TildeAble:Debug {
            fn len(&self) -> usize;
            #(#all_default_methods)*
        }
    };

    let mut auto_impl_for_types = types_impl_methods
        .iter()
        .map(|(ty, methods)| {
            quote! {
                impl TildeAble for #ty {
                    fn len(&self) -> usize {
                        1
                    }
                    #(#methods)*
                }
            }
        })
        .collect();

    // merge together
    result.push(tilde_able_trait);
    result.append(&mut auto_impl_for_types);
    result.append(&mut return_types_traits);

    proc_macro2::TokenStream::from_iter(result.into_iter()).into()
}

/// return the field Ident and all types implTo. Empty if there is no implTo types
fn parse_variant_attrs(variant: &Variant) -> (String, impl Iterator<Item = Ident> + '_) {
    let all_impl_to_type = variant
        .attrs
        .iter()
        .filter(|attr| attr.path().get_ident().map(|d| d.to_string()) == Some("implTo".to_string()))
        .map(|attr| get_types_impl_to(attr).unwrap())
        .flatten();

    let field = variant.ident.to_string();

    (field.clone(), all_impl_to_type)
}

/// parse the `implTo` attribute
fn get_types_impl_to(attribute: &Attribute) -> Result<impl Iterator<Item = Ident>, Box<dyn Error>> {
    let mut result = vec![];
    attribute.parse_nested_meta(|meta| {
        result.push(
            meta.path
                .get_ident()
                .ok_or(syn::Error::new(meta.path.span(), "get_ident issue"))?
                .clone(),
        );
        Ok(())
    });

    Ok(result.into_iter())
}

///////////////////////
///////////////////////
///////////////////////

// abandon, use the one in cl-format
#[proc_macro]
pub fn cl_format(tokens: TokenStream) -> TokenStream {
    let items = Punctuated::<Expr, Token![,]>::parse_terminated
        .parse(tokens)
        .unwrap();

    //dbg!(&items);

    let mut items = items.pairs();

    let cs = match items.next() {
        Some(cs) => match cs.value() {
            Expr::Lit(l) => match &l.lit {
                syn::Lit::Str(s) => {
                    //dbg!(s.value());
                    let ss = s.value();
                    quote! {let cs = control_str::ControlStr::from(#ss).unwrap();}
                }
                _ => panic!("the first arg have to be &str"),
            },
            Expr::Path(syn::ExprPath { attrs, qself, path }) => {
                let pp = path
                    .get_ident()
                    .unwrap_or_else(|| panic!("path get ident failed"));
                quote! {let cs = control_str::ControlStr::from(#pp).unwrap();}
            }
            Expr::Reference(er) => match er.expr.as_ref() {
                Expr::Path(syn::ExprPath { attrs, qself, path }) => {
                    let pp = path
                        .get_ident()
                        .unwrap_or_else(|| panic!("path get ident failed"));
                    quote! {let cs = control_str::ControlStr::from(&#pp).unwrap();}
                }
                _ => panic!("the first arg have to be &str"),
            },
            _ => panic!("the first arg have to be &str"),
        },
        None => return proc_macro2::TokenStream::new().into(),
    };

    //dbg!(cs.to_string());
    //dbg!(items.len());
    let args = args_picker(items);
    //dbg!(args.to_string());

    let q = quote! {{
        #cs
        let args = #args;
        cs.reveal(args)
    }};
    //println!("result: \n{}", q.to_string());
    q.into()
}

// abandon, use the one in cl-format
fn args_picker(mut pairs: syn::punctuated::Pairs<Expr, Token![,]>) -> proc_macro2::TokenStream {
    let mut result = vec![];
    loop {
        match pairs.next() {
            Some(a) => match a.value() {
                Expr::Path(syn::ExprPath { attrs, qself, path }) => {
                    let pp = path
                        .get_ident()
                        .unwrap_or_else(|| panic!("path get ident failed"));
                    result.push(quote! {#pp as &dyn tildes::TildeAble})
                }
                Expr::Reference(er) => match er.expr.as_ref() {
                    Expr::Path(syn::ExprPath { attrs, qself, path }) => {
                        let pp = path
                            .get_ident()
                            .unwrap_or_else(|| panic!("path get ident failed"));
                        result.push(quote! {&#pp as &dyn tildes::TildeAble})
                    }
                    Expr::Lit(l) => {
                        let x = match &l.lit {
                            syn::Lit::Str(x) => x.to_token_stream(),
                            syn::Lit::ByteStr(x) => x.to_token_stream(),
                            syn::Lit::Byte(x) => x.to_token_stream(),
                            syn::Lit::Char(x) => x.to_token_stream(),
                            syn::Lit::Int(x) => x.to_token_stream(),
                            syn::Lit::Float(x) => x.to_token_stream(),
                            syn::Lit::Bool(x) => x.to_token_stream(),
                            syn::Lit::Verbatim(x) => x.to_token_stream(),
                            _ => unreachable!(),
                        };

                        result.push(quote! {&#x as &dyn tildes::TildeAble})
                    }
                    _ => panic!("unsupport"),
                },
                // temporary value lifetime issue
                // Expr::Array(a) => {
                //     let a = args_picker(a.elems.pairs());
                //     result.push(quote! {&#a as &dyn tildes::TildeAble})
                // }
                _ => panic!("only accept Path, Referance, and Array"),
            },
            None => {
                return quote! {
                    Into::<tildes::Args<'_>>::into([
                        #(
                            #result,
                        )*
                    ])
                };
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;
    use proc_macro2::TokenStream;
    use quote::quote;
    use syn::{parse::Parser, parse2, parse_quote, Variant};

    #[test]
    fn test_get_types_impl_to() -> Result<(), Box<dyn Error>> {
        let test_case: Attribute = parse_quote! {
                #[implTo(a,b,c,d)]
        };

        //dbg!(test_case);
        assert_eq!(
            vec!["a", "b", "c", "d"]
                .into_iter()
                .map(|s| s.to_string())
                .collect::<Vec<String>>(),
            get_types_impl_to(&test_case)
                .unwrap()
                .into_iter()
                .map(|x| x.to_string())
                .collect::<Vec<String>>()
        );

        let test_case: Attribute = parse_quote! {
                #[implTo(a)]
        };

        //dbg!(test_case);
        assert_eq!(
            vec!["a"]
                .into_iter()
                .map(|s| s.to_string())
                .collect::<Vec<String>>(),
            get_types_impl_to(&test_case)
                .unwrap()
                .into_iter()
                .map(|x| x.to_string())
                .collect::<Vec<String>>()
        );

        Ok(())
    }

    #[test]
    fn test_parse_variant_attrs() -> Result<(), Box<dyn Error>> {
        let test_case: Variant = parse_quote! {
            #[implTo(a,b,c,d)]
            A
        };

        //dbg!(test_case);
        let result = parse_variant_attrs(&test_case);
        assert_eq!(result.0, "A");
        assert_eq!(
            result.1.map(|i| i.to_string()).collect::<Vec<_>>(),
            vec!["a", "b", "c", "d"]
                .into_iter()
                .map(|s| s.to_string())
                .collect::<Vec<String>>(),
        );

        //
        let test_case: Variant = parse_quote! {
            B
        };

        //dbg!(&test_case);
        let mut result = parse_variant_attrs(&test_case);
        assert_eq!(result.0, "B");
        assert_eq!(result.1.next(), None);

        Ok(())
    }

    #[test]
    fn test_args_picker() -> Result<(), Box<dyn Error>> {
        //let s: syn::Expr = syn::parse_str("a!(a1, &a2, a3)")?;
        //let s: Punctuated<Expr, Token![,]> = syn::parse_str("a!(a1, &a2, a3)")?;
        // let s: TokenStream = "a1, &a2, a3, [[&3]]".parse().unwrap();
        // let items = Punctuated::<Expr, Token![,]>::parse_terminated
        //     .parse(s.into())
        //     .unwrap();
        // dbg!(items);

        Ok(())
    }
}