xmlity-derive 0.0.9

Derive proc-macros for xmlity.
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
Derives the [`Deserialize`] trait for a type.

This macro supports deriving deserialization from elements, attributes and values.

One of the following can be applied to the root of a type:

- `#[xelement(...)]` - Specifies that the type can be deserialized as an element.
- `#[xvalue(...)]` - Specifies that the type can be deserialized as a value.
- `#[xattribute(...)]` - Specifies that the type can be deserialized as an attribute.
- No attribute/default behavior - Specifies that the type is a composite type. Can be deserialized from a sequence of elements.

## Modes of deserialization

Modes of deserialization depends on the type of data structure you are deserializing.

### Deserialize as an element - `#[xelement(...)]` on the root of a type

The `#[xelement(...)]` attribute can be applied to the root of a type to specify that the type can be deserialized from an element.

#### Root Options

<table style="width:100%;">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody style="vertical-align:top;">
<!--=================================================-->
<tr>
<th>
name
</th>
<td>
<code>String</code>
</td>
<td>
Element name.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
namespace
</th>
<td>
<code>String</code>
</td>
<td>
Must be a valid namespace string.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
namespace_expr
</th>
<td>
<code>Expr</code>
</td>
<td>
Element namespace expression. This should be a value of type `xmlity::XmlNamespace`.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
allow_unknown_children
</th>
<td>
<code>"any"</code>, <code>"at_end"</code>, <code>"none"</code>
</td>
<td>
Allow unknown children when deserializing.<br/>
- <code>"any"</code>: Allow any unknown children.<br/>
- <code>"at_end"</code> (default): Allow unknown children only at the end of the element.<br/>
- <code>"none"</code>: Do not allow unknown children at all.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
allow_unknown_attributes
</th>
<td>
<code>"any"</code>, <code>"at_end"</code>, <code>"none"</code>
</td>
<td>
Allow unknown attributes when deserializing.<br/>
- <code>"any"</code>: Allow any unknown attributes.<br/>
- <code>"at_end"</code> (default): Allow unknown attributes only at the end of the element.<br/>
- <code>"none"</code>: Do not allow unknown attributes at all.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
deserialize_any_name
</th>
<td>
<code>bool</code>
</td>
<td>
Allow any name for the element when deserializing.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
attribute_order
</th>
<td>
<code>"strict"</code>, <code>"none"</code>
</td>
<td>
Set if the order of attributes is important when deserializing.<br/>
- <code>"strict"</code>: The order of attributes must match the order in the struct or enum variant.<br/>
- <code>"none"</code> (default): The order of attributes does not matter, but the attributes must be present.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
children_order
</th>
<td>
<code>"strict"</code>, <code>"none"</code>
</td>
<td>
Set if the order of children is important when deserializing.<br/>
- <code>"strict"</code>: The order of children must match the order in the struct or enum variant.<br/>
- <code>"none"</code> (default): The order of children does not matter, but the children must be present.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
ignore_whitespace
</th>
<td>
<code>"any"</code>, <code>"none"</code>
</td>
<td>
Set if whitespace should be ignored when deserializing.<br/>
- <code>"any"</code> (default): Ignore any whitespace.<br/>
- <code>"none"</code>: Do not ignore whitespace.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
ignore_comments
</th>
<td>
<code>"any"</code>, <code>"none"</code>
</td>
<td>
Set if comments should be ignored when deserializing.<br/>
- <code>"any"</code> (default): Ignore any comments.<br/>
- <code>"none"</code>: Do not ignore comments.
</td>
</tr>
<!--=================================================-->
</tbody>
</table>

### Deserialize from a sequence - structs with `#[xvalue(...)]` on the root of a type or no root attribute

The `#[xvalue(...)]` attribute can be applied to the root of a type to specify that the type can be deserialized from a text or CDATA node.

#### Root options

