float-pigment-css-macro 0.8.2

Helper macros for float-pigment-css.
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
577
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::*;
use serde::{Deserialize, Serialize};
use std::fs::OpenOptions;
use std::io::{BufReader, Read, Write};
use std::path::{Path, PathBuf};
use syn::parse::*;
use syn::parse_macro_input;
use syn::*;

const CACHE_ROOT: &str = "compile_cache";
const EXTENSION: &str = "toml";

fn file_creator(
    folder: &str,
    name: &str,
    extension: &str,
    truncate: bool,
    write: bool,
) -> std::result::Result<std::fs::File, std::io::Error> {
    let mut path_buffer = PathBuf::new();
    path_buffer.push(std::env::var("CARGO_MANIFEST_DIR").unwrap());
    path_buffer.push(Path::new(CACHE_ROOT));
    path_buffer.push(Path::new(folder));
    path_buffer.push(Path::new(&format!("{name}.{extension}")));
    let mut options = OpenOptions::new();
    let file = options
        .read(true)
        .write(write)
        .truncate(truncate)
        .create(true)
        .open(&path_buffer);
    file
}

fn folder_checker() {
    // check folder
    if cfg!(feature = "serialize_compile_cache") {
        let mut path_buffer = PathBuf::new();
        path_buffer.push(std::env::var("CARGO_MANIFEST_DIR").unwrap());
        path_buffer.push(Path::new(CACHE_ROOT));
        path_buffer.push("struct");
        std::fs::create_dir_all(&path_buffer).unwrap_or_default();
        path_buffer.pop();
        path_buffer.push("enum");
        std::fs::create_dir(&path_buffer).unwrap_or_default();
        path_buffer.pop();
        path_buffer.push("publish");
        path_buffer.push("enum");
        std::fs::create_dir_all(&path_buffer).unwrap_or_default();
        path_buffer.pop();
        path_buffer.push("struct");
        std::fs::create_dir(&path_buffer).unwrap_or_default();
        path_buffer.pop();
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub(crate) enum EnumCheckMode {
    Full,
    Variant,
}

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct CacheItem {
    key: String,
    value: Vec<String>,
}

pub(crate) struct ParseInput {
    next_cache: CacheItem,
    name: String,
    token: TokenStream,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub(crate) enum EnumType {
    Unit,
    Named,
    Unnamed,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub(crate) struct EnumItem {
    typing: EnumType,
    ident: String,
    field: Vec<String>,
}
impl ParseInput {
    fn parse_for_enum(input: ParseStream) -> Result<Self> {
        let origin: ItemEnum = input.parse()?;
        let enum_name = origin.ident.to_string();
        let mut enum_variants = vec![];
        let mut check_variants = proc_macro2::TokenStream::new();
        for variant in origin.variants.into_iter() {
            // enum_variants.push(variant.ident.to_string());
            match &variant.fields {
                Fields::Unit => {
                    let item = EnumItem {
                        typing: EnumType::Unit,
                        ident: variant.ident.to_string(),
                        field: Vec::with_capacity(0),
                    };
                    let str = toml::to_string(&item).unwrap();
                    enum_variants.push(str)
                }
                Fields::Named(n) => {
                    let mut field = Vec::with_capacity(n.named.len());
                    for f in n.named.iter() {
                        let ty = f.ty.clone();
                        let ty_string = ty.to_token_stream().to_string();
                        let token: TokenStream = ty_string.parse().unwrap();
                        check_variants.append_all(quote! {
                            <#token as crate::CompatibilityCheck>::check();
                        });
                        field.push(ty_string);
                    }
                    let item = EnumItem {
                        typing: EnumType::Named,
                        ident: variant.ident.to_string(),
                        field,
                    };
                    let str = toml::to_string(&item).unwrap();
                    enum_variants.push(str)
                }
                Fields::Unnamed(un) => {
                    let mut field = Vec::with_capacity(un.unnamed.len());
                    for f in un.unnamed.iter() {
                        let ty = f.ty.clone();
                        let ty_string = ty.to_token_stream().to_string();
                        let token: TokenStream = ty_string.parse().unwrap();
                        check_variants.append_all(quote! {
                            <#token as crate::CompatibilityCheck>::check();
                        });
                        field.push(ty_string);
                    }
                    let item = EnumItem {
                        typing: EnumType::Unnamed,
                        ident: variant.ident.to_string(),
                        field,
                    };
                    let str = toml::to_string(&item).unwrap();
                    enum_variants.push(str)
                }
            }
        }
        let next_cache = CacheItem {
            key: enum_name.clone(),
            value: enum_variants,
        };
        let ident = origin.ident;
        let generics = origin.generics;
        let has_generics = generics.lt_token.is_some();
        let token = if has_generics {
            let generics_token: proc_macro2::TokenStream = generics
                .params
                .clone()
                .into_iter()
                .map(|p| match p {
                    GenericParam::Type(params) => {
                        let ident = params.ident;
                        quote! {
                            #ident: crate::CompatibilityCheck,
                        }
                    }
                    _ => {
                        quote! {}
                    }
                })
                .collect();
            quote! {
                impl #generics crate::CompatibilityCheck for #ident #generics
                    where #generics_token
                {
                    fn check(){
                        #check_variants
                    }
                }
            }
        } else {
            quote! {
                impl crate::CompatibilityCheck for #ident {
                    fn check(){
                        #check_variants
                    }
                }
            }
        };
        // println!("{:?}", token.to_string());
        Ok(Self {
            name: enum_name,
            next_cache,
            token,
        })
    }
    fn parse_for_struct(input: ParseStream) -> Result<Self> {
        let origin: ItemStruct = input.parse()?;
        let struct_name = origin.ident.to_string();
        let ident = origin.ident;
        let mut struct_fields = vec![];
        let generics = origin.generics;
        let mut check_field = proc_macro2::TokenStream::new();
        for field in origin.fields.iter() {
            let field_value = field.ty.to_token_stream().to_string();
            let __field = field_value.clone();
            struct_fields.push(__field.replace(' ', ""));
            let value = field_value;
            let token: TokenStream = value.parse().unwrap();
            check_field.append_all(quote! {
                <#token as crate::CompatibilityCheck>::check();
            });
        }
        let next_cache = CacheItem {
            key: struct_name.clone(),
            value: struct_fields,
        };
        // check trait
        let has_generics = generics.lt_token.is_some();
        let token = if has_generics {
            let generics_token: proc_macro2::TokenStream = generics
                .params
                .clone()
                .into_iter()
                .map(|p| match p {
                    GenericParam::Type(params) => {
                        let ident = params.ident;
                        quote! {
                            #ident: crate::CompatibilityCheck,
                        }
                    }
                    _ => {
                        quote! {}
                    }
                })
                .collect();
            quote! {
                impl #generics crate::CompatibilityCheck for #ident #generics
                    where #generics_token
                    {
                        fn check() {
                            #check_field
                        }
                    }
            }
        } else {
            quote! {
                impl crate::CompatibilityCheck for #ident {
                    fn check() {
                        #check_field
                    }
                }
            }
        };
        Ok(Self {
            name: struct_name,
            next_cache,
            token,
        })
    }
}

pub(crate) fn check_enum_inner(
    input: proc_macro::TokenStream,
    mod_name: Option<String>,
    mode: EnumCheckMode,
) -> proc_macro::TokenStream {
    folder_checker();
    let p = parse_macro_input!(input with ParseInput::parse_for_enum);
    compare_enum_cache(p, mod_name, mode)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

pub(crate) fn compare_enum_cache(
    input: ParseInput,
    mod_name: Option<String>,
    mode: EnumCheckMode,
) -> Result<TokenStream> {
    let ParseInput {
        next_cache,
        name,
        token,
    } = input;
    if mode == EnumCheckMode::Variant {
        return Ok(token);
    }
    let enum_name = match mod_name {
        Some(n) => {
            format!("{n}_{name}")
        }
        None => name,
    };
    // open or create cache
    let mut pb = PathBuf::new();
    pb.push("publish");
    pb.push("enum");
    let folder = pb.to_str().unwrap();
    let file = file_creator(folder, &enum_name, EXTENSION, false, false);
    if let Ok(file) = file {
        let mut reader = BufReader::new(&file);
        let mut string = String::new();
        reader
            .read_to_string(&mut string)
            .expect("error: read to string");
        let prev_cache = toml::from_str::<CacheItem>(&string);
        if let Ok(prev_cache) = prev_cache {
            if next_cache.value.len() < prev_cache.value.len() {
                return Err(Error::new(
                    Span::call_site(),
                    format!(
                        "[CompatibilityEnumCheck: 1000] enum {:?}, cache_variants_len: {:?}, cur_variants_len: {:?}",
                        enum_name,
                        prev_cache.value.len(),
                        next_cache.value.len()
                    )
                ));
            }
            for (idx, (prev, next)) in prev_cache
                .value
                .iter()
                .zip(next_cache.value.iter())
                .enumerate()
            {
                let next_cache_value_item = toml::from_str::<EnumItem>(next).unwrap();
                let prev_cache_value_item = toml::from_str::<EnumItem>(prev).unwrap();
                if next_cache_value_item.typing != prev_cache_value_item.typing {
                    return Err(Error::new(
                    Span::call_site(),
                    format!(
                        "[CompatibilityEnumCheck: 1001] enum {:?}, cache_variants_type: {:?}, cur_variants_type: {:?}, idx: {:?}",
                        enum_name,
                        prev_cache_value_item.typing,
                        next_cache_value_item.typing,
                        idx
                    )
                ));
                }
                let typing = next_cache_value_item.typing;
                match typing {
                    EnumType::Unit => {
                        if next_cache_value_item.ident != prev_cache_value_item.ident {
                            return Err(Error::new(
                            Span::call_site(),
                            format!(
                                "[CompatibilityEnumCheck: 1002] enum {:?}, cache_variants: {:?}, cur_variants: {:?}, idx: {:?}",
                            enum_name, prev_cache_value_item.ident, next_cache_value_item.ident, idx
                            )
                        ));
                        }
                    }
                    EnumType::Named => {
                        if next_cache_value_item.ident != prev_cache_value_item.ident {
                            return Err(Error::new(
                            Span::call_site(),
                            format!(
                                "[CompatibilityEnumCheck: 1003] enum {:?}, cache_named_variants {:?}, cur_named_variants {:?}",
                                enum_name,
                                prev_cache_value_item.ident,
                                next_cache_value_item.ident
                            )
                        ));
                        }
                        if next_cache_value_item.field.len() < prev_cache_value_item.field.len() {
                            return Err(Error::new(
                            Span::call_site(),
                            format!(
                                "[CompatibilityEnumCheck: 1004] enum {:?}, named_variants {:?}, cache_len: {:?}, cur_len: {:?}",
                            enum_name,
                            prev_cache_value_item.ident,
                            prev_cache_value_item.field.len(),
                            next_cache_value_item.field.len()
                            )
                        ));
                        }
                        for (idx, (prev, next)) in prev_cache_value_item
                            .field
                            .iter()
                            .zip(next_cache_value_item.field.iter())
                            .enumerate()
                        {
                            if next != prev {
                                return Err(Error::new(
                                        Span::call_site(),
                                        format!(
                                            "[CompatibilityEnumCheck: 1005] enum {:?}, named_variants {:?}, cache_variants_type: {:?}, cur_variants_type: {:?}, idx: {:?}",
                                            enum_name,
                                            next_cache_value_item.ident,
                                            next,
                                            prev,
                                            idx
                                        )
                                    ));
                            }
                        }
                    }
                    EnumType::Unnamed => {
                        if next_cache_value_item.ident != prev_cache_value_item.ident {
                            return Err(Error::new(
                            Span::call_site(),
                            format!(
                                "[CompatibilityEnumCheck: 1006] enum {:?}, cache_unnamed_variants {:?}, cur_unnamed_variants {:?}",
                                enum_name,
                                prev_cache_value_item.ident,
                                next_cache_value_item.ident
                            )
                        ));
                        }
                        if next_cache_value_item.field.len() != prev_cache_value_item.field.len() {
                            return Err(Error::new(
                            Span::call_site(),
                            format!(
                                "[CompatibilityEnumCheck: 1007] enum {:?}, unnamed_variants {:?}, cache_len: {:?}, cur_len: {:?}",
                                enum_name,
                                prev_cache_value_item.ident,
                                prev_cache_value_item.field.len(),
                                next_cache_value_item.field.len()
                            )
                        ));
                        }
                        for (idx, (prev, next)) in prev_cache_value_item
                            .field
                            .iter()
                            .zip(next_cache_value_item.field.iter())
                            .enumerate()
                        {
                            if next.replace(' ', "") != prev.replace(' ', "") {
                                return Err(Error::new(
                                Span::call_site(),
                                format!(
                                    "[CompatibilityEnumCheck: 1008] enum {:?}, unnamed_variants {:?}, cache_variants_type: {:?}, cur_variants_type: {:?}, idx: {:?}",
                                    enum_name,
                                    next_cache_value_item.ident,
                                    next,
                                    prev,
                                    idx
                                )
                            ));
                            }
                        }
                    }
                }
            }
        }
    }
    if cfg!(feature = "serialize_compile_cache") {
        let next_cache_toml = toml::to_string(&next_cache).unwrap();
        let mut file = file_creator("enum", &enum_name, EXTENSION, true, true).unwrap();
        file.write_all(next_cache_toml.as_bytes())
            .unwrap_or_else(|_| {
                panic!("[CompatibilityEnumCheck] {enum_name:?}.{EXTENSION:?}: write cache error")
            });
    }

    Ok(token)
}

pub(crate) fn check_enum(
    input: proc_macro::TokenStream,
    mode: EnumCheckMode,
) -> proc_macro::TokenStream {
    check_enum_inner(input, None, mode)
}

pub(crate) fn check_enum_with_mod(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let mod_name = attr.to_string();
    let t = item.clone();
    let token = check_enum_inner(t, Some(mod_name), EnumCheckMode::Full);
    let token = proc_macro2::TokenStream::from(token);
    let item: DeriveInput = syn::parse(item).unwrap();
    let ret = quote! {
        #item
        #token
    };
    proc_macro::TokenStream::from(ret)
}
pub fn check_struct_inner(
    input: proc_macro::TokenStream,
    mod_name: Option<String>,
) -> proc_macro::TokenStream {
    folder_checker();
    let p = parse_macro_input!(input with ParseInput::parse_for_struct);
    compare_struct_cache(p, mod_name)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

pub(crate) fn compare_struct_cache(
    input: ParseInput,
    mod_name: Option<String>,
) -> Result<TokenStream> {
    let ParseInput {
        next_cache,
        name,
        token,
    } = input;
    let struct_name = match mod_name {
        Some(n) => format!("{n}_{name}"),
        None => name,
    };
    // open or create cache
    let mut pb = PathBuf::new();
    pb.push("publish");
    pb.push("struct");
    let folder = pb.to_str().unwrap();
    let file = file_creator(folder, &struct_name, EXTENSION, false, false);
    if let Ok(file) = file {
        let mut reader = BufReader::new(&file);
        let mut string = String::new();
        reader
            .read_to_string(&mut string)
            .expect("error: read to string");
        let prev_cache = toml::from_str::<CacheItem>(&string);
        if let Ok(prev_cache) = prev_cache {
            if next_cache.value.len() < prev_cache.value.len() {
                return Err(Error::new(
                    Span::call_site(),
                    format!(
                        "[CompatibilityStructCheck: 2000] struct {:?}, cache_fields_len: {:?}, cur_fields_len: {:?}",
                        struct_name,
                        prev_cache.value.len(),
                        next_cache.value.len()
                    )
                ));
            }
            for (idx, (prev, next)) in prev_cache
                .value
                .iter()
                .zip(next_cache.value.iter())
                .enumerate()
            {
                if next != prev {
                    return Err(Error::new(
                        Span::call_site(),
                        format!(
                            "[CompatibilityStructCheck: 2001] struct {struct_name:?}, cache_fields: {prev:?}, cur_fields: {next:?}, idx: {idx:?}"
                        )
                    ));
                }
            }
        }
    }
    if cfg!(feature = "serialize_compile_cache") {
        let next_cache_toml = toml::to_string(&next_cache).unwrap();
        let mut file = file_creator("struct", &struct_name, EXTENSION, true, true).unwrap();
        file.write_all(next_cache_toml.as_bytes())
            .unwrap_or_else(|_| {
                panic!(
                    "[CompatibilityStructCheck] {struct_name:?}.{EXTENSION:?}: write cache error"
                )
            });
    }
    Ok(token)
}

pub(crate) fn check_struct(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    check_struct_inner(input, None)
}

pub(crate) fn check_struct_with_mod(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let mod_name = attr.to_string();
    let t = item.clone();
    let token = check_struct_inner(t, Some(mod_name));
    let token = proc_macro2::TokenStream::from(token);
    let item: DeriveInput = syn::parse(item).unwrap();
    let ret = quote! {
        #item
        #token
    };
    proc_macro::TokenStream::from(ret)
}