eggplant-macros 0.2.7

macros of eggplant
Documentation
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use core::panic;
use proc_macro_crate::{FoundCrate, crate_name};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{ToTokens, format_ident, quote};
use std::{
    marker::PhantomData,
    sync::{LazyLock, Mutex, MutexGuard},
};
use syn::{
    DataEnum, Expr, Fields, GenericArgument, Path, PathArguments, Type, Variant, parse::Parse,
    parse_str,
};

pub const PANIC_TY_LIST: [&'static str; 4] = ["i32", "u32", "u64", "f32"];
pub const EGGLOG_BASE_TY_LIST: [&'static str; 4] = ["String", "i64", "f64", "& 'static str"];
pub const EGGLOG_BASIC_TY_DEFAULT_LIST: [LazyTokenStream<Expr>; 4] = [
    LazyTokenStream::new(|| "String::new()".to_owned()),
    LazyTokenStream::new(|| "0".to_owned()),
    LazyTokenStream::new(|| "0.".to_owned()),
    LazyTokenStream::new(|| r#""""#.to_owned()),
];
pub struct EgglogUserDefined {
    user_defined: Mutex<UserDefined>,
}
unsafe impl Send for EgglogUserDefined {}
unsafe impl Sync for EgglogUserDefined {}
impl EgglogUserDefined {
    fn sgl() -> MutexGuard<'static, UserDefined> {
        static INSTANCE: std::sync::OnceLock<EgglogUserDefined> = std::sync::OnceLock::new();
        INSTANCE
            .get_or_init(|| -> Self {
                Self {
                    user_defined: Mutex::new(UserDefined {
                        containers: vec![],
                        base_types: vec![],
                    }),
                }
            })
            .user_defined
            .lock()
            .unwrap()
    }
    pub fn set(containers: Vec<Ident>, base_types: Vec<Ident>) {
        Self::sgl().containers = containers;
        Self::sgl().base_types = base_types;
    }
    pub fn contain_container(ty: &str) -> bool {
        Self::sgl()
            .containers
            .iter()
            .position(|x| x.to_string() == ty)
            .is_some()
    }
    pub fn contain_base_type(ty: &str) -> bool {
        Self::sgl()
            .base_types
            .iter()
            .position(|x| x.to_string() == ty)
            .is_some()
    }
}

pub struct UserDefined {
    containers: Vec<Ident>,
    base_types: Vec<Ident>,
}

pub static E: LazyTokenStream = LazyTokenStream::new(|| format!("{}::egglog", *EP.s));
pub static EP: LazyTokenStream = LazyTokenStream::new(|| eggplant_path());
// pub static DE: LazyTokenStream = LazyTokenStream::new(|| format!("{}::derive_more", *EP.s));
pub static W: LazyTokenStream = LazyTokenStream::new(|| format!("{}::wrap", *EP.s));
pub static ENM: LazyTokenStream = LazyTokenStream::new(|| format!("{}::strum_macros", *EP.s));
pub static EN: LazyTokenStream = LazyTokenStream::new(|| format!("{}::strum", *EP.s));
pub static INVE: LazyTokenStream = LazyTokenStream::new(|| format!("{}::inventory", *EP.s));
pub(crate) struct LazyTokenStream<T: Parse + ToTokens = Path> {
    s: LazyLock<String>,
    p: PhantomData<T>,
}
impl<T: Parse + ToTokens> LazyTokenStream<T> {
    pub const fn new(f: fn() -> String) -> Self {
        Self {
            s: LazyLock::new(f),
            p: PhantomData,
        }
    }
}
unsafe impl<T: Parse + ToTokens> Sync for LazyTokenStream<T> {}
unsafe impl<T: Parse + ToTokens> Send for LazyTokenStream<T> {}
impl<T: Parse + ToTokens> ToTokens for LazyTokenStream<T> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let p = parse_str(&*self.s).expect("can't parse this");
        T::to_tokens(&p, tokens);
    }
}

