csv-schema-validator-derive 0.2.0

Procedural macro for deriving CSV schema validation
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
// csv-schema-validator-derive/src/lib.rs
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{
    parse_macro_input, Data, DeriveInput, Fields, GenericArgument, Ident,
    PathArguments, Type,
};

// Armazena validações por campo
struct FieldValidation {
    field_name: Ident,
    is_option: bool, // [FIX] passamos a carregar se o campo é Option<T>
    validations: Vec<Validation>,
    core_ty_ts: proc_macro2::TokenStream, // v.0.1.3: tipo efetivo (T de Option<T> ou o próprio) para codegen
}

// Tipos de validações suportadas
enum Validation {
    Range { min: Option<String>, max: Option<String>, is_float: bool }, // v.0.1.3
    Regex { regex: String },
    Required,
    Custom { path: syn::Path },
    Length { min: usize, max: usize },
    NotBlank,
    OneOf { values: Vec<String> },
    NotIn { values: Vec<String> },
    IfThen { conditional_column: String, conditional_value: String, expected_value: String }, // v 0.2.0 
}

impl Validation {
    /// Faz o parse de #[validate(...)] em uma lista de Validation
    fn parse_validations(input: syn::parse::ParseStream) -> syn::Result<Vec<Self>> {
        let mut out = Vec::new();
        let meta_items = syn::punctuated::Punctuated::<syn::Meta, syn::Token![,]>::parse_terminated(input)?;
        for meta in meta_items {
            match meta {
                syn::Meta::Path(path) => Self::parse_meta_path(path, &mut out)?,
                syn::Meta::NameValue(mnv) => Self::parse_meta_name_value(mnv, &mut out)?,
                syn::Meta::List(list) => Self::parse_meta_list(list, &mut out)?,
            }
        }
        Ok(out)
    }

    // ---------- Dispatchers por variante de Meta ----------

    fn parse_meta_path(path: syn::Path, out: &mut Vec<Self>) -> syn::Result<()> {
        if path.is_ident("required") {
            out.push(Validation::Required);
        } else if path.is_ident("not_blank") {
            out.push(Validation::NotBlank);
        }
        Ok(())
    }

    fn parse_meta_name_value(mnv: syn::MetaNameValue, out: &mut Vec<Self>) -> syn::Result<()> {
        if mnv.path.is_ident("regex") {
            let s = Self::expect_lit_str(&mnv.value, "Expected string literal for `regex`")?;
            out.push(Validation::Regex { regex: s });
            Ok(())
        } else if mnv.path.is_ident("custom") {
            let s = Self::expect_lit_str(&mnv.value, "Expected string literal for `custom` (e.g., custom = \"path::to::fn\")")?;
            let path: syn::Path = syn::parse_str(&s).map_err(|e| syn::Error::new_spanned(&mnv.value, e))?;
            out.push(Validation::Custom { path });
            Ok(())
        } else {
            Err(syn::Error::new_spanned(mnv, "chave desconhecida em atributo"))
        }
    }

    fn parse_meta_list(list: syn::MetaList, out: &mut Vec<Self>) -> syn::Result<()> {
        let ident = &list.path;
        if ident.is_ident("length") {
            Self::parse_length_list(list, out)
        } else if ident.is_ident("range") {
            Self::parse_range_list(list, out)
        } else if ident.is_ident("one_of") {
            Self::parse_one_of_list(list, out)
        } else if ident.is_ident("not_in") {
            Self::parse_not_in_list(list, out)
        } else if ident.is_ident("if_then") {
            Self::parse_if_then_list(list, out)
        } else {
            Ok(())
        }
    }

    // ---------- Handlers específicos ----------

    fn parse_if_then_list(list: syn::MetaList, out: &mut Vec<Self>) -> syn::Result<()> {
        use syn::{LitStr, Token};
        use syn::punctuated::Punctuated;

        // Espera exatamente 3 literais string
        let args = list.parse_args_with(Punctuated::<LitStr, Token![,]>::parse_terminated)?;
        if args.len() != 3 {
            return Err(syn::Error::new_spanned(
                list,
                "if_then espera exatamente 3 strings: (conditional_column, conditional_value, expected_value)",
            ));
        }

        let conditional_column = args[0].value();
        let conditional_value  = args[1].value();
        let expected_value     = args[2].value();

        out.push(Self::IfThen { conditional_column, conditional_value, expected_value });
        Ok(())
    }

