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
//! This module provides functions for generating token streams related to credit card validation.
//! It's part of a procedural macro and helps in constructing the validation logic
//! that will be injected into the user's code.
use quote;
use crate;
/// Represents the arguments parsed from a `#[validate(credit_card(...))]` attribute.
///
/// This struct is used by `darling` to deserialize the attribute's meta items.
/// It allows users to optionally specify a custom error `message` and `code`
/// for the credit card validation. If not provided, default values will be used.
/// Generates the `proc_macro2::TokenStream` for credit card validation.
///
/// This function takes the necessary components for building a validation check
/// for a field that is expected to be a credit card number. It constructs a
/// code snippet that, when executed, will validate the field's value as a
/// credit card. If the validation fails, it adds an error to a validation
/// errors collection.
///
/// # Arguments
///
/// * `crate_name` - A reference to `CrateName`, used to correctly qualify paths to items
/// from the `valid8r` crate (e.g., `::valid8r::ValidationErrors`).
/// * `credit_card` - A `Card` struct instance containing metadata for the validation,
/// such as a custom error message or error code.
/// * `field_name` - A `proc_macro2::TokenStream` representing the identifier of the field
/// being validated (e.g., `self.my_card_field`).
/// * `field_name_str` - A string slice (`&str`) representing the name of the field,
/// used as a key when adding errors to the `ValidationErrors` map.
///
/// # Returns
///
/// A `proc_macro2::TokenStream` containing the Rust code for the credit card validation logic.
/// This token stream is intended to be interpolated into the derived `Validate` trait
/// implementation.