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
/// stdmd types serde_enum.rs (missing crate)
/// Derives:
/// Serialize_enum
/// Deserialize_enum
/// ToString
/// NamingStyle
///     SnakeCase
///     CamelCase
///     ScreamingSnakeCase
///
/// The clipboard processes new clips and saves to history.
/// You can add a clip to a category or folder. (All standard)
/// Pasting and saving features differentiates this crate.
/// Ideally, all of the crates reformatting tricks should be exported.
/// The serde_enum crate already contained good case conversion functionality.
/// This is a learning process for me (DaveGH).
/// I might be doing it wrong. Hmmmm.
///
/// Functions:
/// enum NamingStyle {
/// static ref NAME_MAP: HashMap<NamingStyle, fn(&str) -> String> = {
/// pub fn to_string_enum(item: TokenStream) -> TokenStream {
/// pub fn deserialize_enum(item: TokenStream) -> TokenStream {
/// pub fn serialize_enum(item: TokenStream) -> TokenStream {
/// pub fn get_naming_style(item: TokenStream) -> TokenStream {
/// pub fn to_snake_case(v: &str) -> String {
/// pub fn to_camel_case(v: &str) -> String {
/// pub fn to_screaming_snake_case(v: &str) -> String {
///
///
/// fn format_variant(v: &Variant, parent_style: NamingStyle) -> String {
/// NamingStyle
/// SnakeCase
/// fn to_snake_case_impl(v: &str) -> String {
/// CamelCase
/// fn to_camel_case_impl(v: &str) -> String {
/// ScreamingSnakeCase
/// fn to_screaming_snake_case_impl(v: &str) -> String {
/// None
/// fn get_variant_alias(v: &Variant) -> Option<String> {
/// fn get_variant_alias(v: &Variant) -> Option<String> {
/// fn get_enum_from_input(target: &DeriveInput) -> DataEnum {
///
/// fn create_ser_arms(target: &DataEnum, n: NamingStyle) -> impl Iterator<Item = TokenStream2> {
/// fn create_to_str_arms(target: &DataEnum, n: NamingStyle) -> impl Iterator<Item = TokenStream2> {
/// fn create_de_arms(target: &DataEnum, n: NamingStyle) -> impl Iterator<Item = TokenStream2> {
///

// serde_enum macros
use std::collections::HashMap;
// use syn::*;
use syn::{self, parse_quote, Arm, Data};
use syn::{Attribute, DataEnum, DeriveInput, Expr, ExprLit, ExprParen, Fields, Lit, parse2, parse_macro_input, Variant}; // path, tokens, Token,