    fn parse_length_list(list: syn::MetaList, out: &mut Vec<Self>) -> syn::Result<()> {
        let items: syn::punctuated::Punctuated<syn::MetaNameValue, syn::Token![,]> =
            list.parse_args_with(syn::punctuated::Punctuated::parse_terminated)?;
        let mut min: Option<usize> = None;
        let mut max: Option<usize> = None;

        for kv in items {
            if kv.path.is_ident("min") {
                let v = Self::expect_lit_int(&kv.value, "`min` for `length` must be an integer literal")?;
                min = Some(v);
            } else if kv.path.is_ident("max") {
                let v = Self::expect_lit_int(&kv.value, "`max` for `length` must be an integer literal")?;
                max = Some(v);
            }
        }

        if min.is_none() && max.is_none() {
            return Err(syn::Error::new_spanned(list, "`length` requires at least one of `min` or `max`"));
        }
        if let Some(mx) = max {
            if mx == 0 {
                return Err(syn::Error::new_spanned(list, "`max` for `length` cannot be zero"));
            }
        }
        if let (Some(a), Some(b)) = (min, max) {
            if a > b {
                return Err(syn::Error::new_spanned(list, "`min` must be <= `max` for `length`"));
            }
        }

        out.push(Validation::Length {
            min: min.unwrap_or(0),
            max: max.unwrap_or(usize::MAX),
        });
        Ok(())
    }

