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
//! Nested serializer configuration
//!
//! This module provides configuration for nested serializers in ModelSerializer.
use std::collections::HashMap;
/// Configuration for a single nested field
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct NestedFieldConfig {
/// Field name that contains the relationship
pub field_name: String,
/// Maximum depth for nested serialization
pub depth: usize,
/// Whether to include this nested field in serialization
pub read_only: bool,
/// Whether to allow creating nested instances during deserialization
pub allow_create: bool,
/// Whether to allow updating nested instances during deserialization
pub allow_update: bool,
}
impl NestedFieldConfig {
/// Create a new nested field configuration
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedFieldConfig;
///
/// let config = NestedFieldConfig::new("author");
/// // Verify the config is created with correct defaults
/// assert_eq!(config.field_name, "author");
/// assert_eq!(config.depth, 1);
/// let _: NestedFieldConfig = config;
/// ```
pub fn new(field_name: impl Into<String>) -> Self {
Self {
field_name: field_name.into(),
depth: 1,
read_only: false,
allow_create: false,
allow_update: false,
}
}
/// Set the nesting depth
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedFieldConfig;
///
/// let config = NestedFieldConfig::new("author").depth(2);
/// // Verify the depth is set correctly
/// assert_eq!(config.depth, 2);
/// let _: NestedFieldConfig = config;
/// ```
pub fn depth(mut self, depth: usize) -> Self {
self.depth = depth;
self
}
/// Mark this field as read-only
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedFieldConfig;
///
/// let config = NestedFieldConfig::new("author").read_only();
/// // Verify read_only flag is set
/// assert!(config.read_only);
/// let _: NestedFieldConfig = config;
/// ```
pub fn read_only(mut self) -> Self {
self.read_only = true;
self
}
/// Allow creating nested instances
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedFieldConfig;
///
/// let config = NestedFieldConfig::new("author").allow_create();
/// // Verify allow_create flag is set
/// assert!(config.allow_create);
/// let _: NestedFieldConfig = config;
/// ```
pub fn allow_create(mut self) -> Self {
self.allow_create = true;
self
}
/// Allow updating nested instances
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedFieldConfig;
///
/// let config = NestedFieldConfig::new("author").allow_update();
/// // Verify allow_update flag is set
/// assert!(config.allow_update);
/// let _: NestedFieldConfig = config;
/// ```
pub fn allow_update(mut self) -> Self {
self.allow_update = true;
self
}
/// Allow both creating and updating nested instances
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedFieldConfig;
///
/// let config = NestedFieldConfig::new("author").writable();
/// // Verify both allow_create and allow_update flags are set
/// assert!(config.allow_create);
/// assert!(config.allow_update);
/// let _: NestedFieldConfig = config;
/// ```
pub fn writable(mut self) -> Self {
self.allow_create = true;
self.allow_update = true;
self
}
}
/// Configuration manager for nested serializers
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub struct NestedSerializerConfig {
/// Map of field names to their nested configurations
nested_fields: HashMap<String, NestedFieldConfig>,
}
impl NestedSerializerConfig {
/// Create a new empty nested serializer configuration
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::NestedSerializerConfig;
///
/// let config = NestedSerializerConfig::new();
/// // Verify the config is created successfully
/// let _: NestedSerializerConfig = config;
/// ```
pub fn new() -> Self {
Self {
nested_fields: HashMap::new(),
}
}
/// Add a nested field configuration
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::{NestedSerializerConfig, NestedFieldConfig};
///
/// let mut config = NestedSerializerConfig::new();
/// config.add_nested_field(NestedFieldConfig::new("author").depth(2));
/// // Verify the nested field is added successfully
/// assert!(config.is_nested_field("author"));
/// ```
pub fn add_nested_field(&mut self, field_config: NestedFieldConfig) {
self.nested_fields
.insert(field_config.field_name.clone(), field_config);
}
/// Get a nested field configuration
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::{NestedSerializerConfig, NestedFieldConfig};
///
/// let mut config = NestedSerializerConfig::new();
/// config.add_nested_field(NestedFieldConfig::new("author"));
///
/// // Verify the nested field is retrieved correctly
/// let author_config = config.get_nested_field("author");
/// assert!(author_config.is_some());
/// ```
pub fn get_nested_field(&self, field_name: &str) -> Option<&NestedFieldConfig> {
self.nested_fields.get(field_name)
}
/// Check if a field is configured as nested
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::{NestedSerializerConfig, NestedFieldConfig};
///
/// let mut config = NestedSerializerConfig::new();
/// config.add_nested_field(NestedFieldConfig::new("author"));
///
/// // Verify nested field presence
/// assert!(config.is_nested_field("author"));
/// assert!(!config.is_nested_field("title"));
/// ```
pub fn is_nested_field(&self, field_name: &str) -> bool {
self.nested_fields.contains_key(field_name)
}
/// Get all nested field names
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::{NestedSerializerConfig, NestedFieldConfig};
///
/// let mut config = NestedSerializerConfig::new();
/// config.add_nested_field(NestedFieldConfig::new("author"));
/// config.add_nested_field(NestedFieldConfig::new("category"));
///
/// // Verify all nested field names are retrieved
/// let fields = config.nested_field_names();
/// assert_eq!(fields.len(), 2);
/// ```
pub fn nested_field_names(&self) -> Vec<String> {
self.nested_fields.keys().cloned().collect()
}
/// Remove a nested field configuration
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::{NestedSerializerConfig, NestedFieldConfig};
///
/// let mut config = NestedSerializerConfig::new();
/// config.add_nested_field(NestedFieldConfig::new("author"));
/// assert!(config.is_nested_field("author"));
///
/// // Verify nested field is removed correctly
/// config.remove_nested_field("author");
/// assert!(!config.is_nested_field("author"));
/// ```
pub fn remove_nested_field(&mut self, field_name: &str) -> Option<NestedFieldConfig> {
self.nested_fields.remove(field_name)
}
/// Get the depth for a nested field
///
/// # Examples
///
/// ```
/// use reinhardt_rest::serializers::nested_config::{NestedSerializerConfig, NestedFieldConfig};
///
/// let mut config = NestedSerializerConfig::new();
/// config.add_nested_field(NestedFieldConfig::new("author").depth(3));
///
/// // Verify depth is retrieved correctly
/// assert_eq!(config.get_depth("author"), Some(3));
/// assert_eq!(config.get_depth("title"), None);
/// ```
pub fn get_depth(&self, field_name: &str) -> Option<usize> {
self.nested_fields.get(field_name).map(|c| c.depth)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nested_field_config_new() {
let config = NestedFieldConfig::new("author");
assert_eq!(config.field_name, "author");
assert_eq!(config.depth, 1);
assert!(!config.read_only);
assert!(!config.allow_create);
assert!(!config.allow_update);
}
#[test]
fn test_nested_field_config_depth() {
let config = NestedFieldConfig::new("author").depth(3);
assert_eq!(config.depth, 3);
}
#[test]
fn test_nested_field_config_read_only() {
let config = NestedFieldConfig::new("author").read_only();
assert!(config.read_only);
}
#[test]
fn test_nested_field_config_allow_create() {
let config = NestedFieldConfig::new("author").allow_create();
assert!(config.allow_create);
assert!(!config.allow_update);
}
#[test]
fn test_nested_field_config_allow_update() {
let config = NestedFieldConfig::new("author").allow_update();
assert!(!config.allow_create);
assert!(config.allow_update);
}
#[test]
fn test_nested_field_config_writable() {
let config = NestedFieldConfig::new("author").writable();
assert!(config.allow_create);
assert!(config.allow_update);
}
#[test]
fn test_nested_serializer_config_new() {
let config = NestedSerializerConfig::new();
assert_eq!(config.nested_field_names().len(), 0);
}
#[test]
fn test_add_and_get_nested_field() {
let mut config = NestedSerializerConfig::new();
config.add_nested_field(NestedFieldConfig::new("author").depth(2));
assert!(config.is_nested_field("author"));
assert!(!config.is_nested_field("title"));
let author_config = config.get_nested_field("author").unwrap();
assert_eq!(author_config.field_name, "author");
assert_eq!(author_config.depth, 2);
}
#[test]
fn test_remove_nested_field() {
let mut config = NestedSerializerConfig::new();
config.add_nested_field(NestedFieldConfig::new("author"));
assert!(config.is_nested_field("author"));
let removed = config.remove_nested_field("author");
assert!(removed.is_some());
assert!(!config.is_nested_field("author"));
}
#[test]
fn test_nested_field_names() {
let mut config = NestedSerializerConfig::new();
config.add_nested_field(NestedFieldConfig::new("author"));
config.add_nested_field(NestedFieldConfig::new("category"));
let names = config.nested_field_names();
assert_eq!(names.len(), 2);
assert!(names.contains(&"author".to_string()));
assert!(names.contains(&"category".to_string()));
}
#[test]
fn test_get_depth() {
let mut config = NestedSerializerConfig::new();
config.add_nested_field(NestedFieldConfig::new("author").depth(3));
assert_eq!(config.get_depth("author"), Some(3));
assert_eq!(config.get_depth("unknown"), None);
}
}