pub fn _egglog_path() -> String {
    match (
        crate_name("egglog"),
        std::env::var("CARGO_CRATE_NAME").as_deref(),
    ) {
        (Ok(FoundCrate::Itself), Ok(_)) => "::egglog".to_string(),
        (Ok(FoundCrate::Name(name)), _) => {
            let ident = proc_macro2::Ident::new(&name, Span::call_site());
            format!("::{}", ident)
        }
        _ => panic!("can't find egglog"),
    }
}
pub fn eggplant_path() -> String {
    match (
        crate_name("eggplant"),
        std::env::var("CARGO_CRATE_NAME").as_deref(),
    ) {
        (Ok(FoundCrate::Itself), Ok(_)) => "eggplant".to_string(),
        (Ok(FoundCrate::Name(name)), _) => {
            let ident = proc_macro2::Ident::new(&name, Span::call_site());
            format!("::{}", ident)
        }
        _ => panic!("can't find eggplant"),
    }
}
pub fn _derive_more_path() -> String {
    match (
        crate_name("derive_more"),
        std::env::var("CARGO_CRATE_NAME").as_deref(),
    ) {
        (Ok(FoundCrate::Itself), Ok(_)) => "::derive_more".to_string(),
        (Ok(FoundCrate::Name(name)), _) => {
            let ident = proc_macro2::Ident::new(&name, Span::call_site());
            format!("::{}", ident)
        }
        _ => panic!("can't find derive_more"),
    }
}
pub fn _inventory_path() -> String {
    match (
        crate_name("inventory"),
        std::env::var("CARGO_CRATE_NAME").as_deref(),
    ) {
        (Ok(FoundCrate::Itself), Ok(_)) => "::inventory".to_string(),
        (Ok(FoundCrate::Name(name)), _) => {
            let ident = proc_macro2::Ident::new(&name, Span::call_site());
            format!("::{}", ident)
        }
        _ => panic!("can't find inventory"),
    }
}