    fn parse_range_list(list: syn::MetaList, out: &mut Vec<Self>) -> syn::Result<()> {
        let items: syn::punctuated::Punctuated<syn::MetaNameValue, syn::Token![,]> =
            list.parse_args_with(syn::punctuated::Punctuated::parse_terminated)?;
        let mut min: Option<String> = None;
        let mut max: Option<String> = None;
        let mut min_is_float = false;
        let mut max_is_float = false;

        for kv in items {
            let (slot_val, slot_is_float) = if kv.path.is_ident("min") {
                (&mut min, &mut min_is_float)
            } else if kv.path.is_ident("max") {
                (&mut max, &mut max_is_float)
            } else {
                continue;
            };

            match &kv.value {
                // inteiros positivos
                syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(i), .. }) => {
                    *slot_val = Some(i.to_string());
                    *slot_is_float = false;
                }
                // floats positivos
                syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Float(f), .. }) => {
                    *slot_val = Some(f.to_string());
                    *slot_is_float = true;
                }
                // negativos: -<int> ou -<float>
                syn::Expr::Unary(syn::ExprUnary { op: syn::UnOp::Neg(_), expr, .. }) => {
                    match &**expr {
                        syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(i), .. }) => {
                            *slot_val = Some(format!("-{}", i.to_string()));
                            *slot_is_float = false;
                        }
                        syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Float(f), .. }) => {
                            *slot_val = Some(format!("-{}", f.to_string()));
                            *slot_is_float = true;
                        }
                        _ => {
                            return Err(syn::Error::new_spanned(
                                &kv.value,
                                "`range` values must be numeric literals (int or float)",
                            ));
                        }
                    }
                }
                _ => {
                    return Err(syn::Error::new_spanned(
                        &kv.value,
                        "`range` values must be numeric literals (int or float)",
                    ));
                }
            }
        }

        if min.is_none() && max.is_none() {
            return Err(syn::Error::new_spanned(
                &list,
                "`range` requires at least one of `min` or `max`",
            ));
        }

        // Se ambos existem, precisam ser do mesmo "kind" (ambos int ou ambos float)
        if min.is_some() && max.is_some() && (min_is_float != max_is_float) {
            return Err(syn::Error::new_spanned(
                &list,
                "`range` `min` and `max` must be of the same type (both int or both float)",
            ));
        }

        // Checagem min <= max quando ambos existem
        if let (Some(ref a), Some(ref b)) = (&min, &max) {
            if min_is_float {
                // float
                let av: f64 = a.parse().map_err(|_| syn::Error::new_spanned(&list, "`range` float literal parse error"))?;
                let bv: f64 = b.parse().map_err(|_| syn::Error::new_spanned(&list, "`range` float literal parse error"))?;
                if av > bv {
                    return Err(syn::Error::new_spanned(&list, "`range` `min` must be <= `max`"));
                }
            } else {
                // inteiro (suporta sinal)
                let av: i128 = a.parse().map_err(|_| syn::Error::new_spanned(&list, "`range` int literal parse error"))?;
                let bv: i128 = b.parse().map_err(|_| syn::Error::new_spanned(&list, "`range` int literal parse error"))?;
                if av > bv {
                    return Err(syn::Error::new_spanned(&list, "`range` `min` must be <= `max`"));
                }
            }
        }

        let is_float = min_is_float || max_is_float;

        out.push(Validation::Range { min, max, is_float });
        Ok(())
    }

    fn parse_one_of_list(list: syn::MetaList, out: &mut Vec<Self>) -> syn::Result<()> {
        let exprs: syn::punctuated::Punctuated<syn::Expr, syn::Token![,]> =
            list.parse_args_with(syn::punctuated::Punctuated::parse_terminated)?;
        let mut values = Vec::new();
        for expr in exprs {
            let s = Self::expect_lit_str_expr(expr, "`one_of` only accepts string literals")?;
            values.push(s);
        }
        if values.is_empty() {
            return Err(syn::Error::new_spanned(list, "`one_of` requires at least one value"));
        }
        out.push(Validation::OneOf { values });
        Ok(())
    }

    fn parse_not_in_list(list: syn::MetaList, out: &mut Vec<Self>) -> syn::Result<()> {
        let exprs: syn::punctuated::Punctuated<syn::Expr, syn::Token![,]> =
            list.parse_args_with(syn::punctuated::Punctuated::parse_terminated)?;
        let mut values = Vec::new();
        for expr in exprs {
            let s = Self::expect_lit_str_expr(expr, "`not_in` only accepts string literals")?;
            values.push(s);
        }
        if values.is_empty() {
            return Err(syn::Error::new_spanned(list, "`not_in` requires at least one value"));
        }
        out.push(Validation::NotIn { values });
        Ok(())
    }

    // ---------- UTILITÁRIOS GERAIS ----------

    fn type_name_of(ty: &Type) -> String {
        if let Type::Path(tp) = ty {
            tp.path.segments.last().map(|s| s.ident.to_string()).unwrap_or_default()
        } else { String::new() }
    }

    fn is_int_ty(n: &str) -> bool {
        matches!(n, "i8"|"i16"|"i32"|"i64"|"i128"|"isize"|
                    "u8"|"u16"|"u32"|"u64"|"u128"|"usize")
    }

    fn is_float_ty(n: &str) -> bool {
        matches!(n, "f32"|"f64")
    }

    // ---------- FUNÇÕES AUXILIARES DE VALIDAÇÃO ----------

    fn validate_if_then_for_field(
        v: &[Validation],
        field: &syn::Field,
        field_name: &syn::Ident,
        is_option: bool,
        fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>,
    ) -> Result<(), proc_macro::TokenStream> {
        // Procura um if_then neste campo (se houver vários, valida o primeiro; repita se quiser todos)
        let Some((conditional_column, conditional_value, expected_value)) =
            v.iter().find_map(|vv| {
                if let Validation::IfThen { conditional_column, conditional_value, expected_value } = vv {
                    Some((conditional_column.as_str(), conditional_value.as_str(), expected_value.as_str()))
                } else {
                    None
                }
            })
        else {
            return Ok(());
        };
        

        // 1) Campo alvo deve ser Option<_>
        if !is_option {
            return Err(
                syn::Error::new_spanned(
                    &field.ty,
                    format!("`if_then` só pode ser usado em campos Option<T> (campo `{}`)", field_name)
                ).to_compile_error().into()
            );
        }

        // 2) Coluna condicional: existe e é Option<_>
        let Some(cond_field) = fields.iter().find(|f|
            f.ident.as_ref().map(|i| i.to_string() == conditional_column).unwrap_or(false)        
        ) else {
            return Err(
                syn::Error::new_spanned(
                    &field.ty,
                    format!("`if_then`: campo condicional `{}` não existe na struct", conditional_column)
                ).to_compile_error().into()
            );
        };

        let Some(cond_core_ty) = option_inner_type(&cond_field.ty) else {
            return Err(
                syn::Error::new_spanned(
                    &cond_field.ty,
                    format!("`if_then`: campo condicional `{}` deve ser Option<U>", conditional_column)
                ).to_compile_error().into()
            );
        };

        // Tipos base
        let cond_ty_name   = Self::type_name_of(cond_core_ty);                          // T
        let Some(target_core_ty) = option_inner_type(&field.ty) else { unreachable!() }; // R
        let target_ty_name = Self::type_name_of(target_core_ty);                         // R

        // 3) Compatibilidade de literais (checagem leve)
        if cond_ty_name != "String" {
            if Self::is_int_ty(&cond_ty_name) {
                if conditional_value.parse::<i128>().is_err() && conditional_value.parse::<u128>().is_err() {
                    return Err(
                        syn::Error::new_spanned(
                            &field.ty,
                            format!("`if_then`: `conditional_value`='{}' inválido para tipo {}", conditional_value, cond_ty_name)
                        ).to_compile_error().into()
                    );
                }
            } else if Self::is_float_ty(&cond_ty_name) {
                if conditional_value.parse::<f64>().is_err() {
                    return Err(
                        syn::Error::new_spanned(
                            &field.ty,
                            format!("`if_then`: `conditional_value`='{}' inválido para tipo {}", conditional_value, cond_ty_name)
                        ).to_compile_error().into()
                    );
                }
            } else if cond_ty_name == "bool" {
                if conditional_value.parse::<bool>().is_err() {
                    return Err(
                        syn::Error::new_spanned(
                            &field.ty,
                            format!("`if_then`: `conditional_value`='{}' inválido para tipo bool (use 'true' ou 'false')", conditional_value)
                        ).to_compile_error().into()
                    );
                }
            } else {
                return Err(
                    syn::Error::new_spanned(
                        &field.ty,
                        format!("`if_then`: tipo condicional `{}` não suportado; use String ou numérico", cond_ty_name)
                    ).to_compile_error().into()
                );
            }
        }

        if target_ty_name != "String" {
            if Self::is_int_ty(&target_ty_name) {
                if expected_value.parse::<i128>().is_err() && expected_value.parse::<u128>().is_err() {
                    return Err(
                        syn::Error::new_spanned(
                            &field.ty,
                            format!("`if_then`: `expected_value`='{}' inválido para tipo {}", expected_value, target_ty_name)
                        ).to_compile_error().into()
                    );
                }
            } else if Self::is_float_ty(&target_ty_name) {
                if expected_value.parse::<f64>().is_err() {
                    return Err(
                        syn::Error::new_spanned(
                            &field.ty,
                            format!("`if_then`: `expected_value`='{}' inválido para tipo {}", expected_value, target_ty_name)
                        ).to_compile_error().into()
                    );
                }
            } else if target_ty_name == "bool" {
                if expected_value.parse::<bool>().is_err() {
                    return Err(
                        syn::Error::new_spanned(
                            &field.ty,
                            format!("`if_then`: `expected_value`='{}' inválido para tipo bool (use 'true' ou 'false')", expected_value)
                        ).to_compile_error().into()
                    );
                }
            } else {
                return Err(
                    syn::Error::new_spanned(
                        &field.ty,
                        format!("`if_then`: tipo do campo `{}` não suportado; use String ou numérico", target_ty_name)
                    ).to_compile_error().into()
                );
            }
        }

        Ok(())
    }


    // ---------- Utilitários de extração ----------

    fn expect_lit_str(expr: &syn::Expr, msg: &str) -> syn::Result<String> {
        if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(s), .. }) = expr {
            Ok(s.value())
        } else {
            Err(syn::Error::new_spanned(expr, msg))
        }
    }

    fn expect_lit_int(expr: &syn::Expr, msg: &str) -> syn::Result<usize> {
        if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(i), .. }) = expr {
            i.base10_parse::<usize>().map_err(|e| syn::Error::new_spanned(expr, e))
        } else {
            Err(syn::Error::new_spanned(expr, msg))
        }
    }

    fn expect_lit_str_expr(expr: syn::Expr, msg: &str) -> syn::Result<String> {
        if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(s), .. }) = expr {
            Ok(s.value())
        } else {
            Err(syn::Error::new_spanned(expr, msg))
        }
    }
}


