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
//! # Reinhardt Serializers
//!
//! Django REST Framework-inspired serializers for data validation and transformation.
//!
//! ## Overview
//!
//! This crate provides serialization, deserialization, and validation capabilities
//! for REST APIs, with ORM integration for model-based serializers.
//!
//! ## Features
//!
//! - **[`Serializer`]**: Base serializer for data validation and transformation
//! - **[`ModelSerializer`]**: Auto-generated serializers from model definitions
//! - **[`NestedSerializer`]**: Handle nested relationships
//! - **[`HyperlinkedModelSerializer`]**: Serializers with hyperlinked relationships
//! - **Field Types**: CharField, IntegerField, DateTimeField, etc.
//! - **Validators**: UniqueValidator, custom validation functions
//! - **Performance**: Query caching, N+1 detection, batch validation
//! - **Content Negotiation**: JSON, XML, and custom parsers
//!
//! ## Validation Strategies
//!
//! [`ModelSerializer`] runs three validator layers, ordered by latency:
//!
//! | Layer | Where it runs | Use case |
//! |-------|---------------|----------|
//! | Object-sync | [`ModelSerializer::validate`] (called first by `validate_async`) | Cross-field invariants, no DB (e.g. `start < end`) |
//! | Field-async (unique) | [`ModelSerializer::validate_async`] | Per-field DB checks (UNIQUE constraints) |
//! | Composite-async (unique-together) | [`ModelSerializer::validate_async`] | Multi-field DB checks |
//!
//! Register synchronous validators with [`ModelSerializer::with_model_validator`].
//! Register database-backed validators with [`ModelSerializer::with_unique_validator`]
//! and [`ModelSerializer::with_unique_together_validator`].
//!
//! ## Quick Start
//!
//! ### Basic Serializer
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{Serializer, CharField, IntegerField, EmailField};
//!
//! struct UserSerializer {
//! id: IntegerField,
//! username: CharField,
//! email: EmailField,
//! }
//!
//! impl Serializer for UserSerializer {
//! type Output = User;
//!
//! fn validate(&self, data: &Value) -> ValidationResult<Self::Output> {
//! // Validation logic
//! }
//! }
//! ```
//!
//! ### Model Serializer
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::ModelSerializer;
//!
//! // Automatically generates serializer fields from User model
//! let serializer = ModelSerializer::<User>::new()
//! .fields(&["id", "username", "email", "created_at"])
//! .read_only(&["id", "created_at"])
//! .build();
//!
//! // Serialize a user
//! let json = serializer.serialize(&user)?;
//!
//! // Deserialize and validate
//! let user: User = serializer.deserialize(&json_data)?;
//! ```
//!
//! ## Relation Fields
//!
//! Handle model relationships:
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{
//! PrimaryKeyRelatedField, SlugRelatedField,
//! HyperlinkedRelatedField, StringRelatedField
//! };
//!
//! // Primary key representation
//! let author = PrimaryKeyRelatedField::<Author>::new();
//! // Output: {"author": 1}
//!
//! // Slug field representation
//! let category = SlugRelatedField::<Category>::new("slug");
//! // Output: {"category": "technology"}
//!
//! // Hyperlink representation
//! let author = HyperlinkedRelatedField::<Author>::new("author-detail");
//! // Output: {"author": "http://example.com/api/authors/1/"}
//!
//! // String representation (uses __str__)
//! let tags = StringRelatedField::<Tag>::many();
//! // Output: {"tags": ["Python", "Rust", "Web"]}
//! ```
//!
//! ## Nested Serializers
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{NestedSerializer, WritableNestedSerializer, ListSerializer};
//!
//! // Read-only nested serializer
//! let author = NestedSerializer::<AuthorSerializer>::new();
//! // Output: {"author": {"id": 1, "name": "Alice"}}
//!
//! // Writable nested serializer (supports create/update)
//! let profile = WritableNestedSerializer::<ProfileSerializer>::new();
//!
//! // List of nested objects
//! let comments = ListSerializer::<CommentSerializer>::new();
//! // Output: {"comments": [{"id": 1, "text": "..."}, ...]}
//! ```
//!
//! ## Validation
//!
//! ### Field-Level Validation
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{FieldValidator, ValidationError};
//!
//! fn validate_username(value: &str) -> Result<(), ValidationError> {
//! if value.len() < 3 {
//! return Err(ValidationError::new("Username must be at least 3 characters"));
//! }
//! Ok(())
//! }
//! ```
//!
//! ### Object-Level Validation
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{ObjectValidator, ValidationError};
//!
//! fn validate_password_match(data: &Value) -> Result<(), ValidationError> {
//! let password = data.get("password");
//! let confirm = data.get("password_confirm");
//! if password != confirm {
//! return Err(ValidationError::new("Passwords do not match"));
//! }
//! Ok(())
//! }
//! ```
//!
//! ### Database Validators
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{UniqueValidator, UniqueTogetherValidator};
//!
//! // Ensure email is unique
//! let email_validator = UniqueValidator::<User>::new("email");
//!
//! // Ensure (user_id, slug) is unique together
//! let slug_validator = UniqueTogetherValidator::<Post>::new(&["user_id", "slug"]);
//! ```
//!
//! ## Performance Optimization
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::{QueryCache, N1Detector, BatchValidator};
//!
//! // Cache repeated queries
//! let cache = QueryCache::new();
//! let serializer = serializer.with_cache(&cache);
//!
//! // Detect N+1 query issues
//! let detector = N1Detector::new();
//! let result = detector.analyze(&serializer, &queryset)?;
//! if let Some(warning) = result.warning() {
//! eprintln!("N+1 detected: {}", warning);
//! }
//!
//! // Batch validation for multiple objects
//! let validator = BatchValidator::new();
//! let results = validator.validate_many(&objects)?;
//! ```
//!
//! ## Content Negotiation
//!
//! ```rust,ignore
//! use reinhardt_rest::serializers::ContentNegotiator;
//!
//! let negotiator = ContentNegotiator::new()
//! .add_parser("application/json", JsonParser::new())
//! .add_parser("application/xml", XmlParser::new());
//!
//! let parser = negotiator.select(&request)?;
//! let data = parser.parse(&request.body)?;
//! ```
// Re-export base layer types from reinhardt-core
pub use ;
// REST-specific modules (ORM-integrated features)
/// Cache invalidation strategies for serialized data.
/// Content negotiation for serializer output formats.
/// Hyperlinked model serializers with URL-based relationships.
/// Serializer introspection utilities.
/// Serializer meta configuration.
/// Method-based computed fields for serializers.
/// ORM-integrated model serializers.
/// Nested serializer support.
/// Configuration for nested serializer behavior.
/// ORM-integrated nested serializer support.
/// Parser integration for serializer input.
/// Serialization performance monitoring and metrics.
/// Database connection pool management for serializers.
/// QuerySet integration for model serializers.
/// ORM-integrated relation field types.
/// Relationship field types for serializers.
/// Validator configuration for serializer fields.
/// Database-backed validators (unique, unique-together).
// Re-export REST-specific types
pub use ;
pub use ContentNegotiator;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ModelSerializer;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;