pub fn variant2mapped_ident_type_list(
    variant: &Variant,
    mut map_basic_ident: impl FnMut(&Ident, &TokenStream) -> Option<TokenStream>,
    mut map_complex_ident: impl FnMut(&Ident, &TokenStream) -> Option<TokenStream>,
) -> Vec<proc_macro2::TokenStream> {
    let types_and_idents = match &variant.fields {
        Fields::Named(fields_named) => fields_named.named.iter(),
        Fields::Unit => {
            panic!("add `{{}}` to the unit variant")
        }
        _ => panic!("only support named fields"),
    }
    .map(|f| {
        let f_ident = f
            .ident
            .as_ref()
            .expect("don't support unnamed field")
            .clone();
        // if it's a box type we should read the first generic
        if is_box_type(&f.ty) {
            (get_first_generic(&f.ty).clone(), f_ident)
        } else {
            (f.ty.clone(), f_ident)
        }
    })
    .map(|(f1, f2)| {
        let f1 = &f1.to_token_stream();
        match f1.to_string().as_str() {
            x if PANIC_TY_LIST.contains(&x) => {
                panic!("{} not supported", x)
            }
            x if BasicOrComplex::from(x).is_basic() => map_basic_ident(&f2, &f1),
            _ => map_complex_ident(&f2, &f1),
        }
        .map(|x| quote! {  #x})
    })
    .flatten()
    .collect();
    types_and_idents
}
pub fn variant2mapped_ident_type_list_detailed(
    variant: &Variant,
    mut map_ident: impl FnMut(&Ident, &TokenStream, BasicOrComplex) -> Option<TokenStream>,
) -> Vec<proc_macro2::TokenStream> {
    let types_and_idents = match &variant.fields {
        Fields::Named(fields_named) => fields_named.named.iter(),
        Fields::Unit => {
            panic!("add `{{}}` to the unit variant")
        }
        _ => panic!("only support named fields"),
    }
    .map(|f| {
        let f_ident = f
            .ident
            .as_ref()
            .expect("don't support unnamed field")
            .clone();
        // if it's a box type we should read the first generic
        if is_box_type(&f.ty) {
            (get_first_generic(&f.ty).clone(), f_ident)
        } else {
            (f.ty.clone(), f_ident)
        }
    })
    .map(|(f1, f2)| {
        let f1 = &f1.to_token_stream();
        match f1.to_string().as_str() {
            x if PANIC_TY_LIST.contains(&x) => {
                panic!("{} not supported", x)
            }
            _ => map_ident(&f2, &f1, BasicOrComplex::from(&f1.to_token_stream())),
        }
        .map(|x| quote! {  #x})
    })
    .flatten()
    .collect();
    types_and_idents
}
#[allow(unused)]
pub fn get_ref_type(ty: &Type) -> proc_macro2::TokenStream {
    match ty {
        Type::Path(type_path) => {
            let type_name = &type_path.path.segments.last().unwrap().ident;
            let sym_name = format_ident!("&{}", type_name); // concatenate `Sym`
            quote! { #sym_name }
        }
        _ => panic!("Unsupported type for `WithSymNode`"),
    }
}
pub fn is_container_type(ty: &Type) -> (bool, Ident) {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            let ty_name = segment.ident.to_string();
            match ty_name.as_str() {
                "VecContainer" => {
                    return (true, segment.ident.clone());
                }
                "SetContainer" => {
                    return (true, segment.ident.clone());
                }
                _ => {
                    panic!(
                        "{} only VecContainer and SetContainer are supported",
                        ty_name,
                    );
                }
            }
        }
    }
    panic!("you should enter some container with one generic")
}
pub fn is_box_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            if segment.ident == "Box" {
                return true;
            }
        }
    }
    false
}
/// mention there is no UserDefinedContainerTy
/// since egglog's default container is not typed and is not recommended
#[derive(strum_macros::EnumDiscriminants)]
pub enum BasicOrComplex {
    BaseType,
    /// User defined basic type, you can specify them in eggplant's attribute like `#[eggplant(base= X)]`
    UserDefinedBaseType,
    /// User defined container type, you can specify them in eggplant's attribute like `#[eggplant(container= X)]`
    UserDefinedContainerType,
    ComplexType,
}
impl BasicOrComplex {
    pub fn is_basic(&self) -> bool {
        match self {
            BasicOrComplex::BaseType => true,
            BasicOrComplex::UserDefinedBaseType => true,
            BasicOrComplex::UserDefinedContainerType => true,
            BasicOrComplex::ComplexType => false,
        }
    }
}
impl From<&proc_macro2::TokenStream> for BasicOrComplex {
    fn from(ty: &proc_macro2::TokenStream) -> Self {
        Self::from(ty.to_string().as_str())
    }
}
impl From<&str> for BasicOrComplex {
    fn from(ty: &str) -> Self {
        if EGGLOG_BASE_TY_LIST.contains(&ty) {
            return BasicOrComplex::BaseType;
        }
        if EgglogUserDefined::contain_base_type(&ty) {
            return BasicOrComplex::UserDefinedBaseType;
        }
        if EgglogUserDefined::contain_container(&ty) {
            return BasicOrComplex::UserDefinedContainerType;
        }
        BasicOrComplex::ComplexType
    }
}
pub fn get_first_generic(ty: &Type) -> &Type {
    if let Type::Path(type_path) = ty
        && let Some(segment) = type_path.path.segments.last()
        && let PathArguments::AngleBracketed(args) = &segment.arguments
    {
        let arg = args
            .args
            .iter()
            .nth(0)
            .expect("type should at least have one generic");
        if let GenericArgument::Type(inner_ty) = arg {
            // inner_ty is Vec<T>'s T
            return inner_ty;
        }
    }
    panic!("first generic generic can only be Type")
}