// [FIX] helper para detectar Option<T>
fn option_inner_type(ty: &Type) -> Option<&Type> {
    if let Type::Path(tp) = ty {
        if let Some(seg) = tp.path.segments.last() {
            if seg.ident == "Option" {
                if let PathArguments::AngleBracketed(args) = &seg.arguments {
                    if let Some(GenericArgument::Type(inner_ty)) = args.args.first() {
                        return Some(inner_ty);
                    }
                }
            }
        }
    }
    None
}

#[proc_macro_derive(ValidateCsv, attributes(validate))]
pub fn validate_csv_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let fields = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(f) => &f.named,
            _ => {
                return syn::Error::new_spanned(
                    &data.fields,
                    "only structs with named fields are supported",
                )
                .to_compile_error()
                .into();
            }
        },
        _ => {
            return syn::Error::new_spanned(&input, "only structs are supported")
                .to_compile_error()
                .into();
        }
    };

    let mut field_validations = Vec::new();

    for field in fields {
        let field_name = field.ident.as_ref().unwrap().clone();
        let is_option = option_inner_type(&field.ty).is_some(); // [FIX] capturamos se é Option<T>
        let mut validations = Vec::new();

        for attr in &field.attrs {
            if attr.path().is_ident("validate") {
                match attr.parse_args_with(Validation::parse_validations) {
                    Ok(mut v) => {
                        // v.0.1.3: `required` só em Option<T>
                        let has_required = v.iter().any(|vv| matches!(vv, Validation::Required)); // v.0.1.3
                        if has_required && !is_option {                                          // v.0.1.3
                            return syn::Error::new_spanned(
                                &field.ty,
                                format!("`required` can only be used on Option<T> fields (field `{}`)", field_name)
                            ).to_compile_error().into();
                        }

                        // v.0.1.3: Restrições de tipo para validações baseadas em String
                        let needs_string = v.iter().any(|vv| matches!(
                            vv,
                            Validation::Regex{..} | Validation::Length{..} | Validation::NotBlank
                            | Validation::OneOf{..} | Validation::NotIn{..}
                        )); // v.0.1.3
                        if needs_string {
                            let core_ty = option_inner_type(&field.ty).unwrap_or(&field.ty); // v.0.1.3
                            let ty_name = Validation::type_name_of(core_ty);                                  // v.0.1.3
                            if ty_name != "String" {                                         // v.0.1.3
                                return syn::Error::new_spanned(
                                    core_ty,
                                    format!("`regex`, `length`, `not_blank`, `one_of`, `not_in` require String (field `{}` is `{}`)", field_name, ty_name)
                                ).to_compile_error().into();                                 // v.0.1.3
                            }
                        }

                        // v.0.1.3: Restrições de tipo para `range`
                        if let Some(is_float) = v.iter().find_map(|vv| {
                            if let Validation::Range{is_float, ..} = vv { Some(*is_float) } else { None }
                        }) { // v.0.1.3
                            let core_ty = option_inner_type(&field.ty).unwrap_or(&field.ty); // v.0.1.3
                            let ty_name     = Validation::type_name_of(core_ty);
                            let is_int      = Validation::is_int_ty(&ty_name);
                            let is_float_ty = Validation::is_float_ty(&ty_name);
                            if !(is_int || is_float_ty) {
                                return syn::Error::new_spanned(
                                    core_ty,
                                    format!("`range` only applies to numeric fields (field `{}` is `{}`)", field_name, ty_name)
                                ).to_compile_error().into(); // v.0.1.3
                            }
                            if is_float && !is_float_ty {
                                return syn::Error::new_spanned(
                                    core_ty,
                                    format!("`range` with float literals requires float field (field `{}` is `{}`)", field_name, ty_name)
                                ).to_compile_error().into(); // v.0.1.3
                            }
                            if !is_float && !is_int {
                                return syn::Error::new_spanned(
                                    core_ty,
                                    format!("`range` with integer literals requires integer field (field `{}` is `{}`)", field_name, ty_name)
                                ).to_compile_error().into(); // v.0.1.3
                            }
                        }

                        // valida if_then para este campo (se houver)
                        if let Err(ts) = Validation::validate_if_then_for_field(&v, field, &field_name, is_option, fields) {
                            return ts; // já vem com to_compile_error().into()
                        }

                        validations.append(&mut v);
                    },
                    Err(e) => return e.to_compile_error().into(),
                }
            }
        }

        if !validations.is_empty() {
            // v.0.1.3: calcular e guardar tipo efetivo para codegen (T se Option<T>, senão o próprio)
            let core_ty = option_inner_type(&field.ty).unwrap_or(&field.ty); // v.0.1.3
            let core_ty_ts = quote! { #core_ty };                            // v.0.1.3

            field_validations.push(FieldValidation {
                field_name,
                is_option,
                validations,
                core_ty_ts,   // v.0.1.3
            });
        }
    }

    let validation_arms = field_validations.into_iter().map(|fv| {
        let field_name_str = fv.field_name.to_string();
        let field_name_ident = fv.field_name;
        let fv_is_option = fv.is_option;
        let fv_core_ty_ts = fv.core_ty_ts.clone(); // v.0.1.3
    
        let checks = fv.validations.into_iter().map(|validation| {
            match validation {
                Validation::Required => {
                    gen_required_check(&field_name_ident, &field_name_str)
                }
                Validation::NotBlank => {
                    gen_not_blank_check(&field_name_ident, &field_name_str, fv_is_option)
                }
                Validation::Range { min, max, is_float: _ } => { // v.0.1.3
                    gen_range_check(&field_name_ident, &field_name_str, fv_is_option, min, max, fv_core_ty_ts.clone()) // v.0.1.3
                }
                Validation::Length { min, max } => {
                    gen_length_check(&field_name_ident, &field_name_str, fv_is_option, min, max)
                }
                Validation::Regex { regex } => {
                    gen_regex_check(&field_name_ident, &field_name_str, fv_is_option, regex)
                }
                Validation::OneOf { values } => {
                    gen_one_of_check(&field_name_ident, &field_name_str, fv_is_option, values)
                }
                Validation::NotIn { values } => {
                    gen_not_in_check(&field_name_ident, &field_name_str, fv_is_option, values)
                }
                Validation::Custom { path } => {
                    gen_custom_check(&field_name_ident, &field_name_str, fv_is_option, path)
                }
                Validation::IfThen { conditional_column, conditional_value, expected_value } => {
                    // Gera o check if_then, tipando expected_value com o tipo efetivo do campo alvo (R)
                    // e inferindo o tipo do campo condicional (U) via FromStr no runtime.
                    gen_if_then(
                        &field_name_ident,
                        &field_name_str,
                        fv_is_option,
                        fv_core_ty_ts.clone(),
                        conditional_column,
                        conditional_value,
                        expected_value,
                    )
                } // v 0.2.0
            }
        });
    
        quote! { #(#checks)* }
    });

    let expanded = quote! {
        impl #name {
            pub fn validate_csv(&self) -> ::core::result::Result<(), ::std::vec::Vec<::csv_schema_validator::ValidationError>> {
                let mut errors = ::std::vec::Vec::new();
                #(#validation_arms)*
                if errors.is_empty() {
                    Ok(())
                } else {
                    Err(errors)
                }
            }
        }
    };

    TokenStream::from(expanded)
}

