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
//! Relation fields - Django REST Framework inspired relationship serialization
//!
//! This module provides field types for representing relationships between models
//! in serialized output, supporting various representation strategies.
use ;
use PhantomData;
/// RelationField - Base trait for relationship representation
///
/// Defines the common interface for all relationship field types.
/// Different field types represent the same relationship in different ways.
/// PrimaryKeyRelatedField - Represent relationships by primary key
///
/// This is the most common and efficient way to represent relationships,
/// storing only the primary key of the related model.
///
/// # Examples
///
/// ```
/// # use reinhardt_rest::serializers::PrimaryKeyRelatedField;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Author {
/// # id: Option<i64>,
/// # name: String,
/// # }
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Post {
/// # id: Option<i64>,
/// # title: String,
/// # author: PrimaryKeyRelatedField<Author>,
/// # }
/// // In JSON, the author field will be represented as just the ID:
/// // {"id": 1, "title": "My Post", "author": 42}
/// ```
pub type PrimaryKeyRelatedField<T> = ;
/// SlugRelatedField - Represent relationships by a slug field
///
/// Uses a unique text field (slug) instead of the primary key to represent
/// the relationship. Useful for human-readable URLs and references.
///
/// # Examples
///
/// ```
/// # use reinhardt_rest::serializers::SlugRelatedField;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Category {
/// # id: Option<i64>,
/// # slug: String,
/// # name: String,
/// # }
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Product {
/// # id: Option<i64>,
/// # name: String,
/// # category: SlugRelatedField<Category>,
/// # }
/// // In JSON, the category field will be represented as a slug:
/// // {"id": 1, "name": "Widget", "category": "electronics"}
/// ```
pub type SlugRelatedField<T> = ;
/// StringRelatedField - Represent relationships by string representation
///
/// Uses the string representation of the related model (typically from
/// Display trait or a custom method). Read-only field type.
///
/// # Examples
///
/// ```
/// # use reinhardt_rest::serializers::StringRelatedField;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct User {
/// # id: Option<i64>,
/// # username: String,
/// # }
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Comment {
/// # id: Option<i64>,
/// # text: String,
/// # author: StringRelatedField<User>,
/// # }
/// // In JSON, the author field will be represented as a string:
/// // {"id": 1, "text": "Great post!", "author": "john_doe"}
/// ```
pub type StringRelatedField<T> = ;
/// HyperlinkedRelatedField - Represent relationships by URL
///
/// Stores a URL that points to the detail view of the related model,
/// following HATEOAS principles.
///
/// # Examples
///
/// ```
/// # use reinhardt_rest::serializers::HyperlinkedRelatedField;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Author {
/// # id: Option<i64>,
/// # name: String,
/// # }
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Book {
/// # id: Option<i64>,
/// # title: String,
/// # author: HyperlinkedRelatedField<Author>,
/// # }
/// // In JSON, the author field will be represented as a URL:
/// // {"id": 1, "title": "My Book", "author": "/api/authors/42/"}
/// ```
pub type HyperlinkedRelatedField<T> = ;
/// ManyRelatedField - Represent many-to-many or reverse relationships
///
/// Wrapper for collections of related objects, can use any of the above
/// field types for the individual items.
///
/// # Examples
///
/// ```
/// # use reinhardt_rest::serializers::ManyRelatedField;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Tag {
/// # id: Option<i64>,
/// # name: String,
/// # }
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Article {
/// # id: Option<i64>,
/// # title: String,
/// # tags: ManyRelatedField<Tag>,
/// # }
/// // In JSON, the tags field will be an array:
/// // {"id": 1, "title": "My Article", "tags": [1, 2, 3]}
/// // or with SlugRelatedField:
/// // {"id": 1, "title": "My Article", "tags": ["rust", "programming", "web"]}
/// ```
/// IdentityField - Returns the entire related object
///
/// Used internally by NestedSerializer to represent the entire related
/// object instead of just a reference.
///
/// # Examples
///
/// ```
/// # use reinhardt_rest::serializers::IdentityField;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct Profile {
/// # id: Option<i64>,
/// # bio: String,
/// # }
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct User {
/// # id: Option<i64>,
/// # username: String,
/// # profile: IdentityField<Profile>,
/// # }
/// // In JSON, the profile field will be the entire object:
/// // {"id": 1, "username": "alice", "profile": {"id": 1, "bio": "Developer"}}
/// ```
pub type IdentityField<T> = ;