/// given variant a{ x:X, y:Y}
/// return vec![ x:XSym, y:YSym ]
pub fn variant2sym_typed_ident_list(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |ident, ty| Some(quote! {#ident:#ty}),
        |ident, ty| Some(quote! {#ident:#W::Sym<#ty>}),
    )
}
pub fn variants2sym_type_list(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |_ident, ty| Some(quote! {#ty}),
        |_ident, ty| Some(quote! {#W::Sym<#ty>}),
    )
}
pub fn variant2ref_node_list(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |ident, ty| Some(quote! {#ident:#ty}),
        |ident, ty| Some(quote! {#ident: &#ty<T>}),
    )
}
pub fn variant2ref_node_list_except_basic(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |_ident, _ty| None,
        |ident, ty| Some(quote! {#ident: &#ty<T>}),
    )
}
pub fn variant2valued_ref_node_list(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |ident, ty| Some(quote! {#ident:#ty}),
        |ident, ty| Some(quote! {#ident: impl #W::Insertable<#ty<(), ()>>}),
    )
}
pub fn variant2ref_node_list_without_type(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |basic, _basic_ty| Some(quote! {#basic}),
        |complex, _complex_ty| Some(quote! {#complex}),
    )
}
pub fn variant2ident_list_except_basic(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |_, _| None,
        |complex, _complex_ty| Some(quote! {#complex}),
    )
}
pub fn variant2sym_list(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |basic, basic_ty| Some(quote!(#basic:#basic_ty)),
        |complex, _| Some(quote!(#complex:#W::Sym)),
    )
}
pub fn variant2sym_list_except_basic(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |_, _| None,
        |complex, _| Some(quote!(#complex:#W::Sym)),
    )
}
pub fn variant2assign_node_field_list(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |basic, _| Some(quote! {#basic}),
        |complex, _| Some(quote!(#complex:#complex.node.sym)),
    )
}
pub fn variant2assign_node_field_typed(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |basic, _| Some(quote! {#basic}),
        |complex, _| Some(quote!(#complex:#complex.typed())),
    )
}
pub fn variant2assign_node_field_list_without_prefixed_ident(
    variant: &Variant,
) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_view_container_as_complex(
        variant,
        |basic, _| Some(quote! {#basic}),
        |complex, _| Some(quote! {#complex.node.sym}),
    )
}
pub fn variant2field_list_complex_ident_only(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list(variant, |_, _| None, |ident, _| Some(quote! {#ident}))
}

/// given variant a{ x:X, y:Y}
/// return vec![ X, Y ]
pub fn variant2tys(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list(
        variant,
        |_, ty| Some(quote! {#ty}),
        |_, ty| Some(quote! {#ty}),
    )
}

/// given variant a{ x:X, y:Y}
/// return iterator [ x, y ].iter()
pub fn variant2field_ident(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list(
        variant,
        |ident, _| Some(quote! {#ident}),
        |ident, _| Some(quote! {#ident}),
    )
}
// pub fn _variant2field_ident_with_all_default(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
//     variant2mapped_ident_type_list(
//         variant,
//         |ident, ty| {
//             Some({
//                 let default = match ty.to_string().as_str() {
//                     "String" => &EGGLOG_BASIC_TY_DEFAULT_LIST[0],
//                     "i64" => &EGGLOG_BASIC_TY_DEFAULT_LIST[1],
//                     "f64" => &EGGLOG_BASIC_TY_DEFAULT_LIST[2],
//                     _ => panic!("can't be {}", ident.to_string()),
//                 };
//                 quote! {#ident: #default}
//                 // quote! {#ident}
//             })
//         },
//         |ident, ty| Some(quote! {#ident: #W::Sym::<self::#ty>::default()}),
//     )
// }
// pub fn _variant2field_ident_with_basic_default(variant: &Variant) -> Vec<proc_macro2::TokenStream> {
//     variant2mapped_ident_type_list(
//         variant,
//         |ident, ty| {
//             Some({
//                 let default = match ty.to_string().as_str() {
//                     "String" => &EGGLOG_BASIC_TY_DEFAULT_LIST[0],
//                     "i64" => &EGGLOG_BASIC_TY_DEFAULT_LIST[1],
//                     "f64" => &EGGLOG_BASIC_TY_DEFAULT_LIST[2],
//                     _ => panic!("can't be {}", ident.to_string()),
//                 };
//                 quote! {#ident: #default}
//                 // quote! {#ident}
//             })
//         },
//         |ident, _| Some(quote! {#ident}),
//     )
// }
// pub fn variant2field_ident_assign_with_basic_default(
//     variant: &Variant,
// ) -> Vec<proc_macro2::TokenStream> {
//     variant2mapped_ident_type_list(
//         variant,
//         |ident, ty| {
//             Some({
//                 let default = get_default_by_ty(ty);
//                 quote! {#ident: #default}
//             })
//         },
//         |ident, _| Some(quote! {#ident: #ident.node.sym}),
//     )
// }
pub fn get_default_by_ty(ty: &TokenStream) -> TokenStream {
    enum Pos {
        BasePos(usize),
        UserDefined,
    }
    let pos = match (
        EGGLOG_BASE_TY_LIST
            .iter()
            .position(|x| x == &ty.to_string().as_str()),
        EgglogUserDefined::contain_base_type(&ty.to_string())
            || EgglogUserDefined::contain_container(&ty.to_string()),
    ) {
        (None, true) => Pos::UserDefined,
        (None, false) => {
            panic!(
                "{} might be a user defined base type or a typo of default base type",
                ty
            )
        }
        (Some(_), true) => panic!("user defined base type name {} conflict", ty),
        (Some(pos), false) => Pos::BasePos(pos),
    };
    match pos {
        Pos::BasePos(pos) => EGGLOG_BASIC_TY_DEFAULT_LIST[pos].to_token_stream(),
        Pos::UserDefined => quote!(Default::default()),
    }
    .to_token_stream()
}
pub fn variant2assign_node_field_typed_with_basic_default(
    variant: &Variant,
) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list(
        variant,
        |ident, ty| {
            Some({
                let default = get_default_by_ty(ty);
                quote! {#ident: #default}
            })
        },
        |ident, _| Some(quote! {#ident: #ident.typed()}),
    )
}

pub fn variant2marker_name(variant: &Variant) -> (Ident, Ident) {
    (
        format_ident!("{}Ty", variant.ident),
        format_ident!("{}", variant.ident),
    )
}
pub fn variant_marker_names(data_enum: &DataEnum) -> (Vec<Ident>, Vec<Ident>) {
    data_enum
        .variants
        .iter()
        .map(|v| variant2marker_name(v))
        .collect()
}

pub fn variant2valued_struct_fields(variant: &Variant) -> Vec<TokenStream> {
    variant2mapped_ident_type_list_detailed(variant, |ident, ty, b_or_c| {
        // println!(
        //     "{} {} is recognized as basic",
        //     ident.to_string(),
        //     ty.to_string()
        // );
        match b_or_c {
            BasicOrComplex::BaseType | BasicOrComplex::UserDefinedBaseType => {
                Some(quote! {#ident: #W::Value<#ty>})
            }
            BasicOrComplex::UserDefinedContainerType => Some(quote! {#ident: #W::Value<#ty>}),
            BasicOrComplex::ComplexType => None,
        }
    })
}

pub fn variant2mapped_ident_type_list_view_container_as_complex(
    variant: &Variant,
    mut map_basic_ident_except_container: impl FnMut(&Ident, &TokenStream) -> Option<TokenStream>,
    mut map_complex_ident_include_container: impl FnMut(&Ident, &TokenStream) -> Option<TokenStream>,
) -> Vec<proc_macro2::TokenStream> {
    variant2mapped_ident_type_list_detailed(variant, |ident, ty, b_or_c| match b_or_c {
        BasicOrComplex::BaseType | BasicOrComplex::UserDefinedBaseType => {
            map_basic_ident_except_container(ident, ty)
        }
        BasicOrComplex::UserDefinedContainerType | BasicOrComplex::ComplexType => {
            map_complex_ident_include_container(ident, ty)
        }
    })
}