use proc_macro2::TokenStream as TokenStream2;

// Helpers por regra
fn gen_required_check(field_ident: &syn::Ident, field_name: &str) -> TokenStream2 {
    quote! {
        if (&self.#field_ident).is_none() {
            errors.push(::csv_schema_validator::ValidationError {
                field: #field_name.to_string(),
                message: "mandatory field".to_string(),
            });
        }
    }
}

fn gen_not_blank_check(field_ident: &syn::Ident, field_name: &str, is_option: bool) -> TokenStream2 {
    if is_option {
        quote! {
            if let Some(value) = &self.#field_ident {
                if value.trim().is_empty() {
                    errors.push(::csv_schema_validator::ValidationError {
                        field: #field_name.to_string(),
                        message: "must not be blank or contain only whitespace".to_string(),
                    });
                }
            }
        }
    } else {
        quote! {
            let value = &self.#field_ident;
            if value.trim().is_empty() {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: "must not be blank or contain only whitespace".to_string(),
                });
            }
        }
    }
}

// v.0.1.3: `range` agora recebe min/max como Option<String> e usa o tipo efetivo do campo;
// tipamos os literais com o tipo do campo, para o compilador checar compatibilidade/overflow.
// Também produzimos mensagens específicas quando ambos os limites existem.
fn gen_range_check(
    field_ident: &syn::Ident,
    field_name: &str,
    is_option: bool,
    min: Option<String>,         // v.0.1.3
    max: Option<String>,         // v.0.1.3
    core_ty_ts: proc_macro2::TokenStream, // v.0.1.3
) -> TokenStream2 {
    let min_ts = min.as_ref().map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).expect("invalid min literal")); // v.0.1.3
    let max_ts = max.as_ref().map(|s| syn::parse_str::<proc_macro2::TokenStream>(s).expect("invalid max literal")); // v.0.1.3

    let min_bind = min_ts.as_ref().map(|ts| quote! { let __csv_min: #core_ty_ts = #ts; }); // v.0.1.3
    let max_bind = max_ts.as_ref().map(|ts| quote! { let __csv_max: #core_ty_ts = #ts; }); // v.0.1.3

    // v.0.1.3: mensagens amigáveis (normalizamos sufixo ".0")
    fn normalize_for_msg(s: &str) -> String { // v.0.1.3
        if let Some(stripped) = s.strip_suffix(".0") { stripped.to_string() } else { s.to_string() }
    }
    let msg_between = match (min.as_ref(), max.as_ref()) { // v.0.1.3
        (Some(a), Some(b)) => format!("value out of expected range: {} to {}", normalize_for_msg(a), normalize_for_msg(b)),
        _ => "value out of expected range".to_string(),
    };
    let msg_below = match min.as_ref() { // v.0.1.3
        Some(a) => format!("value below min: {}", normalize_for_msg(a)),
        None => "value below min".to_string(),
    };
    let msg_above = match max.as_ref() { // v.0.1.3
        Some(b) => format!("value above max: {}", normalize_for_msg(b)),
        None => "value above max".to_string(),
    };

    let cmp = match (min_bind.is_some(), max_bind.is_some()) {
        (true, true) => quote! {
            if !(__csv_min <= *value && *value <= __csv_max) {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: #msg_between.to_string(), // v.0.1.3
                });
            }
        },
        (true, false) => quote! {
            if !(__csv_min <= *value) {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: #msg_below.to_string(), // v.0.1.3
                });
            }
        },
        (false, true) => quote! {
            if !(*value <= __csv_max) {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: #msg_above.to_string(), // v.0.1.3
                });
            }
        },
        _ => quote! {}, // impossível pois já validamos no parse
    };

    if is_option {
        quote! {
            { #min_bind #max_bind
              if let Some(value) = &self.#field_ident { #cmp } }
        }
    } else {
        quote! {
            { #min_bind #max_bind
              let value = &self.#field_ident; #cmp }
        }
    }
}

fn gen_length_check(field_ident: &syn::Ident, field_name: &str, is_option: bool, min: usize, max: usize) -> TokenStream2 {
    if is_option {
        quote! {
            if let Some(value) = &self.#field_ident {
                let len = value.len();
                if len < #min || len > #max {
                    errors.push(::csv_schema_validator::ValidationError {
                        field: #field_name.to_string(),
                        message: format!("length out of expected range: {} to {}", #min, #max),
                    });
                }
            }
        }
    } else {
        quote! {
            let value = &self.#field_ident;
            let len = value.len();
            if len < #min || len > #max {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: format!("length out of expected range: {} to {}", #min, #max),
                });
            }
        }
    }
}