<table style="width:100%;">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody style="vertical-align:top;">
<!--=================================================-->
<tr>
<th>
value
</th>
<td>
<code>String</code>
</td>
<td>
The text value to serialize to and deserialize from. If the type is a unit struct, this attribute can be used to specify a text value to deserialize from.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
ignore_whitespace
</th>
<td>
<code>"any"</code>, <code>"none"</code>
</td>
<td>
Set if whitespace should be ignored when deserializing.<br/>
- <code>"any"</code> (default): Ignore any whitespace.<br/>
- <code>"none"</code>: Do not ignore whitespace.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
ignore_comments
</th>
<td>
<code>"any"</code>, <code>"none"</code>
</td>
<td>
Set if comments should be ignored when deserializing.<br/>
- <code>"any"</code> (default): Ignore any comments.<br/>
- <code>"none"</code>: Do not ignore comments.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
allow_unknown
</th>
<td>
<code>"any"</code>, <code>"at_end"</code>, <code>"none"</code>
</td>
<td>
Allow unknown values when deserializing.<br/>
- <code>"any"</code>: Allow any unknown values.<br/>
- <code>"at_end"</code> (default): Allow unknown values only at the end of the element.<br/>
- <code>"none"</code>: Do not allow unknown values at all.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
order
</th>
<td>
<code>"strict"</code>, <code>"none"</code>
</td>
<td>
Set if the order of values is important when deserializing.<br/>
- <code>"strict"</code>: The order of values must match the order in the struct or enum variant.<br/>
- <code>"none"</code> (default): The order of values does not matter, but the values must be present.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
with
</th>
<td>
<code>Path</code>
</td>
<td>
The path to the module that provides the serialization and deserialization functions. <code>::serialize</code> and <code>::deserialize</code> will be appended to this path and used as the <code>serialize_with</code> and <code>deserialize_with</code> functions.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
serialize_with
</th>
<td>
<code>Expr</code>
</td>
<td>
Use function to serialize the value. Should have signature like <code>pub fn serialize&lt;S: xmlity::Serializer&gt;(value: &T, serializer: S) -> Result&lt;S::Ok, S::Error&gt;</code>
</td>
</tr>
<!--=================================================-->
<tr>
<th>
deserialize_with
</th>
<td>
<code>Expr</code>
</td>
<td>
Use function to deserialize the value. Should have signature like <code>fn deserialize&lt;'de, D: xmlity::Deserializer&lt;'de&gt;&gt;(deserializer: D) -> Result&lt;T, D::Error&gt;</code>
</td>
</tr>
<!--=================================================-->
</tbody>
</table>

### Deserialize from an attribute - structs with `#[xattribute(...)]` on the root of a type

The `#[xattribute(...)]` attribute can be applied to the root of a type to specify that the type can be deserialized from an attribute.

#### Options

<table style="width:100%;">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody style="vertical-align:top;">
<!--=================================================-->
<tr>
<th>
name
</th>
<td>
<code>String</code>
</td>
<td>
Attribute name. If not specified, the name of the struct is used.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
namespace
</th>
<td>
<code>String</code>
</td>
<td>
The namespace of the attribute, defined as a string. This is exclusive with <code>namespace_expr</code>. If none of these are specified, the absence of a namespace is assumed. Must be a valid namespace string.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
namespace_expr
</th>
<td>
<code>Expr</code>
</td>
<td>
The namespace of the attribute given as an expression to an <code>xmlity::XmlNamespace</code> value. This is exclusive with <code>namespace</code>. If none of these are specified, the absence of a namespace is assumed.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
preferred_prefix
</th>
<td>
<code>String</code>
</td>
<td>
The preferred prefix for the attribute, defined as a string. This is exclusive with <code>enforce_prefix</code>. If none of these are specified, the absence of a prefix is assumed. Must be a valid XML prefix. (Serialize only)
</td>
</tr>
<!--=================================================-->
<tr>
<th>
enforce_prefix
</th>
<td>
<code>bool</code>
</td>
<td>
Always set the prefix of the attribute to the prefix set in <code>preferred_prefix</code>. (Serialize only)
</td>
</tr>
<!--=================================================-->
<tr>
<th>
deserialize_any_name
</th>
<td>
<code>bool</code>
</td>
<td>
Allow any name for the attribute when deserializing.
</td>
</tr>
<!--=================================================-->
</tbody>
</table>

### Deserialize as one of several types - enums with `#[xvalue(...)]` on the root of a type or no root attribute

The `#[xvalue(...)]` attribute can be applied to the root of an enum to specify that the type can be deserialized to one of several types.

#### Root options

<table style="width:100%;">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody style="vertical-align:top;">
<!--=================================================-->
<tr>
<th>
rename_all
</th>
<td>
<code>"lowercase"</code>, <code>"UPPERCASE"</code>, <code>"PascalCase"</code>, <code>"camelCase"</code>, <code>"snake_case"</code>, <code>"SCREAMING_SNAKE_CASE"</code>, <code>"kebab-case"</code>, <code>"SCREAMING-KEBAB-CASE"</code>
</td>
<td>
The text casing to use for unit variants when deserializing if they don't have values specified.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
with
</th>
<td>
<code>Path</code>
</td>
<td>
The path to the module that provides the serialization and deserialization functions. <code>::serialize</code> and <code>::deserialize</code> will be appended to this path and used as the <code>serialize_with</code> and <code>deserialize_with</code> functions.
</td>
</tr>
<!--=================================================-->
<tr>
<th>
serialize_with
</th>
<td>
<code>Expr</code>
</td>
<td>
Use function to serialize the value. Should have signature like <code>pub fn serialize&lt;S: xmlity::Serializer&gt;(value: &T, serializer: S) -> Result&lt;S::Ok, S::Error&gt;</code>
</td>
</tr>
<!--=================================================-->
<tr>
<th>
deserialize_with
</th>
<td>
<code>Expr</code>
</td>
<td>
Use function to deserialize the value. Should have signature like <code>fn deserialize&lt;'de, D: xmlity::Deserializer&lt;'de&gt;&gt;(deserializer: D) -> Result&lt;T, D::Error&gt;</code>
</td>
</tr>
<!--=================================================-->
</tbody>
</table>

#### Variant options

Variants have the same options as struct roots, and indeed work the same way.