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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use ;
use ;
use ;
/// Automatically implements `Merge` for a given structure.
///
/// This derive will create a near-identical partial version of the structure with each field being
/// optional. For example:
///
/// ```
/// # use mergeme_derive::Merge;
/// #
/// // Deriving `Merge` on this struct...
/// #[derive(Merge)]
/// #[partial(PartialConfig)]
/// struct Config {
/// name: String,
/// version: u32,
/// dependencies: Vec<String>,
/// }
/// ```
///
/// ```
/// // ...will create this struct as well.
/// struct PartialConfig {
/// name: Option<String>,
/// version: Option<u32>,
/// dependencies: Option<Vec<String>>,
/// }
/// ```
///
/// # Attributes
///
/// - `#[partial(Name)]`, `#[partial(Name, ...)]` (struct)
///
/// *What*: This specifies the name of the partial struct generated, as well as any attributes
/// that should be applied to the partial struct.
///
/// *Where*: This should annotate the struct itself.
///
/// *How*: The name should be a single identifier inside the parenthesis, and is commonly
/// prefixed with "Partial". Attributes to be applied to the partial struct may optionally be
/// specified after name, separated by commas.
///
/// *Required*
///
/// - `#[partial(...)]` (field)
///
/// *What*: This specifies attributes that should annotate fields within the partial struct.
///
/// *Where*: This should annotate fields within the struct.
///
/// *How*: This accepts a comma-separated list of attributes to be applied to the partial's field
/// inside the parenthesis. At least one attribute is required.
///
/// *Optional*
///
/// - `#[strategy(overwrite | merge)]` (field)
///
/// *What*: This specifies how this field should be merged.
///
/// *Where*: This should annotate the struct's fields.
///
/// *How*: The value should either be `overwrite` or `merge` in parenthesis. `overwrite` will
/// replace the base's field with the partial's if it exists, while `merge` will use the field
/// type's `Merge` implementation to combine the two values together.
///
/// *Optional*: Fields without this attribute default to `overwrite`.
///
/// # Examples
///
/// ```
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// // Name the partial version of this type `PartialConfig`.
/// #[partial(PartialConfig)]
/// struct Config {
/// // This field will be overwritten when merged. `#[strategy(overwrite)]` may be omitted.
/// #[strategy(overwrite)]
/// name: String,
///
/// // This field will also be overwritten when merged.
/// version: u32,
///
/// // This field will be combined when merged.
/// #[strategy(merge)]
/// dependencies: Vec<String>,
/// }
/// ```
///
/// Struct and field attributes can be applied to the partial struct using the `#[partial(...)]`
/// attribute. This is commonly used to implement `Default` for the partial struct, as its fields
/// are all `Option<T>`s.
///
/// ```
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// // Implement `Default` for the partial struct.
/// #[partial(PartialFish, derive(Default))]
/// struct Fish {
/// fins: u8,
/// memory: f32,
/// leashed: bool,
/// }
/// #
/// # let partial_fish = PartialFish::default();
/// #
/// # assert!(partial_fish.fins.is_none());
/// # assert!(partial_fish.memory.is_none());
/// # assert!(partial_fish.leashed.is_none());
/// ```
///
/// ```
/// # use mergeme_derive::Merge;
/// # use serde::Deserialize;
/// #
/// #[derive(Merge)]
/// // Implement `Deserialize` for the partial struct, denying unknown fields.
/// #[partial(PartialConfig, derive(Deserialize), serde(deny_unknown_fields))]
/// struct Config {
/// name: String,
///
/// // When deserializing the partial struct, accept the value of either "version" or "v" for
/// // this field.
/// #[partial(serde(alias = "v"))]
/// version: u32,
/// }
/// ```
///
/// Be warned that the fields of partial structs are all `Option<T>`s. This may make certain
/// attributes like `#[serde(default)]` behave differently.
///
/// ```
/// # use mergeme::Merge;
/// # use serde::Deserialize;
/// #
/// #[derive(Merge)]
/// #[partial(PartialTrickyDefault, derive(Deserialize))]
/// struct TrickyDefault {
/// // This attribute actually gets applied to a field like `value: Option<u64>`. Because of
/// // this, the default value will be `None` and not 0.
/// #[partial(serde(default))]
/// tricky_value: u64,
///
/// // This fixes the issue by making the partial field default to `Some(0)`. Much better!
/// #[partial(serde(default = "zero_default"))]
/// corrected_value: u64,
/// }
///
/// fn zero_default() -> Option<u64> {
/// Some(0)
/// }
/// #
/// # use std::collections::HashMap;
/// # use serde::de::{IntoDeserializer, value::{Error, MapDeserializer}};
/// #
/// # // Deserialize an empty hash map, forcing the default values to be used.
/// # let map: HashMap<&'static str, u64> = HashMap::new();
/// # let deserializer: MapDeserializer<'_, _, Error> = map.into_deserializer();
/// # let partial_tricky = PartialTrickyDefault::deserialize(deserializer).unwrap();
/// #
/// # assert!(partial_tricky.tricky_value.is_none());
/// # assert_eq!(partial_tricky.corrected_value, Some(0));
/// ```
///
/// Simple generics are supported, however only generic types that can merge with themselves can
/// be annotated with `#[strategy(merge)]`.
///
/// ```
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// #[partial(PartialNamedData)]
/// struct NamedData<T> {
/// name: String,
/// data: T,
/// }
/// ```
///
/// ```
/// # use mergeme::Merge;
/// #
/// #[derive(Merge)]
/// #[partial(PartialNamedData)]
/// // `T: Merge<T>` means any type that can be merged with itself.
/// struct NamedData<T: Merge<T>>
/// {
/// name: String,
/// #[strategy(merge)]
/// data: T,
/// }
/// ```
///
/// Unit structs can also derive `Merge`, however there is little point in doing so.
///
/// ```
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// #[partial(PartialConfig)]
/// struct Config;
/// ```
///
/// # Errors
///
/// This macro only works on named structs. Enums, unions, or tuple structs will not compile.
///
/// ```compile_fail
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// #[partial(PartialChoice)]
/// enum Choice {
/// A,
/// B,
/// }
/// ```
///
/// ```compile_fail
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// #[partial(PartialConfig)]
/// struct Config(bool, u8, Vec<String>);
/// ```
///
/// This macro requires a single `#[partial(...)]` attribute on the struct itself.
///
/// ```compile_fail
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// // Missing `#[partial(...)]`.
/// struct Config {
/// name: String,
/// dependencies: Vec<String>,
/// }
/// ```
///
/// ```compile_fail
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// // Too many `#[partial(...)]`s.
/// #[partial(PartialConfig1)]
/// #[partial(PartialConfig2)]
/// struct Config {
/// name: String,
/// dependencies: Vec<String>,
/// }
/// ```
///
/// This macro only supports the `overwrite` and `merge` strategies.
///
/// ```compile_fail
/// # use mergeme_derive::Merge;
/// #
/// #[derive(Merge)]
/// #[partial(PartialDog)]
/// struct Dog {
/// name: String,
/// // `add` is not a valid strategy.
/// #[strategy(add)]
/// age: u16,
/// }
/// ```
/// The implementation of `#[derive(Merge)]`.