fn gen_regex_check(field_ident: &syn::Ident, field_name: &str, is_option: bool, regex: String) -> TokenStream2 {
    let body = quote! {
        use ::csv_schema_validator::__private::once_cell::sync::Lazy;
        use ::csv_schema_validator::__private::regex;
        static RE: Lazy<Result<regex::Regex, regex::Error>> = Lazy::new(|| regex::Regex::new(#regex));

        match RE.as_ref() {
            Ok(compiled_regex) => {
                if !compiled_regex.is_match(value) {
                    errors.push(::csv_schema_validator::ValidationError {
                        field: #field_name.to_string(),
                        message: "does not match the expected pattern".to_string(),
                    });
                }
            }
            Err(e) => {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: format!("invalid regex '{}': {}", #regex, e),
                });
            }
        }
    };
    if is_option {
        quote! {
            if let Some(value) = &self.#field_ident {
                #body
            }
        }
    } else {
        quote! {
            let value = &self.#field_ident;
            #body
        }
    }
}

fn gen_one_of_check(field_ident: &syn::Ident, field_name: &str, is_option: bool, values: Vec<String>) -> TokenStream2 {
    let arr = values; // mantém o padrão já usado
    if is_option {
        quote! {
            if let Some(value) = &self.#field_ident {
                const __ALLOWED: &[&str] = &[#(#arr),*];
                if !__ALLOWED.contains(&value.as_str()) {
                    errors.push(::csv_schema_validator::ValidationError {
                        field: #field_name.to_string(),
                        message: format!("invalid value"),
                    });
                }
            }
        }
    } else {
        quote! {
            let value = &self.#field_ident;
            const __ALLOWED: &[&str] = &[#(#arr),*];
            if !__ALLOWED.contains(&value.as_str()) {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: format!("invalid value"),
                });
            }
        }
    }
}

