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
//! Utility macros for working with field selectors
//!
//! This module provides convenient macros for common operations with field selectors:
//! - `contains!`: Check if a field path is enabled
//! - `copy_selected_fields!`: Create structs with conditional field copying
//! - `filter_field_set!`: Create filtered sets of enabled field paths
/// Check if a field path is enabled in a field selector.
///
/// This macro uses non-recursive Option chaining for levels 1-2, and recursive
/// calls for deeper nesting (3+ levels) to balance performance and simplicity.
///
/// # Examples
///
/// ```rust
/// # use serialize_fields::{SerializeFields, SerializeFieldsTrait, contains};
/// # use serde::{Serialize, Deserialize};
/// # #[derive(SerializeFields, Serialize, Deserialize)]
/// # struct User { id: u32, name: String, profile: Profile }
/// # #[derive(SerializeFields, Serialize, Deserialize)]
/// # struct Profile { bio: String }
/// # let user = User { id: 1, name: "Alice".to_string(), profile: Profile { bio: "Developer".to_string() } };
/// let mut selector = user.serialize_fields();
/// selector.enable_dot_hierarchy("id");
/// selector.enable_dot_hierarchy("profile.bio");
///
/// assert!(contains!(selector, id));
/// assert!(contains!(selector, profile.bio));
/// assert!(!contains!(selector, name));
/// ```
/// Copy selected fields from a source struct to a new struct, using provided blocks for enabled fields.
///
/// For each field, if it's enabled in the field selector, the corresponding block is evaluated.
/// Otherwise, the field is set to `None` (for Option fields) or a default value.
///
/// # Examples
///
/// ```rust
/// # use serialize_fields::{SerializeFields, SerializeFieldsTrait, copy_selected_fields};
/// # use serde::{Serialize, Deserialize};
/// # #[derive(SerializeFields, Serialize, Deserialize)]
/// # struct User { id: u32, name: String, email: Option<String> }
/// # let user = User { id: 1, name: "Alice".to_string(), email: Some("alice@example.com".to_string()) };
/// let mut selector = user.serialize_fields();
/// selector.enable_dot_hierarchy("id");
/// selector.enable_dot_hierarchy("name");
///
/// #[derive(Debug)]
/// struct PartialUser {
/// id: Option<u32>,
/// name: Option<String>,
/// email: Option<String>,
/// }
///
/// let partial = copy_selected_fields!(selector, PartialUser {
/// id: Some(user.id),
/// name: Some(user.name.clone()),
/// email: user.email.clone()
/// });
///
/// assert_eq!(partial.id, Some(1));
/// assert_eq!(partial.name, Some("Alice".to_string()));
/// assert_eq!(partial.email, None); // Not enabled in selector
/// ```
/// Create a BTreeSet of enabled field paths that match specified patterns.
///
/// This macro checks which fields are enabled and formats them according to the provided patterns.
/// It supports multiple outputs per field using the `;` separator for cases where one field
/// should map to multiple output values. Field paths use dot notation for nested access.
///
/// # Examples
///
/// ```rust
/// # use serialize_fields::{SerializeFields, SerializeFieldsTrait, filter_field_set};
/// # use serde::{Serialize, Deserialize};
/// # #[derive(SerializeFields, Serialize, Deserialize)]
/// # struct User { id: u32, name: String, profile: Profile }
/// # #[derive(SerializeFields, Serialize, Deserialize)]
/// # struct Profile { bio: String, avatar: String }
/// # let user = User {
/// # id: 1,
/// # name: "Alice".to_string(),
/// # profile: Profile { bio: "Developer".to_string(), avatar: "avatar.jpg".to_string() }
/// # };
/// let mut selector = user.serialize_fields();
/// selector.enable_dot_hierarchy("id");
/// selector.enable_dot_hierarchy("name");
/// selector.enable_dot_hierarchy("profile.bio");
///
/// // Single output per field
/// let field_set = filter_field_set!(selector, {
/// id => format!("user_id"),
/// name => format!("user_name"),
/// profile.bio => format!("profile_bio"),
/// profile.avatar => format!("profile_avatar")
/// });
///
/// // Multiple outputs per field using ; separator
/// let multi_field_set = filter_field_set!(selector, {
/// id => "user_id".to_string() ; "id".to_string() ; "primary_key".to_string(),
/// name => "user_name".to_string() ; "username".to_string(),
/// profile.bio => "bio".to_string() ; "biography".to_string() ; "profile_bio".to_string(),
/// });
///
/// // Only enabled fields are included (all outputs for that field)
/// assert!(multi_field_set.contains("user_id"));
/// assert!(multi_field_set.contains("id"));
/// assert!(multi_field_set.contains("primary_key"));
/// assert!(multi_field_set.contains("bio"));
/// assert!(multi_field_set.contains("biography"));
/// assert!(multi_field_set.contains("profile_bio"));
/// ```
) => ;
}
/// Helper macro to check field paths of any depth for filter_field_set!
/// Helper macro for filter_field_set! to handle different field path patterns
/// Helper macro for creating field path patterns in filter_field_set!
///
/// This handles the conversion of dot notation to nested field access.
/// Helper macro to create nested field selectors.
/// Usage: create_field_selector!(StructName { field1, field2: NestedStruct { nested_field1, nested_field2 }, ... })
;
// Nested field with struct specification
=> ;
}