// ! Crates -------------------------------------------------------
// NamingStyle and NAME_MAP
// serde_enum
// use std::collections::HashMap;
// use syn::*;
// use syn::{Attribute, DataEnum, DeriveInput, Expr, ExprLit, ExprParen, Fields, Lit, parse2, parse_macro_input, Variant}; // path, tokens, Token,
#[derive(Debug, derive_more::Display, Clone, Copy, PartialEq, Eq, Hash)]
enum NamingStyle {
    SnakeCase,
    CamelCase,
    ScreamingSnakeCase,
    None,
}
// ! NAME_MAP -------------------------------------------------------
lazy_static! {
    /// doc todo create_ser_arms
static ref NAME_MAP: HashMap<NamingStyle, fn(&str) -> String> = {
        let mut m = HashMap::new();

        // I have no actual idea why this is working like that, and why I need this cast
        m.insert(NamingStyle::SnakeCase, to_snake_case_impl as fn(&str) -> String);
        m.insert(NamingStyle::CamelCase, to_camel_case_impl);
        m.insert(NamingStyle::ScreamingSnakeCase, to_screaming_snake_case_impl);
        m
    };
}
// ! Serialization -------------------------------------------------------
// You need to mark your macro with #[macro_export] and then mark the module with #[macro_use]:
// #[proc_macro].
// This doesn't apply to proc_macro_derive. #[allow(unused_macros)] needed for libraries.
// ! (serde) Serialization -------------------------------------------------------
/// doc todo create_ser_arms
#[allow(unused_macros)]
// #[macro_export]
#[proc_macro_derive(Deserialize_enum, attributes(serde))]
pub fn deserialize_enum(item: TokenStream) -> TokenStream {
    let target = parse_macro_input!(item as DeriveInput);
    let data = get_enum_from_input(&target);

    let style = get_naming_style_impl(target.attrs.iter());

    let target_ident = &target.ident;
    let de_arms = create_de_arms(&data, style);
    let out = quote! {
        impl<'de> serde::Deserialize<'de> for #target_ident {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>
            {
                Ok(
                    match <&str>::deserialize(deserializer)? {
                        #(#de_arms),*,
                        _ => { unimplemented!() }
                    }
                )
            }
        }
    };
    out.into()
}
/// doc todo create_ser_arms
#[allow(unused_macros)]
// #[macro_export]
#[proc_macro_derive(Serialize_enum, attributes(serde))]
pub fn serialize_enum(item: TokenStream) -> TokenStream {
    let target = parse_macro_input!(item as DeriveInput);
    let data = get_enum_from_input(&target);

    let style = get_naming_style_impl(target.attrs.iter());

    let target_ident = &target.ident;
    let ser_arms = create_ser_arms(&data, style);
    let out = quote! {
        impl serde::Serialize for #target_ident {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer
            {
                match self {
                    #(#ser_arms),*
                }
            }
        }
    };
    out.into()
}
// ! Naming Conversion macros -------------------------------------------------------
// todo move Naming Conversion macros to separate crate.
/// doc todo ToString to_string_enum
#[allow(unused_macros)]
// #[macro_export]
#[proc_macro_derive(ToString)]
pub fn to_string_enum(item: TokenStream) -> TokenStream {
    let target = parse_macro_input!(item as DeriveInput);
    let data = get_enum_from_input(&target); // validates

    let ident = &target.ident;

    let style = get_naming_style_impl(target.attrs.iter());

    let to_str_arms = create_to_str_arms(&data, style);

    let out = quote! {
        impl std::convert::From<&#ident> for &'static str {
            fn from(v: &#ident) -> &'static str {
                match v {
                    #(#ident::#to_str_arms),*
                }
            }
        }

        impl std::string::ToString for #ident {
            fn to_string(&self) -> String {
                <&#ident as std::convert::Into<&'static str>>::into(self).to_string()
            }
        }
    };
    out.into()
}
/// doc todo NamingStyle get_naming_style
#[allow(unused_macros)]
#[proc_macro_derive(NamingStyle)]
pub fn get_naming_style(item: TokenStream) -> TokenStream {
    let target = parse_macro_input!(item as DeriveInput);
    let naming_style = get_naming_style_impl(target.attrs.iter());
    // out.into()
    // out.to_string();
    let s = naming_style.to_string();
    let stream: proc_macro::TokenStream = s.parse().unwrap();
    stream
    // .parse().expect("valid style")
    // let out = quote! {
    //     impl<'de> serde::Deserialize<'de> for #target_ident {
    //         fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    //         where
    //             D: serde::Deserializer<'de>
    //         {
    //             Ok(
    //                 match <&str>::deserialize(deserializer)? {
    //                     #(#de_arms),*,
    //                     _ => { unimplemented!() }
    //                 }
    //             )
    //         }
    //     }
    // };
    // out.into()
}
/// doc todo SnakeCase
// #[proc_macro_derive(SnakeCase)]
// pub fn to_snake_case(v: &str) -> String {
#[allow(unused_macros)]
macro_rules! SnakeCase {
    ($value:ident) => {
        #[doc = "SnakeCase macro"]
        to_snake_case_impl($value)
    };
}
/// doc todo CamelCase to_camel_case_impl
// #[proc_macro_derive(CamelCase)]
// pub fn to_camel_case(v: &str) -> String {
#[allow(unused_macros)]
macro_rules! CamelCase {
    ($value:expr) => {
        #[doc = "SnakeCase macro"]
        to_camel_case_impl($value)
    };
}
/// doc todo ScreamingSnakeCase to_screaming_snake_case_impl
// #[proc_macro_derive(ScreamingSnakeCase)]
// pub fn to_screaming_snake_case(v: &str) -> String {
#[allow(unused_macros)]
macro_rules! ScreamingSnakeCase {
    ($value:expr) => {
        #[doc = "ScreamingSnakeCase macro"]
        to_screaming_snake_case_impl($value)
    };
}
// ! Naming Style functions -------------------------------------------------------
// ! Format functions -------------------------------------------------------
/// Attribute to NamingStyle conversion
/// doc todo get_naming_style_impl
fn get_naming_style_impl<'a>(target: impl Iterator<Item = &'a Attribute>) -> NamingStyle {
    // a is an Attribute
    for a in target {
        // A a.path at which a named item is exported (e.g. std::collections::HashMap).
        // Attribute.meta is Syn:Meta and also has a path
        // Attribute.path() exists. ( was a.path.get_ident() )
        // get_ident() - A word of Rust code, which may be a keyword or legal variable name.
        if let Some(i) = a.path().get_ident() {
            // So serde only
            if i == "serde" {
                // if let Ok(ExprParen { expr, .. }) = parse2::<ExprParen>(a.tokens.clone()) {
                // parse2 - syn
                // pub fn parse2<T>(tokens: proc_macro2::TokenStream) -> Result<T>
                // where
                //     T: parse::Parse,
                // I'm assuming it wants the bracket_token here - []
                // maypbe pound_token instead
                // if let Ok(ExprParen { expr, .. }) = parse2::<ExprParen>(Token![a.bracket_token] ) {
                // TokenStream::new(Vec<a>)
                if let Ok(ExprParen { expr, .. }) = parse2::<ExprParen>(a.parse_args().unwrap()) {
                    // ea Expression Assign
                    if let Expr::Assign(ea) = expr.as_ref() {
                        // ep - unknown meaning here:
                        // two functions are introduced here:
                        // .left.as_ref()
                        // .right.as_ref()
                        if let Expr::Path(ep) = ea.left.as_ref() {
                            // ?
                            if let Some(i) = ep.path.get_ident() {
                                // Attributes:
                                if i == "rename" || i == "rename_all" {
                                    // capture litteral into s as a str?
                                    if let Expr::Lit(ExprLit {
                                        lit: Lit::Str(s), ..
                                    }) = ea.right.as_ref()
                                    {
                                        return match s.value().as_str() {
                                            "snake_case" => NamingStyle::SnakeCase,
                                            "camelCase" => NamingStyle::CamelCase,
                                            "SCREAMING_SNAKE_CASE" => {
                                                NamingStyle::ScreamingSnakeCase
                                            }
                                            _ => {
                                                panic!(
                                                    "Unsupported style. \
                                                    Available: `snake_case`, `camelCase`"
                                                )
                                            }
                                        };
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    NamingStyle::None
}
/// Variant to String conversion
/// doc todo format_variant
fn format_variant(v: &Variant, parent_style: NamingStyle) -> String {
    if let Some(s) = get_variant_alias(v) {
        return s;
    }

    let own_style = get_naming_style_impl(v.attrs.iter());

    match own_style {
        NamingStyle::None => match parent_style {
            NamingStyle::None => v.ident.to_string(),
            ps => NAME_MAP.get(&ps).unwrap()(&v.ident.to_string()),
        },
        os => NAME_MAP.get(&os).unwrap()(&v.ident.to_string()),
    }
}
/// SnakeCase conversion
/// doc todo to_snake_case_impl
fn to_snake_case_impl(v: &str) -> String {
    let mut out = String::with_capacity(v.len());
    if v.is_empty() {
        out.push(v.chars().next().unwrap().to_ascii_lowercase());
    }

    for c in v.chars().skip(1) {
        if c.is_uppercase() {
            out.push('_');
            out.push(c.to_ascii_lowercase());
        } else {
            out.push(c);
        }
    }

    out
}
/// CamelCase conversion
fn to_camel_case_impl(v: &str) -> String {
    v.to_string()
        .char_indices()
        .map(|(i, c)| if i == 0 { c.to_ascii_lowercase() } else { c })
        .collect()
}
/// SCREAMING_SNAKE_CASE conversion
/// doc todo to_screaming_snake_case_impl
fn to_screaming_snake_case_impl(v: &str) -> String {
    v.char_indices()
        .fold(String::with_capacity(v.len()), |mut s, (i, c)| {
            if c.is_uppercase() && i != 0 {
                s.push('_');
            }
            s.push(c.to_ascii_uppercase());
            s
        })
}
// ! Variants -------------------------------------------------------
/// Takes a Variant, checks for the _ident, if "serde", if the path _ident is "name" return the s Expr::Lit
/// Variant Attributes ident("serde"), ExprParen, parse_args, ExprAssign, ExprPath, ident, ExprLit
fn get_variant_alias(v: &Variant) -> Option<String> {
    for a in v.attrs.iter() {
        if let Some(i) = a.path().get_ident() {
            if i == "serde" {
                if let Ok(ExprParen { expr, .. }) = parse2::<ExprParen>(a.parse_args().unwrap()) {
                    // if let Ok(ExprParen { expr, .. }) = parse2::<ExprParen>(a.tokens.clone()) {
                    if let Expr::Assign(ea) = expr.as_ref() {
                        if let Expr::Path(ep) = ea.left.as_ref() {
                            if let Some(i) = ep.path.get_ident() {
                                if i == "name" {
                                    if let Expr::Lit(ExprLit {
                                        lit: Lit::Str(s), ..
                                    }) = ea.right.as_ref()
                                    {
                                        return Some(s.value());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    None
}
/// Checks that the passed input's .data is an Enum and clones the ref
fn get_enum_from_input(target: &DeriveInput) -> DataEnum {
    if !target.generics.params.is_empty() {
        panic!("`Serialize_enum` target cannot have any generics parameters!");
    }

    if let Data::Enum(ref e) = target.data {
        e.clone()
    } else {
        panic!("`Serialize_enum` can only be applied to enums!");
    }
}
// ! Create arms -------------------------------------------------------
/// doc todo create_ser_arms
fn create_ser_arms(target: &DataEnum, n: NamingStyle) -> impl Iterator<Item = TokenStream2> {
    target.variants.clone().into_iter().map(move |v| {
        assert!(matches!(v.fields, Fields::Unit));
        let ident = &v.ident;
        let value = format_variant(&v, n);

        quote! {
            Self::#ident => { serializer.serialize_str(#value) }
        }
    })
}
/// doc todo create_to_str_arms
fn create_to_str_arms(target: &DataEnum, n: NamingStyle) -> impl Iterator<Item = TokenStream2> {
    target.variants.clone().into_iter().map(move |v| {
        let ident = &v.ident;
        let value = format_variant(&v, n);

        quote! {
            #ident => #value
        }
    })
}
/// doc todo create_de_arms
fn create_de_arms(target: &DataEnum, n: NamingStyle) -> impl Iterator<Item = TokenStream2> {
    target.variants.clone().into_iter().map(move |v| {
        assert!(matches!(v.fields, Fields::Unit));

        let ident = &v.ident;
        let value = format_variant(&v, n);

        quote! {
            #value => Self::#ident
        }
    })
}