fn gen_not_in_check(field_ident: &syn::Ident, field_name: &str, is_option: bool, values: Vec<String>) -> TokenStream2 {
    let arr = values;
    if is_option {
        quote! {
            if let Some(value) = &self.#field_ident {
                const __FORBIDDEN: &[&str] = &[#(#arr),*];
                if __FORBIDDEN.contains(&value.as_str()) {
                    errors.push(::csv_schema_validator::ValidationError {
                        field: #field_name.to_string(),
                        message: format!("value not allowed"),
                    });
                }
            }
        }
    } else {
        quote! {
            let value = &self.#field_ident;
            const __FORBIDDEN: &[&str] = &[#(#arr),*];
            if __FORBIDDEN.contains(&value.as_str()) {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #field_name.to_string(),
                    message: format!("value not allowed"),
                });
            }
        }
    }
}

fn gen_custom_check(field_ident: &syn::Ident, field_name: &str, is_option: bool, path: syn::Path) -> TokenStream2 {
    if is_option {
        quote! {
            if let Some(value) = &self.#field_ident {
                match #path(value) {
                    Err(err) => {
                        errors.push(::csv_schema_validator::ValidationError {
                            field: #field_name.to_string(),
                            message: format!("{}", err),
                        });
                    }
                    Ok(()) => {}
                }
            }
        }
    } else {
        quote! {
            match #path(&self.#field_ident) {
                Err(err) => {
                    errors.push(::csv_schema_validator::ValidationError {
                        field: #field_name.to_string(),
                        message: format!("{}", err),
                    });
                }
                Ok(()) => {}
            }
        }
    }
}


// Gera a validação if_then:
// Se self.<conditional_column> == conditional_value então
//   self.<field_ident> deve existir (Some) e ser igual a expected_value.
//
// Observações importantes:
// - Não assumimos tipos específicos. Em vez de tipar literais diretamente,
//   usamos FromStr para converter as strings fornecidas no atributo para os
//   tipos reais dos campos em tempo de execução:
//     * expected_value -> convertido para o tipo efetivo do CAMPO ANOTADO (R)
//       usando <R as FromStr>::from_str(...).
//     * conditional_value -> convertido dinamicamente para o tipo do CAMPO
//       CONDICIONAL (U) usando inferência com <_ as FromStr>::from_str(...)
//       e comparação com *__cond_ref (o compilador infere U).
// - Isso preserva compatibilidade com sinais, tamanhos e booleanos, sem
//   "forçar" i32/f64. Para String, FromStr já devolve String.
// - Se o parse falhar (embora já termos feito check em compile-time), geramos
//   um erro amigável em runtime e ignoramos o restante do bloco.
//
fn gen_if_then(
    target_field_ident: &syn::Ident,                 // campo anotado (alvo)
    target_field_name: &str,                         // nome para mensagem
    _target_is_option: bool,                         // já garantido ser Option<R>
    target_core_ty_ts: proc_macro2::TokenStream,     // tipo efetivo R
    conditional_column: String,                      // nome do campo condicional
    conditional_value: String,                       // valor esperado no campo condicional (string literal)
    expected_value: String,                          // valor que o campo alvo deve ter quando a condição é verdadeira
) -> TokenStream2 {
    let cond_ident = syn::Ident::new(&conditional_column, proc_macro2::Span::call_site());
    let cond_val_str = conditional_value;
    let expected_val_str = expected_value;

    quote! {
        {
            // 1) Parse do expected_value para o tipo do campo alvo (R)
            let __csv_expected_parse: ::core::result::Result<#target_core_ty_ts, _> =
                <#target_core_ty_ts as ::core::str::FromStr>::from_str(#expected_val_str);

            if let Ok(__csv_expected) = __csv_expected_parse {
                // 2) Checagem da condição com helper genérico que monomorfiza em T (= tipo real do campo condicional)
                //    Evita o uso de "<_ as FromStr>::from_str(...)" que causa E0283.
                #[inline]
                fn __csv_eq_parsed<T>(cond_ref: &T, s: &str) -> bool
                where
                    T: ::core::str::FromStr + ::core::cmp::PartialEq,
                {
                    match <T as ::core::str::FromStr>::from_str(s) {
                        Ok(v) => *cond_ref == v,
                        Err(_) => false,
                    }
                }

                let __csv_condition_holds = match &self.#cond_ident {
                    Some(__cond_ref) => __csv_eq_parsed(__cond_ref, #cond_val_str),
                    None => false,
                };

                if __csv_condition_holds {
                    match &self.#target_field_ident {
                        Some(__v) if *__v == __csv_expected => { /* ok */ }
                        Some(_) => {
                            errors.push(::csv_schema_validator::ValidationError {
                                field: #target_field_name.to_string(),
                                message: format!(
                                    "must be {} when {} == {}",
                                    #expected_val_str, #conditional_column, #cond_val_str
                                ),
                            });
                        }
                        None => {
                            errors.push(::csv_schema_validator::ValidationError {
                                field: #target_field_name.to_string(),
                                message: format!(
                                    "must be {} when {} == {} (missing value)",
                                    #expected_val_str, #conditional_column, #cond_val_str
                                ),
                            });
                        }
                    }
                }
            } else {
                errors.push(::csv_schema_validator::ValidationError {
                    field: #target_field_name.to_string(),
                    message: format!(
                        "invalid expected_value '{}' for field type",
                        #expected_val_str
                    ),
                });
            }
        }
    }
}